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