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