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