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