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