diff --git a/hgext/color.py b/hgext/color.py --- a/hgext/color.py +++ b/hgext/color.py @@ -224,6 +224,13 @@ except ImportError: _terminfo_params = False _styles = {'grep.match': 'red bold', + 'grep.linenumber': 'green', + 'grep.rev': 'green', + 'grep.change': 'green', + 'grep.sep': 'cyan', + 'grep.filename': 'magenta', + 'grep.user': 'magenta', + 'grep.date': 'magenta', 'bookmarks.current': 'green', 'branches.active': 'none', 'branches.closed': 'black bold', diff --git a/mercurial/commands.py b/mercurial/commands.py --- a/mercurial/commands.py +++ b/mercurial/commands.py @@ -2994,16 +2994,17 @@ def grep(ui, repo, pattern, *pats, **opt else: iter = [('', l) for l in states] for change, l in iter: - cols = [fn, str(rev)] + cols = [(fn, 'grep.filename'), (str(rev), 'grep.rev')] before, match, after = None, None, None + if opts.get('line_number'): - cols.append(str(l.linenum)) + cols.append((str(l.linenum), 'grep.linenumber')) if opts.get('all'): - cols.append(change) + cols.append((change, 'grep.change')) if opts.get('user'): - cols.append(ui.shortuser(ctx.user())) + cols.append((ui.shortuser(ctx.user()), 'grep.user')) if opts.get('date'): - cols.append(datefunc(ctx.date())) + cols.append((datefunc(ctx.date()), 'grep.date')) if opts.get('files_with_matches'): c = (fn, rev) if c in filerevmatches: @@ -3013,12 +3014,16 @@ def grep(ui, repo, pattern, *pats, **opt before = l.line[:l.colstart] match = l.line[l.colstart:l.colend] after = l.line[l.colend:] - ui.write(sep.join(cols)) + for col, label in cols[:-1]: + ui.write(col, label=label) + ui.write(sep, label='grep.sep') + ui.write(cols[-1][0], label=cols[-1][1]) if before is not None: + ui.write(sep, label='grep.sep') if not opts.get('text') and binary(): - ui.write(sep + " Binary file matches") + ui.write(" Binary file matches") else: - ui.write(sep + before) + ui.write(before) ui.write(match, label='grep.match') ui.write(after) ui.write(eol) diff --git a/tests/test-grep.t b/tests/test-grep.t --- a/tests/test-grep.t +++ b/tests/test-grep.t @@ -32,9 +32,9 @@ simple with color $ hg --config extensions.color= grep --config color.mode=ansi \ > --color=always port port - port:4:ex\x1b[0;31;1mport\x1b[0m (esc) - port:4:va\x1b[0;31;1mport\x1b[0might (esc) - port:4:im\x1b[0;31;1mport\x1b[0m/export (esc) + \x1b[0;35mport\x1b[0m\x1b[0;36m:\x1b[0m\x1b[0;32m4\x1b[0m\x1b[0;36m:\x1b[0mex\x1b[0;31;1mport\x1b[0m (esc) + \x1b[0;35mport\x1b[0m\x1b[0;36m:\x1b[0m\x1b[0;32m4\x1b[0m\x1b[0;36m:\x1b[0mva\x1b[0;31;1mport\x1b[0might (esc) + \x1b[0;35mport\x1b[0m\x1b[0;36m:\x1b[0m\x1b[0;32m4\x1b[0m\x1b[0;36m:\x1b[0mim\x1b[0;31;1mport\x1b[0m/export (esc) all