Improve title updating and remove flickering
[tig] / tig.c
diff --git a/tig.c b/tig.c
index 53d4770..52abe0f 100644 (file)
--- a/tig.c
+++ b/tig.c
@@ -113,11 +113,11 @@ enum request {
        REQ_TOGGLE_LINE_NUMBERS,
        REQ_VIEW_NEXT,
        REQ_VIEW_CLOSE,
+       REQ_NEXT,
+       REQ_PREVIOUS,
 
        REQ_MOVE_UP,
-       REQ_MOVE_UP_ENTER,
        REQ_MOVE_DOWN,
-       REQ_MOVE_DOWN_ENTER,
        REQ_MOVE_PAGE_UP,
        REQ_MOVE_PAGE_DOWN,
        REQ_MOVE_FIRST_LINE,
@@ -646,6 +646,7 @@ static unsigned int current_view;
 #define foreach_view(view, i) \
        for (i = 0; i < ARRAY_SIZE(display) && (view = display[i]); i++)
 
+#define displayed_views()      (display[1] != NULL ? 2 : 1)
 
 /**
  * Current head and commit ID
@@ -806,14 +807,27 @@ update_view_title(struct view *view)
        else
                wprintw(view->title, "[%s]", view->name);
 
-       if (view->lines) {
+       if (view->lines || view->pipe) {
+               unsigned int lines = view->lines
+                                  ? (view->lineno + 1) * 100 / view->lines
+                                  : 0;
+
                wprintw(view->title, " - %s %d of %d (%d%%)",
                        view->ops->type,
                        view->lineno + 1,
                        view->lines,
-                       (view->lineno + 1) * 100 / view->lines);
+                       lines);
+       }
+
+       if (view->pipe) {
+               time_t secs = time(NULL) - view->start_time;
+
+               /* Three git seconds are a long time ... */
+               if (secs > 2)
+                       wprintw(view->title, " %lds", secs);
        }
 
+
        wrefresh(view->title);
 }
 
@@ -847,11 +861,8 @@ resize_display(void)
        offset = 0;
 
        foreach_view (view, i) {
-               /* Keep the height of all view->win windows one larger than is
-                * required so that the cursor can wrap-around on the last line
-                * without scrolling the window. */
                if (!view->win) {
-                       view->win = newwin(view->height + 1, 0, offset, 0);
+                       view->win = newwin(view->height, 0, offset, 0);
                        if (!view->win)
                                die("Failed to create %s view", view->name);
 
@@ -862,7 +873,7 @@ resize_display(void)
                                die("Failed to create title window");
 
                } else {
-                       wresize(view->win, view->height + 1, view->width);
+                       wresize(view->win, view->height, view->width);
                        mvwin(view->win,   offset, 0);
                        mvwin(view->title, offset + view->height, 0);
                        wrefresh(view->win);
@@ -891,7 +902,7 @@ redraw_display(void)
 
 /* Scrolling backend */
 static void
-do_scroll_view(struct view *view, int lines)
+do_scroll_view(struct view *view, int lines, bool redraw)
 {
        /* The rendering expects the new offset. */
        view->offset += lines;
@@ -933,6 +944,9 @@ do_scroll_view(struct view *view, int lines)
 
        assert(view->offset <= view->lineno && view->lineno < view->lines);
 
+       if (!redraw)
+               return;
+
        redrawwin(view->win);
        wrefresh(view->win);
        report("");
@@ -975,12 +989,12 @@ scroll_view(struct view *view, enum request request)
                die("request %d not handled in switch", request);
        }
 
-       do_scroll_view(view, lines);
+       do_scroll_view(view, lines, TRUE);
 }
 
 /* Cursor moving */
 static void
-move_view(struct view *view, enum request request)
+move_view(struct view *view, enum request request, bool redraw)
 {
        int steps;
 
@@ -1004,12 +1018,10 @@ move_view(struct view *view, enum request request)
                break;
 
        case REQ_MOVE_UP:
-       case REQ_MOVE_UP_ENTER:
                steps = -1;
                break;
 
        case REQ_MOVE_DOWN:
-       case REQ_MOVE_DOWN_ENTER:
                steps = 1;
                break;
 
@@ -1054,13 +1066,16 @@ move_view(struct view *view, enum request request)
                        }
                }
 
-               do_scroll_view(view, steps);
+               do_scroll_view(view, steps, redraw);
                return;
        }
 
        /* Draw the current line */
        view->ops->draw(view, view->lineno - view->offset);
 
+       if (!redraw)
+               return;
+
        redrawwin(view->win);
        wrefresh(view->win);
        report("");
@@ -1071,11 +1086,27 @@ move_view(struct view *view, enum request request)
  * Incremental updating
  */
 
+static void
+end_update(struct view *view)
+{
+       if (!view->pipe)
+               return;
+       set_nonblocking_input(FALSE);
+       if (view->pipe == stdin)
+               fclose(view->pipe);
+       else
+               pclose(view->pipe);
+       view->pipe = NULL;
+}
+
 static bool
 begin_update(struct view *view)
 {
        const char *id = view->id;
 
+       if (view->pipe)
+               end_update(view);
+
        if (opt_cmd[0]) {
                string_copy(view->cmd, opt_cmd);
                opt_cmd[0] = 0;
@@ -1124,19 +1155,6 @@ begin_update(struct view *view)
        return TRUE;
 }
 
-static void
-end_update(struct view *view)
-{
-       if (!view->pipe)
-               return;
-       set_nonblocking_input(FALSE);
-       if (view->pipe == stdin)
-               fclose(view->pipe);
-       else
-               pclose(view->pipe);
-       view->pipe = NULL;
-}
-
 static bool
 update_view(struct view *view)
 {
@@ -1209,8 +1227,6 @@ update_view(struct view *view)
                goto end;
 
        } else if (feof(view->pipe)) {
-               time_t secs = time(NULL) - view->start_time;
-
                if (view == VIEW(REQ_VIEW_HELP)) {
                        const char *msg = TIG_HELP;
 
@@ -1225,8 +1241,7 @@ update_view(struct view *view)
                        goto end;
                }
 
-               report("Loaded %d lines in %ld second%s", view->lines, secs,
-                      secs == 1 ? "" : "s");
+               report("");
                goto end;
        }
 
@@ -1254,7 +1269,8 @@ open_view(struct view *prev, enum request request, enum open_flags flags)
        bool split = !!(flags & OPEN_SPLIT);
        bool reload = !!(flags & OPEN_RELOAD);
        struct view *view = VIEW(request);
-       int nviews = display[1] ? 2 : 1;
+       int nviews = displayed_views();
+       struct view *base_view = display[0];
 
        if (view == prev && nviews == 1 && !reload) {
                report("Already in %s view", view->name);
@@ -1278,7 +1294,11 @@ open_view(struct view *prev, enum request request, enum open_flags flags)
                display[current_view] = view;
        }
 
-       resize_display();
+       /* Resize the view when switching between split- and full-screen,
+        * or when switching between two different full-screen views. */
+       if (nviews != displayed_views() ||
+           (nviews == 1 && base_view != display[0]))
+               resize_display();
 
        if (split && prev->lineno - prev->offset >= prev->height) {
                /* Take the title line into account. */
@@ -1286,17 +1306,17 @@ open_view(struct view *prev, enum request request, enum open_flags flags)
 
                /* Scroll the view that was split if the current line is
                 * outside the new limited view. */
-               do_scroll_view(prev, lines);
+               do_scroll_view(prev, lines, TRUE);
        }
 
        if (prev && view != prev) {
-               /* "Blur" the previous view. */
-               if (!backgrounded)
-                       update_view_title(prev);
-
                /* Continue loading split views in the background. */
                if (!split)
                        end_update(prev);
+               else if (!backgrounded)
+                       /* "Blur" the previous view. */
+                       update_view_title(prev);
+
                view->parent = prev;
        }
 
@@ -1304,7 +1324,7 @@ open_view(struct view *prev, enum request request, enum open_flags flags)
                /* Clear the old view and let the incremental updating refill
                 * the screen. */
                wclear(view->win);
-               report("Loading...");
+               report("");
        } else {
                redraw_view(view);
                if (view == VIEW(REQ_VIEW_HELP))
@@ -1336,7 +1356,7 @@ view_driver(struct view *view, enum request request)
        case REQ_MOVE_PAGE_DOWN:
        case REQ_MOVE_FIRST_LINE:
        case REQ_MOVE_LAST_LINE:
-               move_view(view, request);
+               move_view(view, request, TRUE);
                break;
 
        case REQ_SCROLL_LINE_DOWN:
@@ -1354,9 +1374,22 @@ view_driver(struct view *view, enum request request)
                open_view(view, request, OPEN_DEFAULT);
                break;
 
-       case REQ_MOVE_UP_ENTER:
-       case REQ_MOVE_DOWN_ENTER:
-               move_view(view, request);
+       case REQ_NEXT:
+       case REQ_PREVIOUS:
+               request = request == REQ_NEXT ? REQ_MOVE_DOWN : REQ_MOVE_UP;
+
+               if (view == VIEW(REQ_VIEW_DIFF) &&
+                   view->parent == VIEW(REQ_VIEW_MAIN)) {
+                       bool redraw = display[1] == view;
+
+                       view = view->parent;
+                       move_view(view, request, redraw);
+                       if (redraw)
+                               update_view_title(view);
+               } else {
+                       move_view(view, request, TRUE);
+                       break;
+               }
                /* Fall-through */
 
        case REQ_ENTER:
@@ -1368,7 +1401,7 @@ view_driver(struct view *view, enum request request)
 
        case REQ_VIEW_NEXT:
        {
-               int nviews = display[1] ? 2 : 1;
+               int nviews = displayed_views();
                int next_view = (current_view + 1) % nviews;
 
                if (next_view == current_view) {
@@ -1778,7 +1811,9 @@ main_read(struct view *view, char *line)
 static bool
 main_enter(struct view *view)
 {
-       open_view(view, REQ_VIEW_DIFF, OPEN_SPLIT);
+       enum open_flags flags = display[0] == view ? OPEN_SPLIT : OPEN_DEFAULT;
+
+       open_view(view, REQ_VIEW_DIFF, flags);
        return TRUE;
 }
 
@@ -1815,6 +1850,16 @@ static struct keymap keymap[] = {
         *      Switch to pager view.
         * h::
         *      Show man page.
+        **/
+       { 'm',          REQ_VIEW_MAIN },
+       { 'd',          REQ_VIEW_DIFF },
+       { 'l',          REQ_VIEW_LOG },
+       { 'p',          REQ_VIEW_PAGER },
+       { 'h',          REQ_VIEW_HELP },
+
+       /**
+        * View manipulation
+        * ~~~~~~~~~~~~~~~~~
         * q::
         *      Close view, if multiple views are open it will jump back to the
         *      previous view in the view stack. If it is the last open view it
@@ -1826,30 +1871,28 @@ static struct keymap keymap[] = {
         *      pressing Enter will simply scroll the view one line down.
         * Tab::
         *      Switch to next view.
+        * Up::
+        *      This key is "context sensitive" and will move the cursor one
+        *      line up. However, uf you opened a diff view from the main view
+        *      (split- or full-screen) it will change the cursor to point to
+        *      the previous commit in the main view and update the diff view
+        *      to display it.
+        * Down::
+        *      Similar to 'Up' but will move down.
         **/
-       { 'm',          REQ_VIEW_MAIN },
-       { 'd',          REQ_VIEW_DIFF },
-       { 'l',          REQ_VIEW_LOG },
-       { 'p',          REQ_VIEW_PAGER },
-       { 'h',          REQ_VIEW_HELP },
-
        { 'q',          REQ_VIEW_CLOSE },
        { KEY_TAB,      REQ_VIEW_NEXT },
        { KEY_RETURN,   REQ_ENTER },
+       { KEY_UP,       REQ_PREVIOUS },
+       { KEY_DOWN,     REQ_NEXT },
 
        /**
         * Cursor navigation
         * ~~~~~~~~~~~~~~~~~
-        * Up::
+        * j::
         *      Move cursor one line up.
-        * Down::
-        *      Move cursor one line down.
         * k::
-        *      Move cursor one line up and enter. When used in the main view
-        *      this will always show the diff of the current commit in the
-        *      split diff view.
-        * j::
-        *      Move cursor one line down and enter.
+        *      Move cursor one line down.
         * PgUp::
         * b::
         * -::
@@ -1862,10 +1905,8 @@ static struct keymap keymap[] = {
         * End::
         *      Jump to last line.
         **/
-       { KEY_UP,       REQ_MOVE_UP },
-       { KEY_DOWN,     REQ_MOVE_DOWN },
-       { 'k',          REQ_MOVE_UP_ENTER },
-       { 'j',          REQ_MOVE_DOWN_ENTER },
+       { 'k',          REQ_MOVE_UP },
+       { 'j',          REQ_MOVE_DOWN },
        { KEY_HOME,     REQ_MOVE_FIRST_LINE },
        { KEY_END,      REQ_MOVE_LAST_LINE },
        { KEY_NPAGE,    REQ_MOVE_PAGE_DOWN },
@@ -1959,7 +2000,7 @@ unicode_width(unsigned long c)
            || c == 0x2329
            || c == 0x232a
            || (c >= 0x2e80  && c <= 0xa4cf && c != 0x303f)
-                                               /* CJK ... Yi */
+                                               /* CJK ... Yi */
            || (c >= 0xac00  && c <= 0xd7a3)    /* Hangul Syllables */
            || (c >= 0xf900  && c <= 0xfaff)    /* CJK Compatibility Ideographs */
            || (c >= 0xfe30  && c <= 0xfe6f)    /* CJK Compatibility Forms */
@@ -2543,6 +2584,9 @@ main(int argc, char *argv[])
  *   outside the current view causing bad wrapping. Same goes
  *   for title and status windows.
  *
+ * - The cursor can wrap-around on the last line and cause the
+ *   window to scroll.
+ *
  * TODO
  * ----
  * Features that should be explored.