split tog's show_diff_view into open/show/close view functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
diff --git a/tog/tog.c b/tog/tog.c
index 0876182..eddecc7 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -95,12 +95,23 @@ struct tog_view {
int nlines, ncols, begin_y, begin_x;
int lines, cols; /* copies of LINES and COLS */
struct tog_view *parent;
+
+ /* type-specific state */
enum tog_view_type type;
+ union {
+ struct {
+ FILE *f;
+ int first_displayed_line;
+ int last_displayed_line;
+ } diff;
+ } state;
};
static const struct got_error *
-show_diff_view(struct tog_view *, struct got_object *, struct got_object *,
+open_diff_view(struct tog_view *, struct got_object *, struct got_object *,
struct got_repository *);
+static const struct got_error *show_diff_view(struct tog_view *);
+static void close_diff_view(struct tog_view *);
static const struct got_error *
show_log_view(struct tog_view *, struct got_object_id *,
struct got_repository *, const char *);
@@ -766,7 +777,11 @@ show_commit(struct tog_view *parent_view, struct commit_queue_entry *entry,
goto done;
}
- err = show_diff_view(view, obj1, obj2, repo);
+ err = open_diff_view(view, obj1, obj2, repo);
+ if (err)
+ goto done;
+ err = show_diff_view(view);
+ close_diff_view(view);
view_close(view);
view_show(parent_view);
done:
@@ -1128,14 +1143,11 @@ draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
}
static const struct got_error *
-show_diff_view(struct tog_view *view, struct got_object *obj1,
+open_diff_view(struct tog_view *view, struct got_object *obj1,
struct got_object *obj2, struct got_repository *repo)
{
const struct got_error *err;
FILE *f;
- int ch, done = 0;
- int first_displayed_line = 1, last_displayed_line = view->nlines;
- int eof, i;
if (obj1 != NULL && obj2 != NULL &&
got_object_get_type(obj1) != got_object_get_type(obj2))
@@ -1161,11 +1173,33 @@ show_diff_view(struct tog_view *view, struct got_object *obj1,
fflush(f);
+ view->state.diff.f = f;
+ view->state.diff.first_displayed_line = 1;
+ view->state.diff.last_displayed_line = view->nlines;
+
+ return NULL;
+}
+
+static void
+close_diff_view(struct tog_view *view)
+{
+ fclose(view->state.diff.f);
+}
+
+static const struct got_error *
+show_diff_view(struct tog_view *view)
+{
+ const struct got_error *err = NULL;
+ int ch, done = 0;
+ int eof, i;
+
view_show(view);
while (!done) {
- err = draw_file(view, f, &first_displayed_line,
- &last_displayed_line, &eof, view->nlines);
+ err = draw_file(view, view->state.diff.f,
+ &view->state.diff.first_displayed_line,
+ &view->state.diff.last_displayed_line,
+ &eof, view->nlines);
if (err)
break;
nodelay(stdscr, FALSE);
@@ -1177,27 +1211,28 @@ show_diff_view(struct tog_view *view, struct got_object *obj1,
break;
case 'k':
case KEY_UP:
- if (first_displayed_line > 1)
- first_displayed_line--;
+ if (view->state.diff.first_displayed_line > 1)
+ view->state.diff.first_displayed_line--;
break;
case KEY_PPAGE:
case KEY_BACKSPACE:
i = 0;
while (i++ < view->nlines - 1 &&
- first_displayed_line > 1)
- first_displayed_line--;
+ view->state.diff.first_displayed_line > 1)
+ view->state.diff.first_displayed_line--;
break;
case 'j':
case KEY_DOWN:
if (!eof)
- first_displayed_line++;
+ view->state.diff.first_displayed_line++;
break;
case KEY_NPAGE:
case ' ':
i = 0;
while (!eof && i++ < view->nlines - 1) {
- char *line = parse_next_line(f, NULL);
- first_displayed_line++;
+ char *line = parse_next_line(
+ view->state.diff.f, NULL);
+ view->state.diff.first_displayed_line++;
if (line == NULL)
break;
}
@@ -1212,7 +1247,6 @@ show_diff_view(struct tog_view *view, struct got_object *obj1,
}
}
done:
- fclose(f);
return err;
}
@@ -1278,7 +1312,11 @@ cmd_diff(int argc, char *argv[])
error = got_error_from_errno();
goto done;
}
- error = show_diff_view(view, obj1, obj2, repo);
+ error = open_diff_view(view, obj1, obj2, repo);
+ if (error)
+ goto done;
+ error = show_diff_view(view);
+ close_diff_view(view);
view_close(view);
done:
got_repo_close(repo);
@@ -1882,8 +1920,14 @@ show_blame_view(struct tog_view *view, const char *path,
err = got_error_from_errno();
break;
}
- err = show_diff_view(diff_view, pobj, obj, repo);
+ err = open_diff_view(diff_view, pobj, obj, repo);
+ if (err)
+ break;
+ err = show_diff_view(diff_view);
+ close_diff_view(diff_view);
view_close(diff_view);
+ if (err)
+ break;
view_show(view);
if (pobj) {
got_object_close(pobj);