old-graph: Make the graph symbol const and all symbol renderers static
[tig.git] / test / tools / test-graph.c
blob173c2fff7246f8eeb38c2137017770827b6f3431
1 /* Copyright (c) 2006-2013 Jonas Fonseca <fonseca@diku.dk>
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License as
5 * published by the Free Software Foundation; either version 2 of
6 * the License, or (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include "tig/tig.h"
15 #include "tig/util.h"
16 #include "tig/io.h"
17 #include "tig/graph.h"
19 #define USAGE \
20 "test-graph [--ascii]\n" \
21 "\n" \
22 "Example usage:\n" \
23 " # git log --pretty=raw --parents | ./test-graph\n" \
24 " # git log --pretty=raw --parents | ./test-graph --ascii"
26 struct commit {
27 char id[SIZEOF_REV];
28 struct graph_canvas canvas;
31 DEFINE_ALLOCATOR(realloc_commits, struct commit *, 8)
33 static const char *(*graph_fn)(const struct graph_symbol *) = graph_symbol_to_utf8;
35 static bool
36 print_symbol(void *__, struct graph_symbol *symbol, int color_id, bool first)
38 const char *chars = graph_fn(symbol);
40 printf("%s", chars + !!first);
41 return FALSE;
44 static void
45 print_commit(struct commit *commit, const char *title)
47 graph_foreach_symbol(&commit->canvas, print_symbol, NULL);
48 printf(" %s\n", title);
51 int
52 main(int argc, const char *argv[])
54 struct graph *graph;
55 struct io io = { };
56 struct buffer buf;
57 struct commit **commits = NULL;
58 size_t ncommits = 0;
59 struct commit *commit = NULL;
60 bool is_boundary;
62 if (argc > 1 && !strcmp(argv[1], "--ascii"))
63 graph_fn = graph_symbol_to_ascii;
65 if (isatty(STDIN_FILENO)) {
66 die(USAGE);
69 if (!(graph = init_graph()))
70 die("Failed to allocated graph");
71 if (!io_open(&io, "%s", ""))
72 die("IO");
74 while (!io_eof(&io)) {
75 for (; io_get(&io, &buf, '\n', TRUE); ) {
76 char *line = buf.data;
78 if (!prefixcmp(line, "commit ")) {
79 line += STRING_SIZE("commit ");
80 is_boundary = *line == '-';
82 if (is_boundary)
83 line++;
85 if (!realloc_commits(&commits, ncommits, 1))
86 die("Commits");
88 commit = calloc(1, sizeof(*commit));
89 if (!commit)
90 die("Commit");
91 commits[ncommits++] = commit;
92 string_copy_rev(commit->id, line);
93 graph_add_commit(graph, &commit->canvas, commit->id, line, is_boundary);
94 graph_render_parents(graph, &commit->canvas);
96 if ((line = io_memchr(&buf, line, 0))) {
97 print_commit(commit, line);
98 commit = NULL;
101 } else if (!prefixcmp(line, " ")) {
103 if (!commit)
104 continue;
106 print_commit(commit, line + 4);
108 commit = NULL;
113 return 0;
116 /* vim: set ts=8 sw=8 noexpandtab: */