Use --topo-order; fclose(stdin); space cleanup
[tig] / tig.c
CommitLineData
4a2909a7
JF
1/* Copyright (c) 2006 Jonas Fonseca <fonseca@diku.dk>
2 * See license info at the bottom. */
b801d8b2
JF
3/**
4 * TIG(1)
5 * ======
6 *
7 * NAME
8 * ----
9 * tig - text-mode interface for git
10 *
11 * SYNOPSIS
12 * --------
13 * [verse]
03a93dbb
JF
14 * tig [options]
15 * tig [options] [--] [git log options]
b76c2afc
JF
16 * tig [options] log [git log options]
17 * tig [options] diff [git diff options]
8855ada4
JF
18 * tig [options] show [git show options]
19 * tig [options] < [git command output]
b801d8b2
JF
20 *
21 * DESCRIPTION
22 * -----------
23 * Browse changes in a git repository.
b801d8b2
JF
24 **/
25
b76c2afc
JF
26#ifndef VERSION
27#define VERSION "tig-0.1"
28#endif
29
8855ada4
JF
30#ifndef DEBUG
31#define NDEBUG
32#endif
33
22f66b0a 34#include <assert.h>
4c6fabc2 35#include <errno.h>
22f66b0a
JF
36#include <ctype.h>
37#include <signal.h>
b801d8b2 38#include <stdarg.h>
b801d8b2 39#include <stdio.h>
22f66b0a 40#include <stdlib.h>
b801d8b2 41#include <string.h>
6908bdbd 42#include <unistd.h>
b76c2afc 43#include <time.h>
b801d8b2
JF
44
45#include <curses.h>
b801d8b2
JF
46
47static void die(const char *err, ...);
48static void report(const char *msg, ...);
6b161b31
JF
49static void set_nonblocking_input(int boolean);
50
51#define ABS(x) ((x) >= 0 ? (x) : -(x))
52#define MIN(x, y) ((x) < (y) ? (x) : (y))
53
54#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
55#define STRING_SIZE(x) (sizeof(x) - 1)
b76c2afc 56
2e8488b4
JF
57#define SIZEOF_REF 256 /* Size of symbolic or SHA1 ID. */
58#define SIZEOF_CMD 1024 /* Size of command buffer. */
b801d8b2 59
82e78006
JF
60/* This color name can be used to refer to the default term colors. */
61#define COLOR_DEFAULT (-1)
78c70acd 62
82e78006 63/* The format and size of the date column in the main view. */
4c6fabc2 64#define DATE_FORMAT "%Y-%m-%d %H:%M"
6b161b31 65#define DATE_COLS STRING_SIZE("2006-04-29 14:21 ")
4c6fabc2 66
a28bcc22 67/* The default interval between line numbers. */
4a2909a7 68#define NUMBER_INTERVAL 1
82e78006 69
a28bcc22
JF
70#define SCALE_SPLIT_VIEW(height) ((height) * 2 / 3)
71
8855ada4 72/* Some ascii-shorthands fitted into the ncurses namespace. */
a28bcc22
JF
73#define KEY_TAB '\t'
74#define KEY_RETURN '\r'
4a2909a7
JF
75#define KEY_ESC 27
76
a28bcc22 77/* User action requests. */
4a2909a7 78enum request {
a28bcc22
JF
79 /* Offset all requests to avoid conflicts with ncurses getch values. */
80 REQ_OFFSET = KEY_MAX + 1,
81
4a2909a7 82 /* XXX: Keep the view request first and in sync with views[]. */
2e8488b4 83 REQ_VIEW_MAIN,
4a2909a7
JF
84 REQ_VIEW_DIFF,
85 REQ_VIEW_LOG,
2e8488b4 86 REQ_VIEW_HELP,
6908bdbd 87 REQ_VIEW_PAGER,
4a2909a7 88
6b161b31 89 REQ_ENTER,
03a93dbb
JF
90 REQ_QUIT,
91 REQ_PROMPT,
4a2909a7
JF
92 REQ_SCREEN_REDRAW,
93 REQ_SCREEN_UPDATE,
03a93dbb
JF
94 REQ_SHOW_VERSION,
95 REQ_STOP_LOADING,
4a2909a7 96 REQ_TOGGLE_LINE_NUMBERS,
03a93dbb 97 REQ_VIEW_NEXT,
4a2909a7
JF
98
99 REQ_MOVE_UP,
100 REQ_MOVE_DOWN,
101 REQ_MOVE_PAGE_UP,
102 REQ_MOVE_PAGE_DOWN,
103 REQ_MOVE_FIRST_LINE,
104 REQ_MOVE_LAST_LINE,
105
106 REQ_SCROLL_LINE_UP,
107 REQ_SCROLL_LINE_DOWN,
108 REQ_SCROLL_PAGE_UP,
109 REQ_SCROLL_PAGE_DOWN,
110};
111
4c6fabc2 112struct commit {
a28bcc22 113 char id[41]; /* SHA1 ID. */
6b161b31 114 char title[75]; /* The first line of the commit message. */
a28bcc22
JF
115 char author[75]; /* The author of the commit. */
116 struct tm time; /* Date from the author ident. */
4c6fabc2
JF
117};
118
03a93dbb
JF
119/*
120 * String helpers
121 */
78c70acd 122
82e78006
JF
123static inline void
124string_ncopy(char *dst, char *src, int dstlen)
125{
126 strncpy(dst, src, dstlen - 1);
127 dst[dstlen - 1] = 0;
03a93dbb 128
82e78006
JF
129}
130
131/* Shorthand for safely copying into a fixed buffer. */
132#define string_copy(dst, src) \
133 string_ncopy(dst, src, sizeof(dst))
134
03a93dbb
JF
135/* Shell quoting
136 *
137 * NOTE: The following is a slightly modified copy of the git project's shell
138 * quoting routines found in the quote.c file.
139 *
140 * Help to copy the thing properly quoted for the shell safety. any single
141 * quote is replaced with '\'', any exclamation point is replaced with '\!',
142 * and the whole thing is enclosed in a
143 *
144 * E.g.
145 * original sq_quote result
146 * name ==> name ==> 'name'
147 * a b ==> a b ==> 'a b'
148 * a'b ==> a'\''b ==> 'a'\''b'
149 * a!b ==> a'\!'b ==> 'a'\!'b'
150 */
151
152static size_t
153sq_quote(char buf[SIZEOF_CMD], size_t bufsize, const char *src)
154{
155 char c;
156
157#define BUFPUT(x) ( (bufsize < SIZEOF_CMD) && (buf[bufsize++] = (x)) )
158
159 BUFPUT('\'');
160 while ((c = *src++)) {
161 if (c == '\'' || c == '!') {
162 BUFPUT('\'');
163 BUFPUT('\\');
164 BUFPUT(c);
165 BUFPUT('\'');
166 } else {
167 BUFPUT(c);
168 }
169 }
170 BUFPUT('\'');
171
172 return bufsize;
173}
174
82e78006 175
b76c2afc
JF
176/**
177 * OPTIONS
178 * -------
179 **/
180
2e8488b4
JF
181static int opt_line_number = FALSE;
182static int opt_num_interval = NUMBER_INTERVAL;
6b161b31 183static enum request opt_request = REQ_VIEW_MAIN;
03a93dbb 184static char opt_cmd[SIZEOF_CMD] = "";
6908bdbd 185static FILE *opt_pipe = NULL;
b76c2afc 186
b76c2afc 187/* Returns the index of log or diff command or -1 to exit. */
8855ada4 188static bool
b76c2afc
JF
189parse_options(int argc, char *argv[])
190{
191 int i;
192
193 for (i = 1; i < argc; i++) {
194 char *opt = argv[i];
195
196 /**
b76c2afc
JF
197 * -l::
198 * Start up in log view.
199 **/
6b161b31 200 if (!strcmp(opt, "-l")) {
4a2909a7 201 opt_request = REQ_VIEW_LOG;
6b161b31
JF
202 continue;
203 }
b76c2afc
JF
204
205 /**
206 * -d::
207 * Start up in diff view.
208 **/
6b161b31 209 if (!strcmp(opt, "-d")) {
4a2909a7 210 opt_request = REQ_VIEW_DIFF;
6b161b31
JF
211 continue;
212 }
b76c2afc
JF
213
214 /**
2e8488b4 215 * -n[INTERVAL], --line-number[=INTERVAL]::
b76c2afc 216 * Prefix line numbers in log and diff view.
2e8488b4 217 * Optionally, with interval different than each line.
b76c2afc 218 **/
6b161b31 219 if (!strncmp(opt, "-n", 2) ||
8855ada4 220 !strncmp(opt, "--line-number", 13)) {
2e8488b4
JF
221 char *num = opt;
222
223 if (opt[1] == 'n') {
224 num = opt + 2;
225
226 } else if (opt[STRING_SIZE("--line-number")] == '=') {
227 num = opt + STRING_SIZE("--line-number=");
228 }
229
230 if (isdigit(*num))
231 opt_num_interval = atoi(num);
232
6b161b31
JF
233 opt_line_number = TRUE;
234 continue;
235 }
b76c2afc
JF
236
237 /**
238 * -v, --version::
239 * Show version and exit.
240 **/
6b161b31 241 if (!strcmp(opt, "-v") ||
8855ada4 242 !strcmp(opt, "--version")) {
b76c2afc 243 printf("tig version %s\n", VERSION);
8855ada4 244 return FALSE;
6b161b31 245 }
b76c2afc
JF
246
247 /**
03a93dbb 248 * \--::
889cb462 249 * End of tig(1) options. Useful when specifying commands
03a93dbb
JF
250 * for the main view. Example:
251 *
889cb462 252 * $ tig -- --since=1.month
b76c2afc 253 **/
6908bdbd
JF
254 if (!strcmp(opt, "--")) {
255 i++;
256 break;
257 }
03a93dbb 258
8855ada4
JF
259 /**
260 * log [options]::
261 * Open log view using the given git log options.
262 *
263 * diff [options]::
264 * Open diff view using the given git diff options.
265 *
266 * show [options]::
267 * Open diff view using the given git show options.
268 **/
269 if (!strcmp(opt, "log") ||
270 !strcmp(opt, "diff") ||
271 !strcmp(opt, "show")) {
272 opt_request = opt[0] == 'l'
273 ? REQ_VIEW_LOG : REQ_VIEW_DIFF;
274 break;
275 }
276
277 /* Make stuff like:
889cb462 278 *
03a93dbb
JF
279 * $ tig tag-1.0..HEAD
280 *
8855ada4 281 * work. */
03a93dbb 282 if (opt[0] && opt[0] != '-')
6908bdbd 283 break;
6b161b31
JF
284
285 die("unknown command '%s'", opt);
b76c2afc
JF
286 }
287
6908bdbd 288 if (!isatty(STDIN_FILENO)) {
8855ada4
JF
289 /**
290 * Pager mode
291 * ~~~~~~~~~~
292 * If stdin is a pipe, any log or diff options will be ignored and the
293 * pager view will be opened loading data from stdin. The pager mode
294 * can be used for colorizing output from various git commands.
295 *
296 * Example on how to colorize the output of git-show(1):
297 *
298 * $ git show | tig
299 **/
6908bdbd
JF
300 opt_request = REQ_VIEW_PAGER;
301 opt_pipe = stdin;
302
303 } else if (i < argc) {
304 size_t buf_size;
305
8855ada4
JF
306 /**
307 * Git command options
308 * ~~~~~~~~~~~~~~~~~~~
8855ada4
JF
309 * All git command options specified on the command line will
310 * be passed to the given command and all will be shell quoted
311 * before used.
312 *
313 * NOTE: It is possible to specify options even for the main
eb98559e 314 * view. If doing this you should not touch the `--pretty`
8855ada4
JF
315 * option.
316 *
317 * Example on how to open the log view and show both author and
318 * committer information:
319 *
320 * $ tig log --pretty=fuller
321 **/
322
6908bdbd 323 if (opt_request == REQ_VIEW_MAIN)
8855ada4
JF
324 /* XXX: This is vulnerable to the user overriding
325 * options required for the main view parser. */
6908bdbd
JF
326 string_copy(opt_cmd, "git log --stat --pretty=raw");
327 else
328 string_copy(opt_cmd, "git");
329 buf_size = strlen(opt_cmd);
330
331 while (buf_size < sizeof(opt_cmd) && i < argc) {
332 opt_cmd[buf_size++] = ' ';
333 buf_size = sq_quote(opt_cmd, buf_size, argv[i++]);
334 }
335
336 if (buf_size >= sizeof(opt_cmd))
337 die("command too long");
338
339 opt_cmd[buf_size] = 0;
340
341 }
342
8855ada4 343 return TRUE;
b76c2afc
JF
344}
345
346
4a2909a7
JF
347/**
348 * KEYS
349 * ----
4a2909a7
JF
350 **/
351
352#define HELP "(d)iff, (l)og, (m)ain, (q)uit, (v)ersion, (h)elp"
353
354struct keymap {
355 int alias;
356 int request;
357};
358
359struct keymap keymap[] = {
6b161b31
JF
360 /**
361 * View switching
362 * ~~~~~~~~~~~~~~
363 * d::
364 * Switch to diff view.
365 * l::
366 * Switch to log view.
367 * m::
368 * Switch to main view.
8855ada4
JF
369 * p::
370 * Switch to pager view.
6b161b31
JF
371 * h::
372 * Show man page.
373 * Return::
374 * If in main view split the view
375 * and show the diff in the bottom view.
03a93dbb
JF
376 * Tab::
377 * Switch to next view.
6b161b31
JF
378 **/
379 { 'm', REQ_VIEW_MAIN },
380 { 'd', REQ_VIEW_DIFF },
381 { 'l', REQ_VIEW_LOG },
8855ada4 382 { 'p', REQ_VIEW_PAGER },
6b161b31
JF
383 { 'h', REQ_VIEW_HELP },
384
03a93dbb 385 { KEY_TAB, REQ_VIEW_NEXT },
6b161b31
JF
386 { KEY_RETURN, REQ_ENTER },
387
388 /**
389 * Cursor navigation
390 * ~~~~~~~~~~~~~~~~~
391 * Up, k::
392 * Move curser one line up.
393 * Down, j::
394 * Move cursor one line down.
395 * Page Up::
396 * Move curser one page up.
397 * Page Down::
398 * Move cursor one page down.
399 * Home::
400 * Jump to first line.
401 * End::
402 * Jump to last line.
403 **/
4a2909a7
JF
404 { KEY_UP, REQ_MOVE_UP },
405 { 'k', REQ_MOVE_UP },
406 { KEY_DOWN, REQ_MOVE_DOWN },
407 { 'j', REQ_MOVE_DOWN },
408 { KEY_HOME, REQ_MOVE_FIRST_LINE },
409 { KEY_END, REQ_MOVE_LAST_LINE },
410 { KEY_NPAGE, REQ_MOVE_PAGE_DOWN },
411 { KEY_PPAGE, REQ_MOVE_PAGE_UP },
412
6b161b31
JF
413 /**
414 * Scrolling
415 * ~~~~~~~~~
416 * Insert::
417 * Scroll view one line up.
418 * Delete::
419 * Scroll view one line down.
420 * w::
421 * Scroll view one page up.
422 * s::
423 * Scroll view one page down.
424 **/
a28bcc22
JF
425 { KEY_IC, REQ_SCROLL_LINE_UP },
426 { KEY_DC, REQ_SCROLL_LINE_DOWN },
427 { 'w', REQ_SCROLL_PAGE_UP },
428 { 's', REQ_SCROLL_PAGE_DOWN },
429
6b161b31
JF
430 /**
431 * Misc
432 * ~~~~
433 * q, Escape::
434 * Quit
435 * r::
436 * Redraw screen.
437 * z::
438 * Stop all background loading.
439 * v::
440 * Show version.
441 * n::
442 * Toggle line numbers on/off.
03a93dbb 443 * ':'::
8855ada4
JF
444 * Open prompt. This allows you to specify what git command to run.
445 * Example:
446 *
447 * :log -p
448 *
6b161b31 449 **/
4a2909a7
JF
450 { KEY_ESC, REQ_QUIT },
451 { 'q', REQ_QUIT },
452 { 'z', REQ_STOP_LOADING },
453 { 'v', REQ_SHOW_VERSION },
454 { 'r', REQ_SCREEN_REDRAW },
455 { 'n', REQ_TOGGLE_LINE_NUMBERS },
03a93dbb 456 { ':', REQ_PROMPT },
4a2909a7
JF
457
458 /* wgetch() with nodelay() enabled returns ERR when there's no input. */
459 { ERR, REQ_SCREEN_UPDATE },
460};
461
6b161b31
JF
462static enum request
463get_request(int key)
4a2909a7
JF
464{
465 int i;
466
467 for (i = 0; i < ARRAY_SIZE(keymap); i++)
6b161b31 468 if (keymap[i].alias == key)
4a2909a7
JF
469 return keymap[i].request;
470
6b161b31 471 return (enum request) key;
4a2909a7
JF
472}
473
474
b76c2afc
JF
475/*
476 * Line-oriented content detection.
6b161b31
JF
477 */
478
2e8488b4 479#define LINE_INFO \
6b161b31
JF
480/* Line type String to match Foreground Background Attributes
481 * --------- --------------- ---------- ---------- ---------- */ \
a28bcc22
JF
482/* Diff markup */ \
483LINE(DIFF, "diff --git ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
8855ada4 484LINE(DIFF_INDEX, "index ", COLOR_BLUE, COLOR_DEFAULT, 0), \
a28bcc22
JF
485LINE(DIFF_CHUNK, "@@", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
486LINE(DIFF_ADD, "+", COLOR_GREEN, COLOR_DEFAULT, 0), \
487LINE(DIFF_DEL, "-", COLOR_RED, COLOR_DEFAULT, 0), \
488LINE(DIFF_OLDMODE, "old mode ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
489LINE(DIFF_NEWMODE, "new mode ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
490LINE(DIFF_COPY, "copy ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
491LINE(DIFF_RENAME, "rename ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
492LINE(DIFF_SIM, "similarity ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
493LINE(DIFF_DISSIM, "dissimilarity ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
494/* Pretty print commit header */ \
6908bdbd 495LINE(PP_AUTHOR, "Author: ", COLOR_CYAN, COLOR_DEFAULT, 0), \
8855ada4 496LINE(PP_COMMIT, "Commit: ", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
6908bdbd
JF
497LINE(PP_MERGE, "Merge: ", COLOR_BLUE, COLOR_DEFAULT, 0), \
498LINE(PP_DATE, "Date: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
8855ada4
JF
499LINE(PP_ADATE, "AuthorDate: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
500LINE(PP_CDATE, "CommitDate: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
a28bcc22
JF
501/* Raw commit header */ \
502LINE(COMMIT, "commit ", COLOR_GREEN, COLOR_DEFAULT, 0), \
503LINE(PARENT, "parent ", COLOR_BLUE, COLOR_DEFAULT, 0), \
504LINE(TREE, "tree ", COLOR_BLUE, COLOR_DEFAULT, 0), \
8855ada4 505LINE(AUTHOR, "author ", COLOR_CYAN, COLOR_DEFAULT, 0), \
a28bcc22
JF
506LINE(COMMITTER, "committer ", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
507/* Misc */ \
508LINE(DIFF_TREE, "diff-tree ", COLOR_BLUE, COLOR_DEFAULT, 0), \
509LINE(SIGNOFF, " Signed-off-by", COLOR_YELLOW, COLOR_DEFAULT, 0), \
510/* UI colors */ \
511LINE(DEFAULT, "", COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL), \
512LINE(CURSOR, "", COLOR_WHITE, COLOR_GREEN, A_BOLD), \
513LINE(STATUS, "", COLOR_GREEN, COLOR_DEFAULT, 0), \
6b161b31
JF
514LINE(TITLE_BLUR, "", COLOR_WHITE, COLOR_BLUE, 0), \
515LINE(TITLE_FOCUS, "", COLOR_WHITE, COLOR_BLUE, A_BOLD), \
a28bcc22
JF
516LINE(MAIN_DATE, "", COLOR_BLUE, COLOR_DEFAULT, 0), \
517LINE(MAIN_AUTHOR, "", COLOR_GREEN, COLOR_DEFAULT, 0), \
518LINE(MAIN_COMMIT, "", COLOR_DEFAULT, COLOR_DEFAULT, 0), \
519LINE(MAIN_DELIM, "", COLOR_MAGENTA, COLOR_DEFAULT, 0),
2e8488b4 520
78c70acd 521enum line_type {
2e8488b4
JF
522#define LINE(type, line, fg, bg, attr) \
523 LINE_##type
524 LINE_INFO
525#undef LINE
78c70acd
JF
526};
527
528struct line_info {
2e8488b4
JF
529 char *line; /* The start of line to match. */
530 int linelen; /* Size of string to match. */
531 int fg, bg, attr; /* Color and text attributes for the lines. */
78c70acd
JF
532};
533
2e8488b4 534static struct line_info line_info[] = {
78c70acd 535#define LINE(type, line, fg, bg, attr) \
a28bcc22 536 { (line), STRING_SIZE(line), (fg), (bg), (attr) }
2e8488b4
JF
537 LINE_INFO
538#undef LINE
78c70acd
JF
539};
540
2e8488b4
JF
541static enum line_type
542get_line_type(char *line)
78c70acd
JF
543{
544 int linelen = strlen(line);
a28bcc22 545 enum line_type type;
78c70acd 546
a28bcc22 547 for (type = 0; type < ARRAY_SIZE(line_info); type++)
2e8488b4 548 /* Case insensitive search matches Signed-off-by lines better. */
a28bcc22
JF
549 if (linelen >= line_info[type].linelen &&
550 !strncasecmp(line_info[type].line, line, line_info[type].linelen))
551 return type;
78c70acd 552
2e8488b4 553 return LINE_DEFAULT;
78c70acd
JF
554}
555
2e8488b4 556static inline int
78c70acd
JF
557get_line_attr(enum line_type type)
558{
2e8488b4
JF
559 assert(type < ARRAY_SIZE(line_info));
560 return COLOR_PAIR(type) | line_info[type].attr;
78c70acd
JF
561}
562
563static void
564init_colors(void)
565{
82e78006
JF
566 int default_bg = COLOR_BLACK;
567 int default_fg = COLOR_WHITE;
a28bcc22 568 enum line_type type;
78c70acd
JF
569
570 start_color();
571
572 if (use_default_colors() != ERR) {
82e78006
JF
573 default_bg = -1;
574 default_fg = -1;
78c70acd
JF
575 }
576
a28bcc22
JF
577 for (type = 0; type < ARRAY_SIZE(line_info); type++) {
578 struct line_info *info = &line_info[type];
82e78006
JF
579 int bg = info->bg == COLOR_DEFAULT ? default_bg : info->bg;
580 int fg = info->fg == COLOR_DEFAULT ? default_fg : info->fg;
78c70acd 581
a28bcc22 582 init_pair(type, fg, bg);
78c70acd
JF
583 }
584}
585
586
b801d8b2
JF
587/*
588 * Viewer
589 */
590
591struct view {
03a93dbb 592 const char *name; /* View name */
49f2b43f 593 const char *cmdfmt; /* Default command line format */
03a93dbb
JF
594 char *id; /* Points to either of ref_{head,commit} */
595 size_t objsize; /* Size of objects in the line index */
6b161b31
JF
596
597 struct view_ops {
8855ada4 598 /* Draw one line; @lineno must be < view->height. */
6b161b31 599 bool (*draw)(struct view *view, unsigned int lineno);
8855ada4 600 /* Read one line; updates view->line. */
6b161b31 601 bool (*read)(struct view *view, char *line);
8855ada4 602 /* Depending on view, change display based on current line. */
6b161b31
JF
603 bool (*enter)(struct view *view);
604 } *ops;
22f66b0a 605
03a93dbb 606 char cmd[SIZEOF_CMD]; /* Command buffer */
49f2b43f
JF
607 char ref[SIZEOF_REF]; /* Hovered commit reference */
608 char vid[SIZEOF_REF]; /* View ID. Set to id member when updating. */
2e8488b4 609
8855ada4
JF
610 int height, width; /* The width and height of the main window */
611 WINDOW *win; /* The main window */
612 WINDOW *title; /* The title window living below the main window */
b801d8b2
JF
613
614 /* Navigation */
615 unsigned long offset; /* Offset of the window top */
616 unsigned long lineno; /* Current line number */
617
618 /* Buffering */
619 unsigned long lines; /* Total number of lines */
8855ada4
JF
620 void **line; /* Line index; each line contains user data */
621 unsigned int digits; /* Number of digits in the lines member. */
b801d8b2
JF
622
623 /* Loading */
624 FILE *pipe;
2e8488b4 625 time_t start_time;
b801d8b2
JF
626};
627
6b161b31
JF
628static struct view_ops pager_ops;
629static struct view_ops main_ops;
a28bcc22 630
fd85fef1 631#define DIFF_CMD \
8855ada4 632 "git show --patch-with-stat --find-copies-harder -B -C %s"
b801d8b2
JF
633
634#define LOG_CMD \
9d3f5834 635 "git log --cc --stat -n100 %s"
b801d8b2 636
fd85fef1 637#define MAIN_CMD \
80ce96ea 638 "git log --topo-order --stat --pretty=raw %s"
fd85fef1 639
2e8488b4
JF
640#define HELP_CMD \
641 "man tig 2> /dev/null"
642
49f2b43f
JF
643char ref_head[SIZEOF_REF] = "HEAD";
644char ref_commit[SIZEOF_REF] = "HEAD";
645
b801d8b2 646static struct view views[] = {
6908bdbd
JF
647 { "main", MAIN_CMD, ref_head, sizeof(struct commit), &main_ops },
648 { "diff", DIFF_CMD, ref_commit, sizeof(char), &pager_ops },
649 { "log", LOG_CMD, ref_head, sizeof(char), &pager_ops },
650 { "help", HELP_CMD, ref_head, sizeof(char), &pager_ops },
8855ada4 651 { "pager", "", "static", sizeof(char), &pager_ops },
b801d8b2
JF
652};
653
a28bcc22
JF
654#define VIEW(req) (&views[(req) - REQ_OFFSET - 1])
655
4c6fabc2 656/* The display array of active views and the index of the current view. */
6b161b31 657static struct view *display[2];
4c6fabc2 658static unsigned int current_view;
22f66b0a 659
b801d8b2 660#define foreach_view(view, i) \
6b161b31 661 for (i = 0; i < ARRAY_SIZE(display) && (view = display[i]); i++)
b801d8b2 662
4c6fabc2 663
b801d8b2 664static void
82e78006 665redraw_view_from(struct view *view, int lineno)
b801d8b2 666{
82e78006 667 assert(0 <= lineno && lineno < view->height);
b801d8b2 668
82e78006 669 for (; lineno < view->height; lineno++) {
6b161b31 670 if (!view->ops->draw(view, lineno))
fd85fef1 671 break;
b801d8b2
JF
672 }
673
674 redrawwin(view->win);
675 wrefresh(view->win);
676}
677
b76c2afc 678static void
82e78006
JF
679redraw_view(struct view *view)
680{
681 wclear(view->win);
682 redraw_view_from(view, 0);
683}
684
6b161b31
JF
685static void
686resize_display(void)
b76c2afc 687{
03a93dbb 688 int offset, i;
6b161b31
JF
689 struct view *base = display[0];
690 struct view *view = display[1] ? display[1] : display[0];
b76c2afc 691
6b161b31 692 /* Setup window dimensions */
b76c2afc 693
03a93dbb 694 getmaxyx(stdscr, base->height, base->width);
b76c2afc 695
6b161b31 696 /* Make room for the status window. */
03a93dbb 697 base->height -= 1;
6b161b31
JF
698
699 if (view != base) {
03a93dbb
JF
700 /* Horizontal split. */
701 view->width = base->width;
6b161b31
JF
702 view->height = SCALE_SPLIT_VIEW(base->height);
703 base->height -= view->height;
704
705 /* Make room for the title bar. */
706 view->height -= 1;
707 }
708
709 /* Make room for the title bar. */
710 base->height -= 1;
711
712 offset = 0;
713
714 foreach_view (view, i) {
b76c2afc 715 if (!view->win) {
6b161b31
JF
716 view->win = newwin(view->height, 0, offset, 0);
717 if (!view->win)
718 die("Failed to create %s view", view->name);
719
720 scrollok(view->win, TRUE);
721
722 view->title = newwin(1, 0, offset + view->height, 0);
723 if (!view->title)
724 die("Failed to create title window");
725
726 } else {
727 wresize(view->win, view->height, view->width);
728 mvwin(view->win, offset, 0);
729 mvwin(view->title, offset + view->height, 0);
730 wrefresh(view->win);
a28bcc22 731 }
a28bcc22 732
6b161b31 733 offset += view->height + 1;
b76c2afc 734 }
6b161b31 735}
b76c2afc 736
6b161b31
JF
737static void
738update_view_title(struct view *view)
739{
740 if (view == display[current_view])
741 wbkgdset(view->title, get_line_attr(LINE_TITLE_FOCUS));
742 else
743 wbkgdset(view->title, get_line_attr(LINE_TITLE_BLUR));
a28bcc22 744
6b161b31
JF
745 werase(view->title);
746 wmove(view->title, 0, 0);
b76c2afc 747
6b161b31 748 /* [main] ref: 334b506... - commit 6 of 4383 (0%) */
6908bdbd
JF
749
750 if (*view->ref)
751 wprintw(view->title, "[%s] ref: %s", view->name, view->ref);
752 else
753 wprintw(view->title, "[%s]", view->name);
754
6b161b31
JF
755 if (view->lines) {
756 char *type = view == VIEW(REQ_VIEW_MAIN) ? "commit" : "line";
757
758 wprintw(view->title, " - %s %d of %d (%d%%)",
759 type,
760 view->lineno + 1,
761 view->lines,
762 (view->lineno + 1) * 100 / view->lines);
763 }
764
765 wrefresh(view->title);
766}
78c70acd 767
2e8488b4
JF
768/*
769 * Navigation
770 */
771
4a2909a7 772/* Scrolling backend */
b801d8b2 773static void
4a2909a7 774do_scroll_view(struct view *view, int lines)
b801d8b2 775{
fd85fef1
JF
776 /* The rendering expects the new offset. */
777 view->offset += lines;
778
779 assert(0 <= view->offset && view->offset < view->lines);
780 assert(lines);
b801d8b2 781
82e78006 782 /* Redraw the whole screen if scrolling is pointless. */
4c6fabc2 783 if (view->height < ABS(lines)) {
b76c2afc
JF
784 redraw_view(view);
785
786 } else {
22f66b0a 787 int line = lines > 0 ? view->height - lines : 0;
82e78006 788 int end = line + ABS(lines);
fd85fef1
JF
789
790 wscrl(view->win, lines);
791
22f66b0a 792 for (; line < end; line++) {
6b161b31 793 if (!view->ops->draw(view, line))
fd85fef1
JF
794 break;
795 }
796 }
797
798 /* Move current line into the view. */
799 if (view->lineno < view->offset) {
800 view->lineno = view->offset;
6b161b31 801 view->ops->draw(view, 0);
fd85fef1
JF
802
803 } else if (view->lineno >= view->offset + view->height) {
804 view->lineno = view->offset + view->height - 1;
6b161b31 805 view->ops->draw(view, view->lineno - view->offset);
fd85fef1
JF
806 }
807
4c6fabc2 808 assert(view->offset <= view->lineno && view->lineno < view->lines);
fd85fef1
JF
809
810 redrawwin(view->win);
811 wrefresh(view->win);
9d3f5834 812 report("");
fd85fef1 813}
78c70acd 814
4a2909a7 815/* Scroll frontend */
fd85fef1 816static void
6b161b31 817scroll_view(struct view *view, enum request request)
fd85fef1
JF
818{
819 int lines = 1;
b801d8b2
JF
820
821 switch (request) {
4a2909a7 822 case REQ_SCROLL_PAGE_DOWN:
fd85fef1 823 lines = view->height;
4a2909a7 824 case REQ_SCROLL_LINE_DOWN:
b801d8b2 825 if (view->offset + lines > view->lines)
bde3653a 826 lines = view->lines - view->offset;
b801d8b2 827
fd85fef1 828 if (lines == 0 || view->offset + view->height >= view->lines) {
eb98559e 829 report("Cannot scroll beyond the last line");
b801d8b2
JF
830 return;
831 }
832 break;
833
4a2909a7 834 case REQ_SCROLL_PAGE_UP:
fd85fef1 835 lines = view->height;
4a2909a7 836 case REQ_SCROLL_LINE_UP:
b801d8b2
JF
837 if (lines > view->offset)
838 lines = view->offset;
839
840 if (lines == 0) {
eb98559e 841 report("Cannot scroll beyond the first line");
b801d8b2
JF
842 return;
843 }
844
fd85fef1 845 lines = -lines;
b801d8b2 846 break;
03a93dbb 847
6b161b31
JF
848 default:
849 die("request %d not handled in switch", request);
b801d8b2
JF
850 }
851
4a2909a7 852 do_scroll_view(view, lines);
fd85fef1 853}
b801d8b2 854
4a2909a7 855/* Cursor moving */
fd85fef1 856static void
6b161b31 857move_view(struct view *view, enum request request)
fd85fef1
JF
858{
859 int steps;
b801d8b2 860
fd85fef1 861 switch (request) {
4a2909a7 862 case REQ_MOVE_FIRST_LINE:
78c70acd
JF
863 steps = -view->lineno;
864 break;
865
4a2909a7 866 case REQ_MOVE_LAST_LINE:
78c70acd
JF
867 steps = view->lines - view->lineno - 1;
868 break;
869
4a2909a7 870 case REQ_MOVE_PAGE_UP:
78c70acd
JF
871 steps = view->height > view->lineno
872 ? -view->lineno : -view->height;
873 break;
874
4a2909a7 875 case REQ_MOVE_PAGE_DOWN:
78c70acd
JF
876 steps = view->lineno + view->height >= view->lines
877 ? view->lines - view->lineno - 1 : view->height;
878 break;
879
4a2909a7 880 case REQ_MOVE_UP:
fd85fef1
JF
881 steps = -1;
882 break;
b801d8b2 883
4a2909a7 884 case REQ_MOVE_DOWN:
fd85fef1
JF
885 steps = 1;
886 break;
6b161b31
JF
887
888 default:
889 die("request %d not handled in switch", request);
78c70acd 890 }
b801d8b2 891
4c6fabc2 892 if (steps <= 0 && view->lineno == 0) {
eb98559e 893 report("Cannot move beyond the first line");
78c70acd 894 return;
b801d8b2 895
6908bdbd 896 } else if (steps >= 0 && view->lineno + 1 >= view->lines) {
eb98559e 897 report("Cannot move beyond the last line");
78c70acd 898 return;
fd85fef1
JF
899 }
900
4c6fabc2 901 /* Move the current line */
fd85fef1 902 view->lineno += steps;
4c6fabc2
JF
903 assert(0 <= view->lineno && view->lineno < view->lines);
904
905 /* Repaint the old "current" line if we be scrolling */
2e8488b4
JF
906 if (ABS(steps) < view->height) {
907 int prev_lineno = view->lineno - steps - view->offset;
908
909 wmove(view->win, prev_lineno, 0);
910 wclrtoeol(view->win);
03a93dbb 911 view->ops->draw(view, prev_lineno);
2e8488b4 912 }
fd85fef1 913
4c6fabc2 914 /* Check whether the view needs to be scrolled */
fd85fef1
JF
915 if (view->lineno < view->offset ||
916 view->lineno >= view->offset + view->height) {
917 if (steps < 0 && -steps > view->offset) {
918 steps = -view->offset;
b76c2afc
JF
919
920 } else if (steps > 0) {
921 if (view->lineno == view->lines - 1 &&
922 view->lines > view->height) {
923 steps = view->lines - view->offset - 1;
924 if (steps >= view->height)
925 steps -= view->height - 1;
926 }
b801d8b2 927 }
78c70acd 928
4a2909a7 929 do_scroll_view(view, steps);
fd85fef1 930 return;
b801d8b2
JF
931 }
932
4c6fabc2 933 /* Draw the current line */
6b161b31 934 view->ops->draw(view, view->lineno - view->offset);
fd85fef1 935
b801d8b2
JF
936 redrawwin(view->win);
937 wrefresh(view->win);
9d3f5834 938 report("");
b801d8b2
JF
939}
940
b801d8b2 941
2e8488b4
JF
942/*
943 * Incremental updating
944 */
b801d8b2 945
03a93dbb 946static bool
b801d8b2
JF
947begin_update(struct view *view)
948{
2e8488b4 949 char *id = view->id;
fd85fef1 950
03a93dbb
JF
951 if (opt_cmd[0]) {
952 string_copy(view->cmd, opt_cmd);
953 opt_cmd[0] = 0;
8855ada4
JF
954 /* When running random commands, the view ref could have become
955 * invalid so clear it. */
956 view->ref[0] = 0;
03a93dbb 957 } else {
49f2b43f 958 if (snprintf(view->cmd, sizeof(view->cmd), view->cmdfmt,
03a93dbb
JF
959 id, id, id) >= sizeof(view->cmd))
960 return FALSE;
961 }
b801d8b2 962
6908bdbd
JF
963 /* Special case for the pager view. */
964 if (opt_pipe) {
965 view->pipe = opt_pipe;
966 opt_pipe = NULL;
967 } else {
968 view->pipe = popen(view->cmd, "r");
969 }
970
2e8488b4
JF
971 if (!view->pipe)
972 return FALSE;
b801d8b2 973
6b161b31 974 set_nonblocking_input(TRUE);
b801d8b2
JF
975
976 view->offset = 0;
977 view->lines = 0;
978 view->lineno = 0;
49f2b43f 979 string_copy(view->vid, id);
b801d8b2 980
2e8488b4
JF
981 if (view->line) {
982 int i;
983
984 for (i = 0; i < view->lines; i++)
985 if (view->line[i])
986 free(view->line[i]);
987
988 free(view->line);
989 view->line = NULL;
990 }
991
992 view->start_time = time(NULL);
993
b801d8b2
JF
994 return TRUE;
995}
996
997static void
998end_update(struct view *view)
999{
03a93dbb
JF
1000 if (!view->pipe)
1001 return;
6b161b31 1002 set_nonblocking_input(FALSE);
80ce96ea
JF
1003 if (view->pipe == stdin)
1004 fclose(view->pipe);
1005 else
1006 pclose(view->pipe);
2e8488b4 1007 view->pipe = NULL;
b801d8b2
JF
1008}
1009
03a93dbb 1010static bool
b801d8b2
JF
1011update_view(struct view *view)
1012{
1013 char buffer[BUFSIZ];
1014 char *line;
22f66b0a 1015 void **tmp;
82e78006
JF
1016 /* The number of lines to read. If too low it will cause too much
1017 * redrawing (and possible flickering), if too high responsiveness
1018 * will suffer. */
8855ada4 1019 unsigned long lines = view->height;
82e78006 1020 int redraw_from = -1;
b801d8b2
JF
1021
1022 if (!view->pipe)
1023 return TRUE;
1024
82e78006
JF
1025 /* Only redraw if lines are visible. */
1026 if (view->offset + view->height >= view->lines)
1027 redraw_from = view->lines - view->offset;
b801d8b2
JF
1028
1029 tmp = realloc(view->line, sizeof(*view->line) * (view->lines + lines));
1030 if (!tmp)
1031 goto alloc_error;
1032
1033 view->line = tmp;
1034
1035 while ((line = fgets(buffer, sizeof(buffer), view->pipe))) {
1036 int linelen;
1037
b801d8b2
JF
1038 linelen = strlen(line);
1039 if (linelen)
1040 line[linelen - 1] = 0;
1041
6b161b31 1042 if (!view->ops->read(view, line))
b801d8b2 1043 goto alloc_error;
fd85fef1
JF
1044
1045 if (lines-- == 1)
1046 break;
b801d8b2
JF
1047 }
1048
8855ada4
JF
1049 {
1050 int digits;
1051
1052 lines = view->lines;
1053 for (digits = 0; lines; digits++)
1054 lines /= 10;
1055
1056 /* Keep the displayed view in sync with line number scaling. */
1057 if (digits != view->digits) {
1058 view->digits = digits;
1059 redraw_from = 0;
1060 }
1061 }
1062
82e78006
JF
1063 if (redraw_from >= 0) {
1064 /* If this is an incremental update, redraw the previous line
a28bcc22
JF
1065 * since for commits some members could have changed when
1066 * loading the main view. */
82e78006
JF
1067 if (redraw_from > 0)
1068 redraw_from--;
1069
1070 /* Incrementally draw avoids flickering. */
1071 redraw_view_from(view, redraw_from);
4c6fabc2 1072 }
b801d8b2 1073
eb98559e
JF
1074 /* Update the title _after_ the redraw so that if the redraw picks up a
1075 * commit reference in view->ref it'll be available here. */
1076 update_view_title(view);
1077
b801d8b2 1078 if (ferror(view->pipe)) {
03a93dbb 1079 report("Failed to read: %s", strerror(errno));
b801d8b2
JF
1080 goto end;
1081
1082 } else if (feof(view->pipe)) {
2e8488b4
JF
1083 time_t secs = time(NULL) - view->start_time;
1084
a28bcc22
JF
1085 if (view == VIEW(REQ_VIEW_HELP)) {
1086 report("%s", HELP);
2e8488b4
JF
1087 goto end;
1088 }
1089
1090 report("Loaded %d lines in %ld second%s", view->lines, secs,
1091 secs == 1 ? "" : "s");
b801d8b2
JF
1092 goto end;
1093 }
1094
1095 return TRUE;
1096
1097alloc_error:
2e8488b4 1098 report("Allocation failure");
b801d8b2
JF
1099
1100end:
1101 end_update(view);
1102 return FALSE;
1103}
1104
49f2b43f
JF
1105enum open_flags {
1106 OPEN_DEFAULT = 0, /* Use default view switching. */
1107 OPEN_SPLIT = 1, /* Split current view. */
1108 OPEN_BACKGROUNDED = 2, /* Backgrounded. */
1109 OPEN_RELOAD = 4, /* Reload view even if it is the current. */
1110};
1111
6b161b31 1112static void
49f2b43f 1113open_view(struct view *prev, enum request request, enum open_flags flags)
b801d8b2 1114{
49f2b43f
JF
1115 bool backgrounded = !!(flags & OPEN_BACKGROUNDED);
1116 bool split = !!(flags & OPEN_SPLIT);
1117 bool reload = !!(flags & OPEN_RELOAD);
a28bcc22 1118 struct view *view = VIEW(request);
b801d8b2 1119 struct view *displayed;
6b161b31
JF
1120 int nviews;
1121
03a93dbb 1122 /* Cycle between displayed views and count the views. */
6b161b31 1123 foreach_view (displayed, nviews) {
03a93dbb
JF
1124 if (prev != view &&
1125 view == displayed &&
8855ada4 1126 !strcmp(view->vid, prev->vid)) {
6b161b31
JF
1127 current_view = nviews;
1128 /* Blur out the title of the previous view. */
1129 update_view_title(prev);
1130 report("Switching to %s view", view->name);
1131 return;
a28bcc22 1132 }
6b161b31 1133 }
b801d8b2 1134
49f2b43f 1135 if (view == prev && nviews == 1 && !reload) {
6b161b31
JF
1136 report("Already in %s view", view->name);
1137 return;
1138 }
b801d8b2 1139
8855ada4 1140 if ((reload || strcmp(view->vid, view->id)) &&
03a93dbb 1141 !begin_update(view)) {
6b161b31
JF
1142 report("Failed to load %s view", view->name);
1143 return;
1144 }
a28bcc22 1145
6b161b31
JF
1146 if (split) {
1147 display[current_view + 1] = view;
1148 if (!backgrounded)
a28bcc22 1149 current_view++;
6b161b31
JF
1150 } else {
1151 /* Maximize the current view. */
1152 memset(display, 0, sizeof(display));
1153 current_view = 0;
1154 display[current_view] = view;
a28bcc22 1155 }
b801d8b2 1156
6b161b31 1157 resize_display();
b801d8b2 1158
a8891802 1159 if (split && prev->lineno - prev->offset >= prev->height) {
03a93dbb 1160 /* Take the title line into account. */
eb98559e 1161 int lines = prev->lineno - prev->offset - prev->height + 1;
03a93dbb
JF
1162
1163 /* Scroll the view that was split if the current line is
1164 * outside the new limited view. */
1165 do_scroll_view(prev, lines);
1166 }
1167
6b161b31
JF
1168 if (prev && view != prev) {
1169 /* "Blur" the previous view. */
1170 update_view_title(prev);
1171
1172 /* Continue loading split views in the background. */
03a93dbb 1173 if (!split)
6b161b31 1174 end_update(prev);
b801d8b2
JF
1175 }
1176
03a93dbb
JF
1177 if (view->pipe) {
1178 /* Clear the old view and let the incremental updating refill
1179 * the screen. */
1180 wclear(view->win);
1181 report("Loading...");
1182 } else {
1183 redraw_view(view);
1184 report("");
1185 }
b801d8b2
JF
1186}
1187
1188
6b161b31
JF
1189/*
1190 * User request switch noodle
1191 */
1192
b801d8b2 1193static int
6b161b31 1194view_driver(struct view *view, enum request request)
b801d8b2 1195{
b801d8b2
JF
1196 int i;
1197
1198 switch (request) {
4a2909a7
JF
1199 case REQ_MOVE_UP:
1200 case REQ_MOVE_DOWN:
1201 case REQ_MOVE_PAGE_UP:
1202 case REQ_MOVE_PAGE_DOWN:
1203 case REQ_MOVE_FIRST_LINE:
1204 case REQ_MOVE_LAST_LINE:
a28bcc22 1205 move_view(view, request);
fd85fef1
JF
1206 break;
1207
4a2909a7
JF
1208 case REQ_SCROLL_LINE_DOWN:
1209 case REQ_SCROLL_LINE_UP:
1210 case REQ_SCROLL_PAGE_DOWN:
1211 case REQ_SCROLL_PAGE_UP:
a28bcc22 1212 scroll_view(view, request);
b801d8b2
JF
1213 break;
1214
4a2909a7 1215 case REQ_VIEW_MAIN:
4a2909a7 1216 case REQ_VIEW_DIFF:
2e8488b4
JF
1217 case REQ_VIEW_LOG:
1218 case REQ_VIEW_HELP:
6908bdbd 1219 case REQ_VIEW_PAGER:
49f2b43f 1220 open_view(view, request, OPEN_DEFAULT);
b801d8b2
JF
1221 break;
1222
6b161b31 1223 case REQ_ENTER:
6908bdbd
JF
1224 if (!view->lines) {
1225 report("Nothing to enter");
1226 break;
1227 }
6b161b31
JF
1228 return view->ops->enter(view);
1229
03a93dbb
JF
1230 case REQ_VIEW_NEXT:
1231 {
1232 int nviews = display[1] ? 2 : 1;
1233 int next_view = (current_view + 1) % nviews;
1234
1235 if (next_view == current_view) {
1236 report("Only one view is displayed");
1237 break;
1238 }
1239
1240 current_view = next_view;
1241 /* Blur out the title of the previous view. */
1242 update_view_title(view);
1243 report("Switching to %s view", display[current_view]->name);
1244 break;
1245 }
4a2909a7 1246 case REQ_TOGGLE_LINE_NUMBERS:
b76c2afc 1247 opt_line_number = !opt_line_number;
a28bcc22 1248 redraw_view(view);
b801d8b2
JF
1249 break;
1250
03a93dbb 1251 case REQ_PROMPT:
8855ada4 1252 /* Always reload^Wrerun commands from the prompt. */
49f2b43f 1253 open_view(view, opt_request, OPEN_RELOAD);
03a93dbb
JF
1254 break;
1255
4a2909a7 1256 case REQ_STOP_LOADING:
03a93dbb 1257 foreach_view (view, i) {
2e8488b4 1258 if (view->pipe)
03a93dbb
JF
1259 report("Stopped loaded of %s view", view->name),
1260 end_update(view);
1261 }
b801d8b2
JF
1262 break;
1263
4a2909a7 1264 case REQ_SHOW_VERSION:
2e8488b4 1265 report("Version: %s", VERSION);
b801d8b2
JF
1266 return TRUE;
1267
4a2909a7 1268 case REQ_SCREEN_REDRAW:
eb98559e
JF
1269 foreach_view (view, i) {
1270 redraw_view(view);
1271 update_view_title(view);
1272 }
4a2909a7
JF
1273 break;
1274
1275 case REQ_SCREEN_UPDATE:
b801d8b2
JF
1276 doupdate();
1277 return TRUE;
1278
1279 case REQ_QUIT:
1280 return FALSE;
1281
1282 default:
2e8488b4 1283 /* An unknown key will show most commonly used commands. */
a28bcc22 1284 report("%s", HELP);
b801d8b2
JF
1285 return TRUE;
1286 }
1287
1288 return TRUE;
1289}
1290
1291
1292/*
6b161b31 1293 * View backend handlers
b801d8b2
JF
1294 */
1295
6b161b31 1296static bool
22f66b0a 1297pager_draw(struct view *view, unsigned int lineno)
b801d8b2 1298{
78c70acd 1299 enum line_type type;
b801d8b2 1300 char *line;
4c6fabc2 1301 int linelen;
78c70acd 1302 int attr;
b801d8b2 1303
fd85fef1
JF
1304 if (view->offset + lineno >= view->lines)
1305 return FALSE;
1306
b801d8b2 1307 line = view->line[view->offset + lineno];
78c70acd 1308 type = get_line_type(line);
b801d8b2 1309
fd85fef1 1310 if (view->offset + lineno == view->lineno) {
8855ada4 1311 if (type == LINE_COMMIT) {
03a93dbb
JF
1312 string_copy(view->ref, line + 7);
1313 string_copy(ref_commit, view->ref);
1314 }
8855ada4 1315
78c70acd 1316 type = LINE_CURSOR;
fd85fef1
JF
1317 }
1318
78c70acd 1319 attr = get_line_attr(type);
b801d8b2 1320 wattrset(view->win, attr);
b76c2afc 1321
4c6fabc2
JF
1322 linelen = strlen(line);
1323 linelen = MIN(linelen, view->width);
1324
b76c2afc 1325 if (opt_line_number) {
8855ada4
JF
1326 static char indent[] = " ";
1327 unsigned long real_lineno = view->offset + lineno + 1;
a28bcc22 1328 int col = 0;
82e78006 1329
2e8488b4 1330 if (real_lineno == 1 || (real_lineno % opt_num_interval) == 0)
8855ada4
JF
1331 mvwprintw(view->win, lineno, 0, "%.*d", view->digits, real_lineno);
1332
1333 else if (view->digits < sizeof(indent))
1334 mvwaddnstr(view->win, lineno, 0, indent, view->digits);
1335
1336 waddstr(view->win, ": ");
4c6fabc2 1337
b76c2afc
JF
1338 while (line) {
1339 if (*line == '\t') {
82e78006
JF
1340 waddnstr(view->win, " ", 8 - (col % 8));
1341 col += 8 - (col % 8);
b76c2afc 1342 line++;
82e78006 1343
b76c2afc
JF
1344 } else {
1345 char *tab = strchr(line, '\t');
1346
1347 if (tab)
1348 waddnstr(view->win, line, tab - line);
1349 else
1350 waddstr(view->win, line);
82e78006 1351 col += tab - line;
b76c2afc
JF
1352 line = tab;
1353 }
1354 }
1355 waddstr(view->win, line);
1356
1357 } else {
2e8488b4
JF
1358#if 0
1359 /* NOTE: Code for only highlighting the text on the cursor line.
1360 * Kept since I've not yet decided whether to highlight the
1361 * entire line or not. --fonseca */
b76c2afc
JF
1362 /* No empty lines makes cursor drawing and clearing implicit. */
1363 if (!*line)
4c6fabc2 1364 line = " ", linelen = 1;
2e8488b4 1365#endif
4c6fabc2 1366 mvwaddnstr(view->win, lineno, 0, line, linelen);
b76c2afc 1367 }
b801d8b2 1368
2e8488b4
JF
1369 /* Paint the rest of the line if it's the cursor line. */
1370 if (type == LINE_CURSOR)
1371 wchgat(view->win, -1, 0, type, NULL);
1372
b801d8b2
JF
1373 return TRUE;
1374}
1375
6b161b31 1376static bool
22f66b0a
JF
1377pager_read(struct view *view, char *line)
1378{
8855ada4
JF
1379 /* For some reason the piped man page has many empty lines
1380 * so skip successive emptys lines to work around it. */
1381 if (view == VIEW(REQ_VIEW_HELP) &&
1382 !*line &&
1383 view->lines &&
1384 !*((char *) view->line[view->lines - 1]))
1385 return TRUE;
1386
22f66b0a
JF
1387 view->line[view->lines] = strdup(line);
1388 if (!view->line[view->lines])
1389 return FALSE;
1390
1391 view->lines++;
1392 return TRUE;
1393}
1394
6b161b31
JF
1395static bool
1396pager_enter(struct view *view)
1397{
1398 char *line = view->line[view->lineno];
1399
1400 if (get_line_type(line) == LINE_COMMIT) {
49f2b43f 1401 open_view(view, REQ_VIEW_DIFF, OPEN_DEFAULT);
6b161b31
JF
1402 }
1403
1404 return TRUE;
1405}
1406
6b161b31
JF
1407static struct view_ops pager_ops = {
1408 pager_draw,
1409 pager_read,
1410 pager_enter,
1411};
1412
80ce96ea 1413
6b161b31 1414static bool
22f66b0a
JF
1415main_draw(struct view *view, unsigned int lineno)
1416{
2e8488b4 1417 char buf[DATE_COLS + 1];
22f66b0a 1418 struct commit *commit;
78c70acd 1419 enum line_type type;
b76c2afc
JF
1420 int cols = 0;
1421 size_t timelen;
22f66b0a
JF
1422
1423 if (view->offset + lineno >= view->lines)
1424 return FALSE;
1425
1426 commit = view->line[view->offset + lineno];
4c6fabc2
JF
1427 if (!*commit->author)
1428 return FALSE;
22f66b0a 1429
22f66b0a 1430 if (view->offset + lineno == view->lineno) {
03a93dbb 1431 string_copy(view->ref, commit->id);
49f2b43f 1432 string_copy(ref_commit, view->ref);
78c70acd
JF
1433 type = LINE_CURSOR;
1434 } else {
b76c2afc
JF
1435 type = LINE_MAIN_COMMIT;
1436 }
1437
1438 wmove(view->win, lineno, cols);
1439 wattrset(view->win, get_line_attr(LINE_MAIN_DATE));
1440
4c6fabc2 1441 timelen = strftime(buf, sizeof(buf), DATE_FORMAT, &commit->time);
b76c2afc 1442 waddnstr(view->win, buf, timelen);
4c6fabc2 1443 waddstr(view->win, " ");
b76c2afc 1444
4c6fabc2 1445 cols += DATE_COLS;
b76c2afc
JF
1446 wmove(view->win, lineno, cols);
1447 wattrset(view->win, get_line_attr(LINE_MAIN_AUTHOR));
1448
1449 if (strlen(commit->author) > 19) {
1450 waddnstr(view->win, commit->author, 18);
1451 wattrset(view->win, get_line_attr(LINE_MAIN_DELIM));
1452 waddch(view->win, '~');
1453 } else {
1454 waddstr(view->win, commit->author);
22f66b0a
JF
1455 }
1456
b76c2afc
JF
1457 cols += 20;
1458 wattrset(view->win, A_NORMAL);
1459 mvwaddch(view->win, lineno, cols, ACS_LTEE);
78c70acd 1460 wattrset(view->win, get_line_attr(type));
b76c2afc 1461 mvwaddstr(view->win, lineno, cols + 2, commit->title);
22f66b0a
JF
1462 wattrset(view->win, A_NORMAL);
1463
1464 return TRUE;
1465}
1466
4c6fabc2 1467/* Reads git log --pretty=raw output and parses it into the commit struct. */
6b161b31 1468static bool
22f66b0a
JF
1469main_read(struct view *view, char *line)
1470{
78c70acd
JF
1471 enum line_type type = get_line_type(line);
1472 struct commit *commit;
22f66b0a 1473
78c70acd
JF
1474 switch (type) {
1475 case LINE_COMMIT:
22f66b0a
JF
1476 commit = calloc(1, sizeof(struct commit));
1477 if (!commit)
1478 return FALSE;
1479
4c6fabc2 1480 line += STRING_SIZE("commit ");
b76c2afc 1481
22f66b0a 1482 view->line[view->lines++] = commit;
82e78006 1483 string_copy(commit->id, line);
78c70acd 1484 break;
22f66b0a 1485
8855ada4 1486 case LINE_AUTHOR:
b76c2afc 1487 {
4c6fabc2 1488 char *ident = line + STRING_SIZE("author ");
b76c2afc
JF
1489 char *end = strchr(ident, '<');
1490
1491 if (end) {
1492 for (; end > ident && isspace(end[-1]); end--) ;
1493 *end = 0;
1494 }
1495
1496 commit = view->line[view->lines - 1];
82e78006 1497 string_copy(commit->author, ident);
b76c2afc 1498
4c6fabc2 1499 /* Parse epoch and timezone */
b76c2afc
JF
1500 if (end) {
1501 char *secs = strchr(end + 1, '>');
1502 char *zone;
1503 time_t time;
1504
1505 if (!secs || secs[1] != ' ')
1506 break;
1507
1508 secs += 2;
1509 time = (time_t) atol(secs);
1510 zone = strchr(secs, ' ');
4c6fabc2 1511 if (zone && strlen(zone) == STRING_SIZE(" +0700")) {
b76c2afc
JF
1512 long tz;
1513
1514 zone++;
1515 tz = ('0' - zone[1]) * 60 * 60 * 10;
1516 tz += ('0' - zone[2]) * 60 * 60;
1517 tz += ('0' - zone[3]) * 60;
1518 tz += ('0' - zone[4]) * 60;
1519
1520 if (zone[0] == '-')
1521 tz = -tz;
1522
1523 time -= tz;
1524 }
1525 gmtime_r(&time, &commit->time);
1526 }
1527 break;
1528 }
78c70acd 1529 default:
2e8488b4
JF
1530 /* We should only ever end up here if there has already been a
1531 * commit line, however, be safe. */
1532 if (view->lines == 0)
1533 break;
1534
1535 /* Fill in the commit title if it has not already been set. */
78c70acd 1536 commit = view->line[view->lines - 1];
2e8488b4
JF
1537 if (commit->title[0])
1538 break;
1539
1540 /* Require titles to start with a non-space character at the
1541 * offset used by git log. */
eb98559e
JF
1542 /* FIXME: More gracefull handling of titles; append "..." to
1543 * shortened titles, etc. */
2e8488b4 1544 if (strncmp(line, " ", 4) ||
eb98559e 1545 isspace(line[4]))
82e78006
JF
1546 break;
1547
1548 string_copy(commit->title, line + 4);
22f66b0a
JF
1549 }
1550
1551 return TRUE;
1552}
1553
6b161b31
JF
1554static bool
1555main_enter(struct view *view)
b801d8b2 1556{
49f2b43f 1557 open_view(view, REQ_VIEW_DIFF, OPEN_SPLIT | OPEN_BACKGROUNDED);
6b161b31 1558 return TRUE;
b801d8b2
JF
1559}
1560
6b161b31
JF
1561static struct view_ops main_ops = {
1562 main_draw,
1563 main_read,
1564 main_enter,
1565};
2e8488b4 1566
6b161b31
JF
1567/*
1568 * Status management
1569 */
2e8488b4 1570
8855ada4
JF
1571/* Whether or not the curses interface has been initialized. */
1572bool cursed = FALSE;
1573
6b161b31
JF
1574/* The status window is used for polling keystrokes. */
1575static WINDOW *status_win;
4a2909a7 1576
2e8488b4 1577/* Update status and title window. */
4a2909a7
JF
1578static void
1579report(const char *msg, ...)
1580{
1581 va_list args;
b76c2afc 1582
b801d8b2
JF
1583 va_start(args, msg);
1584
2e8488b4
JF
1585 /* Update the title window first, so the cursor ends up in the status
1586 * window. */
6b161b31 1587 update_view_title(display[current_view]);
4a2909a7 1588
b801d8b2
JF
1589 werase(status_win);
1590 wmove(status_win, 0, 0);
b801d8b2
JF
1591 vwprintw(status_win, msg, args);
1592 wrefresh(status_win);
1593
1594 va_end(args);
b801d8b2
JF
1595}
1596
6b161b31
JF
1597/* Controls when nodelay should be in effect when polling user input. */
1598static void
1599set_nonblocking_input(int loading)
b801d8b2 1600{
6b161b31
JF
1601 /* The number of loading views. */
1602 static unsigned int nloading;
b801d8b2 1603
6b161b31
JF
1604 if (loading == TRUE) {
1605 if (nloading++ == 0)
1606 nodelay(status_win, TRUE);
1607 return;
82e78006 1608 }
b76c2afc 1609
6b161b31
JF
1610 if (nloading-- == 1)
1611 nodelay(status_win, FALSE);
1612}
1613
1614static void
1615init_display(void)
1616{
1617 int x, y;
b76c2afc 1618
6908bdbd
JF
1619 /* Initialize the curses library */
1620 if (isatty(STDIN_FILENO)) {
8855ada4 1621 cursed = !!initscr();
6908bdbd
JF
1622 } else {
1623 /* Leave stdin and stdout alone when acting as a pager. */
1624 FILE *io = fopen("/dev/tty", "r+");
1625
8855ada4 1626 cursed = !!newterm(NULL, io, io);
6908bdbd
JF
1627 }
1628
8855ada4
JF
1629 if (!cursed)
1630 die("Failed to initialize curses");
1631
2e8488b4
JF
1632 nonl(); /* Tell curses not to do NL->CR/NL on output */
1633 cbreak(); /* Take input chars one at a time, no wait for \n */
1634 noecho(); /* Don't echo input */
b801d8b2 1635 leaveok(stdscr, TRUE);
b801d8b2
JF
1636
1637 if (has_colors())
1638 init_colors();
1639
1640 getmaxyx(stdscr, y, x);
1641 status_win = newwin(1, 0, y - 1, 0);
1642 if (!status_win)
1643 die("Failed to create status window");
1644
1645 /* Enable keyboard mapping */
1646 keypad(status_win, TRUE);
78c70acd 1647 wbkgdset(status_win, get_line_attr(LINE_STATUS));
6b161b31
JF
1648}
1649
1650/*
1651 * Main
1652 */
1653
1654static void
1655quit(int sig)
1656{
8855ada4
JF
1657 /* XXX: Restore tty modes and let the OS cleanup the rest! */
1658 if (cursed)
1659 endwin();
6b161b31
JF
1660 exit(0);
1661}
1662
1663static void die(const char *err, ...)
1664{
1665 va_list args;
1666
1667 endwin();
1668
1669 va_start(args, err);
1670 fputs("tig: ", stderr);
1671 vfprintf(stderr, err, args);
1672 fputs("\n", stderr);
1673 va_end(args);
1674
1675 exit(1);
1676}
1677
1678int
1679main(int argc, char *argv[])
1680{
1681 enum request request;
6b161b31
JF
1682
1683 signal(SIGINT, quit);
1684
8855ada4 1685 if (!parse_options(argc, argv))
6b161b31
JF
1686 return 0;
1687
6b161b31
JF
1688 request = opt_request;
1689
1690 init_display();
b801d8b2
JF
1691
1692 while (view_driver(display[current_view], request)) {
1693 struct view *view;
6b161b31 1694 int key;
b801d8b2
JF
1695 int i;
1696
6b161b31
JF
1697 foreach_view (view, i)
1698 update_view(view);
b801d8b2
JF
1699
1700 /* Refresh, accept single keystroke of input */
6b161b31
JF
1701 key = wgetch(status_win);
1702 request = get_request(key);
03a93dbb
JF
1703
1704 if (request == REQ_PROMPT) {
6908bdbd
JF
1705 report(":");
1706 /* Temporarily switch to line-oriented and echoed
1707 * input. */
03a93dbb
JF
1708 nocbreak();
1709 echo();
49f2b43f
JF
1710
1711 if (wgetnstr(status_win, opt_cmd + 4, sizeof(opt_cmd) - 4) == OK) {
1712 memcpy(opt_cmd, "git ", 4);
1713 opt_request = REQ_VIEW_PAGER;
1714 } else {
1715 request = ERR;
1716 }
1717
6908bdbd
JF
1718 noecho();
1719 cbreak();
03a93dbb 1720 }
b801d8b2
JF
1721 }
1722
1723 quit(0);
1724
1725 return 0;
1726}
1727
1728/**
4c6fabc2
JF
1729 * TODO
1730 * ----
1731 * Features that should be explored.
1732 *
03a93dbb
JF
1733 * - Terminal resizing support. I am yet to figure out whether catching
1734 * SIGWINCH is preferred over using ncurses' built-in support for resizing.
4c6fabc2
JF
1735 *
1736 * - Locale support.
1737 *
b801d8b2
JF
1738 * COPYRIGHT
1739 * ---------
4a2909a7 1740 * Copyright (c) Jonas Fonseca <fonseca@diku.dk>, 2006
b801d8b2
JF
1741 *
1742 * This program is free software; you can redistribute it and/or modify
1743 * it under the terms of the GNU General Public License as published by
1744 * the Free Software Foundation; either version 2 of the License, or
1745 * (at your option) any later version.
1746 *
1747 * SEE ALSO
1748 * --------
4c6fabc2 1749 * [verse]
b801d8b2
JF
1750 * link:http://www.kernel.org/pub/software/scm/git/docs/[git(7)],
1751 * link:http://www.kernel.org/pub/software/scm/cogito/docs/[cogito(7)]
4c6fabc2
JF
1752 * gitk(1): git repository browser written using tcl/tk,
1753 * gitview(1): git repository browser written using python/gtk.
b801d8b2 1754 **/