3 * Copyright (c) Jonas Fonseca, 2006
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
21 #define MSG_HELP "(q)uit, (s)hell, (j) down, (k) up"
30 typedef void (*pipe_reader_T)(char *, int);
34 pipe_reader_T pipe_reader;
38 put_status(char *msg, ...)
44 wmove(statuswin, 0, 0);
45 vwprintw(statuswin, msg, args);
59 /* do your non-curses wrapup here */
71 if (use_default_colors() != ERR)
74 init_pair(COLOR_BLACK, COLOR_BLACK, bg);
75 init_pair(COLOR_GREEN, COLOR_GREEN, bg);
76 init_pair(COLOR_RED, COLOR_RED, bg);
77 init_pair(COLOR_CYAN, COLOR_CYAN, bg);
78 init_pair(COLOR_WHITE, COLOR_WHITE, bg);
79 init_pair(COLOR_MAGENTA, COLOR_MAGENTA, bg);
80 init_pair(COLOR_BLUE, COLOR_BLUE, bg);
81 init_pair(COLOR_YELLOW, COLOR_YELLOW, bg);
91 initscr(); /* initialize the curses library */
92 nonl(); /* tell curses not to do NL->CR/NL on output */
93 cbreak(); /* take input chars one at a time, no wait for \n */
94 noecho(); /* don't echo input */
95 leaveok(stdscr, TRUE);
100 getmaxyx(stdscr, y, x);
103 titlewin = newwin(1, 0, y - 2, 0);
105 wattrset(titlewin, COLOR_PAIR(COLOR_GREEN));
106 waddch(titlewin, ACS_VLINE);
107 wprintw(titlewin, "%s", "cg-view");
108 waddch(titlewin, ACS_LTEE);
109 whline(titlewin, ACS_HLINE, x);
112 statuswin = newwin(1, 0, y - 1, 0);
114 wattrset(statuswin, COLOR_PAIR(COLOR_GREEN));
115 put_status(MSG_HELP);
117 mainwin = newwin(y - 1, 0, 0, 0);
118 scrollok(mainwin, TRUE);
119 keypad(mainwin, TRUE); /* enable keyboard mapping */
127 "git-rev-list HEAD^..HEAD | " \
128 "git-diff-tree --stdin --pretty -r --cc --always --stat"
132 "git-rev-list $(git-rev-parse --since=1.month) HEAD | " \
133 "git-diff-tree --stdin --pretty -r --root"
136 "git-rev-list HEAD | git-diff-tree --stdin --pretty -r --root"
139 log_reader(char *line, int lineno)
141 static int log_reader_skip;
144 wattrset(mainwin, A_NORMAL);
149 if (!strncmp("commit ", line, 7)) {
150 wattrset(mainwin, COLOR_PAIR(COLOR_GREEN));
152 } else if (!strncmp("Author: ", line, 8)) {
153 wattrset(mainwin, COLOR_PAIR(COLOR_CYAN));
155 } else if (!strncmp("Date: ", line, 6)) {
156 wattrset(mainwin, COLOR_PAIR(COLOR_YELLOW));
158 } else if (!strncmp("diff --git ", line, 11)) {
159 wattrset(mainwin, COLOR_PAIR(COLOR_YELLOW));
161 } else if (!strncmp("diff-tree ", line, 10)) {
162 wattrset(mainwin, COLOR_PAIR(COLOR_BLUE));
164 } else if (!strncmp("index ", line, 6)) {
165 wattrset(mainwin, COLOR_PAIR(COLOR_BLUE));
167 } else if (line[0] == '-') {
168 wattrset(mainwin, COLOR_PAIR(COLOR_RED));
170 } else if (line[0] == '+') {
171 wattrset(mainwin, COLOR_PAIR(COLOR_GREEN));
173 } else if (line[0] == '@') {
174 wattrset(mainwin, COLOR_PAIR(COLOR_MAGENTA));
176 } else if (line[0] == ':') {
181 } else if (log_reader_skip) {
187 wattrset(mainwin, A_NORMAL);
190 mvwaddstr(mainwin, lineno, 0, line);
194 open_pipe(char *cmd, pipe_reader_T reader)
196 pipe = popen(cmd, "r");
198 pipe_reader = reader;
200 wmove(mainwin, 0, 0);
201 put_status("Loading...");
212 while ((line = fgets(buffer, sizeof(buffer), pipe))) {
218 linelen = strlen(line);
220 line[linelen - 1] = 0;
222 pipe_reader(line, pipe_lineno++);
225 if (feof(pipe) || ferror(pipe)) {
226 pipe_reader(NULL, pipe_lineno - 1);
230 put_status("%s (lines %d)", MSG_HELP, pipe_lineno - 1);
239 main(int argc, char *argv[])
243 //pipe = open_pipe(LOG_CMD, log_reader);
248 if (pipe) read_pipe(20);
249 if (pipe) nodelay(mainwin, TRUE);
251 c = wgetch(mainwin); /* refresh, accept single keystroke of input */
253 if (pipe) nodelay(mainwin, FALSE);
255 /* No input from wgetch() with nodelay() enabled. */
259 /* Process the command keystroke */
281 pipe = open_pipe(DIFF_CMD, log_reader);
285 pipe = open_pipe(LOG_CMD, log_reader);
289 mvwaddstr(statuswin, 0, 0, "Shelling out...");
290 def_prog_mode(); /* save current tty modes */
291 endwin(); /* restore original tty modes */
292 system("sh"); /* run shell */
295 mvwaddstr(statuswin, 0, 0, MSG_HELP);