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