Add search keymap with keys for navigating search results
[tig.git] / test / tools / test-graph.c
blob332a7266307fb5c1b6939276e5ef604d5953fde7
1 /* Copyright (c) 2006-2015 Jonas Fonseca <jonas.fonseca@gmail.com>
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 *);
35 static bool
36 print_symbol(void *__, const struct graph *graph, const 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 graph *graph, struct commit *commit, const char *title)
47 graph->foreach_symbol(graph, &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 = {0};
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 (isatty(STDIN_FILENO)) {
63 die(USAGE);
66 if (!(graph = init_graph(GRAPH_DISPLAY_V2)))
67 die("Failed to allocated graph");
69 if (argc > 1 && !strcmp(argv[1], "--ascii"))
70 graph_fn = graph->symbol_to_ascii;
71 else
72 graph_fn = graph->symbol_to_utf8;
74 if (!io_open(&io, "%s", ""))
75 die("IO");
77 while (!io_eof(&io)) {
78 for (; io_get(&io, &buf, '\n', TRUE); ) {
79 char *line = buf.data;
81 if (!prefixcmp(line, "commit ")) {
82 line += STRING_SIZE("commit ");
83 is_boundary = *line == '-';
85 if (is_boundary)
86 line++;
88 if (!realloc_commits(&commits, ncommits, 1))
89 die("Commits");
91 commit = calloc(1, sizeof(*commit));
92 if (!commit)
93 die("Commit");
94 commits[ncommits++] = commit;
95 string_copy_rev(commit->id, line);
96 graph->add_commit(graph, &commit->canvas, commit->id, line, is_boundary);
97 graph->render_parents(graph, &commit->canvas);
99 if ((line = io_memchr(&buf, line, 0))) {
100 print_commit(graph, commit, line);
101 commit = NULL;
104 } else if (!prefixcmp(line, " ")) {
106 if (!commit)
107 continue;
109 print_commit(graph, commit, line + 4);
111 commit = NULL;
116 return 0;
119 /* vim: set ts=8 sw=8 noexpandtab: */