270
return isalnum(ch) || ch == '_';
271
}
272
273
+static void output_color(struct grep_opt *opt, const void *data, size_t size,
274
+ const char *color)
275
+{
276
+ if (opt->color && color && color[0]) {
277
+ opt->output(opt, color, strlen(color));
278
+ opt->output(opt, data, size);
279
+ opt->output(opt, GIT_COLOR_RESET, strlen(GIT_COLOR_RESET));
280
+ } else
281
+ opt->output(opt, data, size);
282
+}
283
+
284
+static void output_sep(struct grep_opt *opt, char sign)
285
+{
286
+ if (opt->null_following_name)
287
+ opt->output(opt, "\0", 1);
288
+ else
289
+ output_color(opt, &sign, 1, opt->color_sep);
290
+}
291
+
292
static void show_name(struct grep_opt *opt, const char *name)
293
{
275
- opt->output(opt, name, strlen(name));
294
+ output_color(opt, name, strlen(name), opt->color_filename);
295
opt->output(opt, opt->null_following_name ? "\0" : "\n", 1);
296
}
297
529
const char *name, unsigned lno, char sign)
530
{
531
int rest = eol - bol;
513
- char sign_str[1];
532
515
- sign_str[0] = sign;
533
if (opt->pre_context || opt->post_context) {
534
if (opt->last_shown == 0) {
518
- if (opt->show_hunk_mark)
519
- opt->output(opt, "--\n", 3);
520
- else
535
+ if (opt->show_hunk_mark) {
536
+ output_color(opt, "--", 2, opt->color_sep);
537
+ opt->output(opt, "\n", 1);
538
+ } else
539
opt->show_hunk_mark = 1;
522
- } else if (lno > opt->last_shown + 1)
523
- opt->output(opt, "--\n", 3);
540
+ } else if (lno > opt->last_shown + 1) {
541
+ output_color(opt, "--", 2, opt->color_sep);
542
+ opt->output(opt, "\n", 1);
543
+ }
544
}
545
opt->last_shown = lno;
546
527
- if (opt->null_following_name)
528
- sign_str[0] = '\0';
547
if (opt->pathname) {
530
- opt->output(opt, name, strlen(name));
531
- opt->output(opt, sign_str, 1);
548
+ output_color(opt, name, strlen(name), opt->color_filename);
549
+ output_sep(opt, sign);
550
}
551
if (opt->linenum) {
552
char buf[32];
553
snprintf(buf, sizeof(buf), "%d", lno);
536
- opt->output(opt, buf, strlen(buf));
537
- opt->output(opt, sign_str, 1);
554
+ output_color(opt, buf, strlen(buf), opt->color_lineno);
555
+ output_sep(opt, sign);
556
}
557
if (opt->color) {
558
regmatch_t match;
566
break;
567
568
opt->output(opt, bol, match.rm_so);
551
- opt->output(opt, opt->color_match,
552
- strlen(opt->color_match));
553
- opt->output(opt, bol + match.rm_so,
554
- (int)(match.rm_eo - match.rm_so));
555
- opt->output(opt, GIT_COLOR_RESET,
556
- strlen(GIT_COLOR_RESET));
569
+ output_color(opt, bol + match.rm_so,
570
+ match.rm_eo - match.rm_so,
571
+ opt->color_match);
572
bol += match.rm_eo;
573
rest -= match.rm_eo;
574
eflags = REG_NOTBOL;
838
return 1;
839
if (binary_match_only) {
840
opt->output(opt, "Binary file ", 12);
826
- opt->output(opt, name, strlen(name));
841
+ output_color(opt, name, strlen(name),
842
+ opt->color_filename);
843
opt->output(opt, " matches\n", 9);
844
return 1;
845
}
898
*/
899
if (opt->count && count) {
900
char buf[32];
885
- opt->output(opt, name, strlen(name));
886
- snprintf(buf, sizeof(buf), "%c%u\n",
887
- opt->null_following_name ? '\0' : ':', count);
901
+ output_color(opt, name, strlen(name), opt->color_filename);
902
+ output_sep(opt, ':');
903
+ snprintf(buf, sizeof(buf), "%u\n", count);
904
opt->output(opt, buf, strlen(buf));
905
}
906
return !!last_hit;