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