log: colour the diff stat
[tig.git] / src / log.c
blob94dbbc6eaf970870b51b301a79847807754ff09e
1 /* Copyright (c) 2006-2014 Jonas Fonseca <jonas.fonseca@gmail.com>
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License as
5 * published by the Free Software Foundation; either version 2 of
6 * the License, or (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include "tig/refdb.h"
15 #include "tig/display.h"
16 #include "tig/draw.h"
17 #include "tig/log.h"
18 #include "tig/diff.h"
19 #include "tig/pager.h"
21 struct log_state {
22 /* Used for tracking when we need to recalculate the previous
23 * commit, for example when the user scrolls up or uses the page
24 * up/down in the log view. */
25 int last_lineno;
26 enum line_type last_type;
27 bool commit_title_read;
28 bool after_commit_header;
29 bool reading_diff_stat;
32 static void
33 log_select(struct view *view, struct line *line)
35 struct log_state *state = view->private;
36 int last_lineno = state->last_lineno;
38 if (!last_lineno || abs(last_lineno - line->lineno) > 1
39 || (state->last_type == LINE_COMMIT && last_lineno > line->lineno)) {
40 const struct line *commit_line = find_prev_line_by_type(view, line, LINE_COMMIT);
42 if (commit_line)
43 string_copy_rev_from_commit_line(view->ref, commit_line->data);
46 if (line->type == LINE_COMMIT && !view_has_flags(view, VIEW_NO_REF)) {
47 string_copy_rev_from_commit_line(view->ref, (char *)line->data);
49 string_copy_rev(view->env->commit, view->ref);
50 state->last_lineno = line->lineno;
51 state->last_type = line->type;
54 static bool
55 log_open(struct view *view, enum open_flags flags)
57 const char *log_argv[] = {
58 "git", "log", encoding_arg, commit_order_arg(), "--cc",
59 "--stat", "%(cmdlineargs)", "%(revargs)", "--no-color",
60 "--", "%(fileargs)", NULL
63 if (!pager_column_init(view))
64 return FALSE;
65 return begin_update(view, NULL, log_argv, flags);
68 static enum request
69 log_request(struct view *view, enum request request, struct line *line)
71 switch (request) {
72 case REQ_REFRESH:
73 load_refs(TRUE);
74 refresh_view(view);
75 return REQ_NONE;
77 case REQ_ENTER:
78 if (!display[1] || strcmp(display[1]->vid, view->ref))
79 open_diff_view(view, OPEN_SPLIT);
80 return REQ_NONE;
82 default:
83 return request;
87 static bool
88 log_read(struct view *view, char *data)
90 enum line_type type;
91 struct log_state *state = view->private;
92 size_t len;
94 if (!data)
95 return TRUE;
97 type = get_line_type(data);
98 len = strlen(data);
100 if (type == LINE_COMMIT)
101 state->commit_title_read = TRUE;
102 else if (state->commit_title_read && len < 1) {
103 state->commit_title_read = FALSE;
104 state->after_commit_header = TRUE;
105 } else if (state->after_commit_header && len < 1) {
106 state->after_commit_header = FALSE;
107 state->reading_diff_stat = TRUE;
108 } else if (state->reading_diff_stat) {
109 if (diff_common_add_diff_stat(view, data))
110 return TRUE;
111 state->reading_diff_stat = FALSE;
114 return pager_common_read(view, data, type);
117 static bool
118 log_draw(struct view *view, struct line *line, unsigned int lineno)
120 char *text = line->data;
121 enum line_type type = line->type;
122 struct view_column *column = get_view_column(view, VIEW_COLUMN_LINE_NUMBER);
124 if (column && (type == LINE_DIFF_STAT) && draw_lineno(view, column, lineno))
125 return TRUE;
127 if (type == LINE_DIFF_STAT) {
128 diff_common_draw_diff_stat(view, &type, &text);
129 draw_text(view, type, text);
130 return TRUE;
133 return view_column_draw(view, line, lineno);
136 static struct view_ops log_ops = {
137 "line",
138 argv_env.head,
139 VIEW_ADD_PAGER_REFS | VIEW_OPEN_DIFF | VIEW_SEND_CHILD_ENTER | VIEW_LOG_LIKE | VIEW_REFRESH,
140 sizeof(struct log_state),
141 log_open,
142 log_read,
143 log_draw,
144 log_request,
145 view_column_grep,
146 log_select,
147 NULL,
148 pager_get_column_data,
151 DEFINE_VIEW(log);
153 /* vim: set ts=8 sw=8 noexpandtab: */