grep: fix coloring of hunk marks between files
Commit431d6e7b(grep: enable threading for context line printing) split the printing of the "--\n" mark between results from different files out into two places: show_line() in grep.c for the non-threaded case and work_done() in builtin/grep.c for the threaded case. Commit55f638bd(grep: Colorize filename, line number, and separator) updated the former, but not the latter, so the separators between files are not colored if threads are used. This patch merges the two. In the threaded case, hunk marks are now printed by show_line() for every file, including the first one, and the very first mark is simply skipped in work_done(). This ensures that the output is properly colored and works just as well. Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
committed by
Junio C Hamano
parent
a6605d76cd
commit
08303c3636
@ -93,8 +93,7 @@ static pthread_cond_t cond_write;
|
||||
/* Signalled when we are finished with everything. */
|
||||
static pthread_cond_t cond_result;
|
||||
|
||||
static int print_hunk_marks_between_files;
|
||||
static int printed_something;
|
||||
static int skip_first_line;
|
||||
|
||||
static void add_work(enum work_type type, char *name, void *id)
|
||||
{
|
||||
@ -160,10 +159,20 @@ static void work_done(struct work_item *w)
|
||||
todo_done = (todo_done+1) % ARRAY_SIZE(todo)) {
|
||||
w = &todo[todo_done];
|
||||
if (w->out.len) {
|
||||
if (print_hunk_marks_between_files && printed_something)
|
||||
write_or_die(1, "--\n", 3);
|
||||
write_or_die(1, w->out.buf, w->out.len);
|
||||
printed_something = 1;
|
||||
const char *p = w->out.buf;
|
||||
size_t len = w->out.len;
|
||||
|
||||
/* Skip the leading hunk mark of the first file. */
|
||||
if (skip_first_line) {
|
||||
while (len) {
|
||||
len--;
|
||||
if (*p++ == '\n')
|
||||
break;
|
||||
}
|
||||
skip_first_line = 0;
|
||||
}
|
||||
|
||||
write_or_die(1, p, len);
|
||||
}
|
||||
free(w->name);
|
||||
free(w->identifier);
|
||||
@ -968,7 +977,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
|
||||
|
||||
if (use_threads) {
|
||||
if (opt.pre_context || opt.post_context)
|
||||
print_hunk_marks_between_files = 1;
|
||||
skip_first_line = 1;
|
||||
start_threads(&opt);
|
||||
}
|
||||
#else
|
||||
|
||||
Reference in New Issue
Block a user