add color support to 'tog diff'
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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
diff --git a/include/got_error.h b/include/got_error.h
index f1c6547..4e252ff 100644
--- a/include/got_error.h
+++ b/include/got_error.h
@@ -125,6 +125,7 @@
#define GOT_ERR_TAG_EXISTS 109
#define GOT_ERR_GIT_REPO_FORMAT 110
#define GOT_ERR_REBASE_REQUIRED 111
+#define GOT_ERR_REGEX 112
static const struct got_error {
int code;
@@ -256,6 +257,7 @@ static const struct got_error {
{ GOT_ERR_TAG_EXISTS,"specified tag already exists" },
{ GOT_ERR_GIT_REPO_FORMAT,"unknown git repository format version" },
{ GOT_ERR_REBASE_REQUIRED,"specified branch must be rebased first" },
+ { GOT_ERR_REGEX, "regular expression error" },
};
/*
diff --git a/tog/tog.1 b/tog/tog.1
index 4af5ca7..59ba7c9 100644
--- a/tog/tog.1
+++ b/tog/tog.1
@@ -296,6 +296,43 @@ An abbreviated hash argument will be expanded to a full SHA1 hash
automatically, provided the abbreviation is unique.
.El
.El
+.Sh ENVIRONMENT
+.Bl -tag -width TOG_COLORS
+.It Ev TOG_COLORS
+.Nm
+shows colorized output if this variable is set to a non-empty value.
+The default color scheme can be modified by setting the environment
+variables documented below.
+The colors available in color schemes are
+.Dq black ,
+.Dq red ,
+.Dq green ,
+.Dq yellow ,
+.Dq blue ,
+.Dq megenta ,
+and
+.Dq cyan .
+.It Ev TOG_COLOR_DIFF_MINUS
+The color used to mark up removed lines in diffs.
+If not set, the default value
+.Dq magenta
+is used.
+.It Ev TOG_COLOR_DIFF_PLUS
+The color used to mark up added lines in diffs.
+If not set, the default value
+.Dq cyan
+is used.
+.It Ev TOG_COLOR_DIFF_CHUNK_HEADER
+The color used to mark up chunk header lines in diffs.
+If not set, the default value
+.Dq yellow
+is used.
+.It Ev TOG_COLOR_DIFF_META
+The color used to mark up meta data in diffs.
+If not set, the default value
+.Dq green
+is used.
+.El
.Sh EXIT STATUS
.Ex -std tog
.Sh SEE ALSO
diff --git a/tog/tog.c b/tog/tog.c
index 0ff3b62..cc9b6c1 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -112,6 +112,13 @@ struct commit_queue {
struct commit_queue_head head;
};
+struct tog_line_color {
+ SIMPLEQ_ENTRY(tog_line_color) entry;
+ regex_t regex;
+ short colorpair;
+};
+SIMPLEQ_HEAD(tog_line_colors, tog_line_color);
+
struct tog_diff_view_state {
struct got_object_id *id1, *id2;
FILE *f;
@@ -121,6 +128,7 @@ struct tog_diff_view_state {
int diff_context;
struct got_repository *repo;
struct got_reflist_head *refs;
+ struct tog_line_colors line_colors;
/* passed from log view; may be NULL */
struct tog_view *log_view;
@@ -2257,6 +2265,10 @@ init_curses(void)
intrflush(stdscr, FALSE);
keypad(stdscr, TRUE);
curs_set(0);
+ if (getenv("TOG_COLORS") != NULL) {
+ start_color();
+ use_default_colors();
+ }
signal(SIGWINCH, tog_sigwinch);
signal(SIGPIPE, tog_sigpipe);
}
@@ -2437,14 +2449,36 @@ parse_next_line(FILE *f, size_t *len)
return line;
}
+static int
+match_line(const char *line, regex_t *regex)
+{
+ regmatch_t regmatch;
+
+ return regexec(regex, line, 1, ®match, 0) == 0;
+}
+
+struct tog_line_color *
+match_line_color(struct tog_line_colors *colors, const char *line)
+{
+ struct tog_line_color *tc = NULL;
+
+ SIMPLEQ_FOREACH(tc, colors, entry) {
+ if (match_line(line, &tc->regex))
+ return tc;
+ }
+
+ return NULL;
+}
+
static const struct got_error *
draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
- int *last_displayed_line, int *eof, int max_lines,
- char *header)
+ int *last_displayed_line, int *eof, int max_lines, char *header,
+ struct tog_line_colors *colors)
{
const struct got_error *err;
int nlines = 0, nprinted = 0;
char *line;
+ struct tog_line_color *lc;
size_t len;
wchar_t *wline;
int width;
@@ -2488,7 +2522,15 @@ draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
free(line);
return err;
}
+
+ lc = match_line_color(colors, line);
+ if (lc)
+ wattr_on(view->window,
+ COLOR_PAIR(lc->colorpair), NULL);
waddwstr(view->window, wline);
+ if (lc)
+ wattr_off(view->window,
+ COLOR_PAIR(lc->colorpair), NULL);
if (width <= view->ncols - 1)
waddch(view->window, '\n');
if (++nprinted == 1)
@@ -2684,6 +2726,93 @@ diff_view_indicate_progress(struct tog_view *view)
}
static const struct got_error *
+add_line_color(struct tog_line_colors *colors, const char *pattern,
+ int idx, short color)
+{
+ const struct got_error *err = NULL;
+ struct tog_line_color *lc;
+ int regerr = 0;
+
+ init_pair(idx, color, -1);
+
+ lc = calloc(1, sizeof(*lc));
+ if (lc == NULL)
+ return got_error_from_errno("calloc");
+ regerr = regcomp(&lc->regex, pattern,
+ REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
+ if (regerr) {
+ static char regerr_msg[512];
+ static char err_msg[512];
+ regerror(regerr, &lc->regex, regerr_msg,
+ sizeof(regerr_msg));
+ snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
+ regerr_msg);
+ err = got_error_msg(GOT_ERR_REGEX, err_msg);
+ free(lc);
+ return err;
+ }
+ lc->colorpair = idx;
+ SIMPLEQ_INSERT_HEAD(colors, lc, entry);
+ return NULL;
+}
+
+void
+free_line_colors(struct tog_line_colors *colors)
+{
+ struct tog_line_color *lc;
+
+ while (!SIMPLEQ_EMPTY(colors)) {
+ lc = SIMPLEQ_FIRST(colors);
+ SIMPLEQ_REMOVE_HEAD(colors, entry);
+ regfree(&lc->regex);
+ free(lc);
+ }
+}
+
+static int
+default_color_value(const char *envvar)
+{
+ if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
+ return COLOR_MAGENTA;
+ if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
+ return COLOR_CYAN;
+ if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
+ return COLOR_YELLOW;
+ if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
+ return COLOR_GREEN;
+
+ return -1;
+}
+
+static int
+get_color_value(const char *envvar)
+{
+ const char *val = getenv(envvar);
+
+ if (val == NULL)
+ return default_color_value(envvar);
+
+ if (strcasecmp(val, "black") == 0)
+ return COLOR_BLACK;
+ if (strcasecmp(val, "red") == 0)
+ return COLOR_RED;
+ if (strcasecmp(val, "green") == 0)
+ return COLOR_GREEN;
+ if (strcasecmp(val, "yellow") == 0)
+ return COLOR_YELLOW;
+ if (strcasecmp(val, "blue") == 0)
+ return COLOR_BLUE;
+ if (strcasecmp(val, "magenta") == 0)
+ return COLOR_MAGENTA;
+ if (strcasecmp(val, "cyan") == 0)
+ return COLOR_CYAN;
+ if (strcasecmp(val, "white") == 0)
+ return COLOR_WHITE;
+
+ return default_color_value(envvar);
+}
+
+static const struct got_error *
open_diff_view(struct tog_view *view, struct got_object_id *id1,
struct got_object_id *id2, struct tog_view *log_view,
struct got_reflist_head *refs, struct got_repository *repo)
@@ -2723,6 +2852,35 @@ open_diff_view(struct tog_view *view, struct got_object_id *id1,
view->state.diff.log_view = log_view;
view->state.diff.repo = repo;
view->state.diff.refs = refs;
+ SIMPLEQ_INIT(&view->state.diff.line_colors);
+
+ if (has_colors() && getenv("TOG_COLORS") != NULL) {
+ err = add_line_color(&view->state.diff.line_colors,
+ "^-", 1, get_color_value("TOG_COLOR_DIFF_MINUS"));
+ if (err)
+ return err;
+ err = add_line_color(&view->state.diff.line_colors,
+ "^\\+", 2, get_color_value("TOG_COLOR_DIFF_PLUS"));
+ if (err) {
+ free_line_colors(&view->state.diff.line_colors);
+ return err;
+ }
+ err = add_line_color(&view->state.diff.line_colors,
+ "^@@", 3,
+ get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
+ if (err) {
+ free_line_colors(&view->state.diff.line_colors);
+ return err;
+ }
+
+ err = add_line_color(&view->state.diff.line_colors,
+ "^(commit|(blob|file) [-+] )", 4,
+ get_color_value("TOG_COLOR_DIFF_META"));
+ if (err) {
+ free_line_colors(&view->state.diff.line_colors);
+ return err;
+ }
+ }
if (log_view && view_is_splitscreen(view))
show_log_view(log_view); /* draw vborder */
@@ -2755,6 +2913,7 @@ close_diff_view(struct tog_view *view)
view->state.diff.id2 = NULL;
if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
err = got_error_from_errno("fclose");
+ free_line_colors(&view->state.diff.line_colors);
return err;
}
@@ -2786,7 +2945,7 @@ show_diff_view(struct tog_view *view)
return draw_file(view, s->f, &s->first_displayed_line,
&s->last_displayed_line, &s->eof, view->nlines,
- header);
+ header, &s->line_colors);
}
static const struct got_error *
@@ -3492,15 +3651,6 @@ search_start_blame_view(struct tog_view *view)
return NULL;
}
-static int
-match_line(const char *line, regex_t *regex)
-{
- regmatch_t regmatch;
-
- return regexec(regex, line, 1, ®match, 0) == 0;
-}
-
-
static const struct got_error *
search_next_blame_view(struct tog_view *view)
{