grep: colorize all fields
Colors were picked in accordance to GNU grep.
--- a/hgext/color.py Sun Oct 14 20:40:23 2012 +0200
+++ b/hgext/color.py Sun Oct 14 20:27:55 2012 +0200
@@ -224,6 +224,13 @@
_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',
--- a/mercurial/commands.py Sun Oct 14 20:40:23 2012 +0200
+++ b/mercurial/commands.py Sun Oct 14 20:27:55 2012 +0200
@@ -2994,16 +2994,17 @@
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 @@
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)
--- a/tests/test-grep.t Sun Oct 14 20:40:23 2012 +0200
+++ b/tests/test-grep.t Sun Oct 14 20:27:55 2012 +0200
@@ -32,9 +32,9 @@
$ 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