Code

Make the low-level drawing functions update the view column
[tig.git] / tig.c
diff --git a/tig.c b/tig.c
index 4a42cf7bbb7656d7c67b9bc57b366e58425156cf..e58a6b65f2f3e3c5e50f15c7c275f569fbdd11aa 100644 (file)
--- a/tig.c
+++ b/tig.c
@@ -1440,8 +1440,6 @@ enum open_flags {
 struct view_ops {
        /* What type of content being displayed. Used in the title bar. */
        const char *type;
-       /* Default command arguments. */
-       const char **argv;
        /* Open and reads in all view content. */
        bool (*open)(struct view *view, enum open_flags flags);
        /* Read one line; updates view->line. */
@@ -1521,7 +1519,7 @@ set_view_attr(struct view *view, enum line_type type)
 
 #define VIEW_MAX_LEN(view) ((view)->width + (view)->yoffset - (view)->col)
 
-static int
+static bool
 draw_chars(struct view *view, enum line_type type, const char *string,
           int max_len, bool use_tilde)
 {
@@ -1532,7 +1530,7 @@ draw_chars(struct view *view, enum line_type type, const char *string,
        size_t skip = view->yoffset > view->col ? view->yoffset - view->col : 0;
 
        if (max_len <= 0)
-               return 0;
+               return VIEW_MAX_LEN(view) <= 0;
 
        len = utf8_length(&string, skip, &col, max_len, &trimmed, use_tilde, opt_tab_size);
 
@@ -1563,25 +1561,26 @@ draw_chars(struct view *view, enum line_type type, const char *string,
                }
        }
 
-       return col;
+       view->col += col;
+       return VIEW_MAX_LEN(view) <= 0;
 }
 
-static int
+static bool
 draw_space(struct view *view, enum line_type type, int max, int spaces)
 {
        static char space[] = "                    ";
-       int col = 0;
 
        spaces = MIN(max, spaces);
 
        while (spaces > 0) {
                int len = MIN(spaces, sizeof(space) - 1);
 
-               col += draw_chars(view, type, space, len, FALSE);
+               if (draw_chars(view, type, space, len, FALSE))
+                       return TRUE;
                spaces -= len;
        }
 
-       return col;
+       return VIEW_MAX_LEN(view) <= 0;
 }
 
 static bool
@@ -1592,9 +1591,10 @@ draw_text(struct view *view, enum line_type type, const char *string)
        do {
                size_t pos = string_expand(text, sizeof(text), string, opt_tab_size);
 
-               view->col += draw_chars(view, type, text, VIEW_MAX_LEN(view), TRUE);
+               if (draw_chars(view, type, text, VIEW_MAX_LEN(view), TRUE))
+                       return TRUE;
                string += pos;
-       } while (*string && VIEW_MAX_LEN(view) > 0);
+       } while (*string);
 
        return VIEW_MAX_LEN(view) <= 0;
 }
@@ -1629,16 +1629,13 @@ static bool
 draw_field(struct view *view, enum line_type type, const char *text, int len, bool trim)
 {
        int max = MIN(VIEW_MAX_LEN(view), len);
-       int col;
+       int col = view->col;
 
-       if (text)
-               col = draw_chars(view, type, text, max - 1, trim);
-       else
-               col = draw_space(view, type, max - 1, max - 1);
+       if (!text) 
+               return draw_space(view, type, max, max);
 
-       view->col += col;
-       view->col += draw_space(view, LINE_DEFAULT, max - col, max - col);
-       return VIEW_MAX_LEN(view) <= 0;
+       return draw_chars(view, type, text, max - 1, trim)
+           || draw_space(view, LINE_DEFAULT, max - (view->col - col), max);
 }
 
 static bool
@@ -1701,9 +1698,9 @@ draw_lineno(struct view *view, unsigned int lineno)
                        text = number;
        }
        if (text)
-               view->col += draw_chars(view, LINE_LINE_NUMBER, text, max, TRUE);
+               draw_chars(view, LINE_LINE_NUMBER, text, max, TRUE);
        else
-               view->col += draw_space(view, LINE_LINE_NUMBER, max, digits3);
+               draw_space(view, LINE_LINE_NUMBER, max, digits3);
        return draw_graphic(view, LINE_DEFAULT, &separator, 1, TRUE);
 }
 
@@ -2528,7 +2525,7 @@ prepare_update_file(struct view *view, const char *name)
 }
 
 static bool
-begin_update(struct view *view, const char *dir, enum open_flags flags)
+begin_update(struct view *view, const char *dir, const char **argv, enum open_flags flags)
 {
        bool reload = !!(flags & (OPEN_RELOAD | OPEN_REFRESH | OPEN_PREPARED));
        bool refresh = flags & (OPEN_REFRESH | OPEN_PREPARED);
@@ -2540,7 +2537,7 @@ begin_update(struct view *view, const char *dir, enum open_flags flags)
                end_update(view, TRUE);
 
        if (!refresh) {
-               if (!prepare_io(view, dir, view->ops->argv, TRUE))
+               if (!prepare_io(view, dir, argv, TRUE))
                        return FALSE;
 
                /* Put the current ref_* value to the view title ref
@@ -2562,7 +2559,7 @@ begin_update(struct view *view, const char *dir, enum open_flags flags)
 static bool
 view_open(struct view *view, enum open_flags flags)
 {
-       return begin_update(view, NULL, flags);
+       return begin_update(view, NULL, NULL, flags);
 }
 
 static bool
@@ -3334,7 +3331,6 @@ pager_select(struct view *view, struct line *line)
 
 static struct view_ops pager_ops = {
        "line",
-       NULL,
        view_open,
        pager_read,
        pager_draw,
@@ -3343,9 +3339,15 @@ static struct view_ops pager_ops = {
        pager_select,
 };
 
-static const char *log_argv[SIZEOF_ARG] = {
-       "git", "log", "--no-color", "--cc", "--stat", "-n100", "%(head)", NULL
-};
+static bool
+log_open(struct view *view, enum open_flags flags)
+{
+       static const char *log_argv[] = {
+               "git", "log", "--no-color", "--cc", "--stat", "-n100", "%(head)", NULL
+       };
+
+       return begin_update(view, NULL, log_argv, flags);
+}
 
 static enum request
 log_request(struct view *view, enum request request, struct line *line)
@@ -3362,8 +3364,7 @@ log_request(struct view *view, enum request request, struct line *line)
 
 static struct view_ops log_ops = {
        "line",
-       log_argv,
-       view_open,
+       log_open,
        pager_read,
        pager_draw,
        log_request,
@@ -3371,11 +3372,17 @@ static struct view_ops log_ops = {
        pager_select,
 };
 
-static const char *diff_argv[SIZEOF_ARG] = {
-       "git", "show", "--pretty=fuller", "--no-color", "--root",
-               "--patch-with-stat", "--find-copies-harder", "-C",
-               "%(diffargs)", "%(commit)", "--", "%(fileargs)", NULL
-};
+static bool
+diff_open(struct view *view, enum open_flags flags)
+{
+       static const char *diff_argv[] = {
+               "git", "show", "--pretty=fuller", "--no-color", "--root",
+                       "--patch-with-stat", "--find-copies-harder", "-C",
+                       "%(diffargs)", "%(commit)", "--", "%(fileargs)", NULL
+       };
+
+       return begin_update(view, NULL, diff_argv, flags);
+}
 
 static bool
 diff_read(struct view *view, char *data)
@@ -3406,8 +3413,7 @@ diff_read(struct view *view, char *data)
 
 static struct view_ops diff_ops = {
        "line",
-       diff_argv,
-       view_open,
+       diff_open,
        diff_read,
        pager_draw,
        pager_request,
@@ -3537,7 +3543,6 @@ help_request(struct view *view, enum request request, struct line *line)
 
 static struct view_ops help_ops = {
        "line",
-       NULL,
        help_open,
        NULL,
        pager_draw,
@@ -3976,6 +3981,10 @@ tree_select(struct view *view, struct line *line)
 static bool
 tree_open(struct view *view, enum open_flags flags)
 {
+       static const char *tree_argv[] = {
+               "git", "ls-tree", "%(commit)", "%(directory)", NULL
+       };
+
        if (view->lines == 0 && opt_prefix[0]) {
                char *pos = opt_prefix;
 
@@ -3996,16 +4005,11 @@ tree_open(struct view *view, enum open_flags flags)
                opt_path[0] = 0;
        }
 
-       return begin_update(view, opt_cdup, flags);
+       return begin_update(view, opt_cdup, tree_argv, flags);
 }
 
-static const char *tree_argv[SIZEOF_ARG] = {
-       "git", "ls-tree", "%(commit)", "%(directory)", NULL
-};
-
 static struct view_ops tree_ops = {
        "file",
-       tree_argv,
        tree_open,
        tree_read,
        tree_draw,
@@ -4014,6 +4018,16 @@ static struct view_ops tree_ops = {
        tree_select,
 };
 
+static bool
+blob_open(struct view *view, enum open_flags flags)
+{
+       static const char *blob_argv[] = {
+               "git", "cat-file", "blob", "%(blob)", NULL
+       };
+
+       return begin_update(view, NULL, blob_argv, flags);
+}
+
 static bool
 blob_read(struct view *view, char *line)
 {
@@ -4034,14 +4048,9 @@ blob_request(struct view *view, enum request request, struct line *line)
        }
 }
 
-static const char *blob_argv[SIZEOF_ARG] = {
-       "git", "cat-file", "blob", "%(blob)", NULL
-};
-
 static struct view_ops blob_ops = {
        "line",
-       blob_argv,
-       view_open,
+       blob_open,
        blob_read,
        pager_draw,
        blob_request,
@@ -4485,7 +4494,6 @@ blame_select(struct view *view, struct line *line)
 
 static struct view_ops blame_ops = {
        "line",
-       NULL,
        blame_open,
        blame_read,
        blame_draw,
@@ -4688,7 +4696,6 @@ branch_select(struct view *view, struct line *line)
 
 static struct view_ops branch_ops = {
        "branch",
-       NULL,
        branch_open,
        branch_read,
        branch_draw,
@@ -5455,7 +5462,6 @@ status_grep(struct view *view, struct line *line)
 
 static struct view_ops status_ops = {
        "file",
-       NULL,
        status_open,
        NULL,
        status_draw,
@@ -5693,7 +5699,6 @@ stage_request(struct view *view, enum request request, struct line *line)
 
 static struct view_ops stage_ops = {
        "line",
-       NULL,
        view_open,
        pager_read,
        pager_draw,
@@ -5785,11 +5790,17 @@ struct commit {
        struct graph_canvas graph;      /* Ancestry chain graphics. */
 };
 
-static const char *main_argv[SIZEOF_ARG] = {
-       "git", "log", "--no-color", "--pretty=raw", "--parents",
-               "--topo-order", "%(diffargs)", "%(revargs)",
-               "--", "%(fileargs)", NULL
-};
+static bool
+main_open(struct view *view, enum open_flags flags)
+{
+       static const char *main_argv[] = {
+               "git", "log", "--no-color", "--pretty=raw", "--parents",
+                       "--topo-order", "%(diffargs)", "%(revargs)",
+                       "--", "%(fileargs)", NULL
+       };
+
+       return begin_update(view, NULL, main_argv, flags);
+}
 
 static bool
 main_draw(struct view *view, struct line *line, unsigned int lineno)
@@ -5992,8 +6003,7 @@ main_select(struct view *view, struct line *line)
 
 static struct view_ops main_ops = {
        "commit",
-       main_argv,
-       view_open,
+       main_open,
        main_read,
        main_draw,
        main_request,