From patchwork Thu Aug 25 15:24:55 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [2, of, 5] grep: factor out function that prints matched line with labels From: Yuya Nishihara X-Patchwork-Id: 16415 Message-Id: <8867e120a95b1f3d89c3.1472138695@mimosa> To: mercurial-devel@mercurial-scm.org Date: Fri, 26 Aug 2016 00:24:55 +0900 # HG changeset patch # User Yuya Nishihara # Date 1471496989 -32400 # Thu Aug 18 14:09:49 2016 +0900 # Node ID 8867e120a95b1f3d89c334de323c518b8be16985 # Parent b1dc2acf966186db6f82017001d6d449fec5021a grep: factor out function that prints matched line with labels Prepares for formatter support. diff --git a/mercurial/commands.py b/mercurial/commands.py --- a/mercurial/commands.py +++ b/mercurial/commands.py @@ -4356,14 +4356,6 @@ def grep(ui, repo, pattern, *pats, **opt yield m.span() p = m.end() - def __iter__(self): - p = 0 - for s, e in self.findpos(): - yield self.line[p:s], '' - yield self.line[s:e], 'grep.match' - p = e - yield self.line[p:], '' - matches = {} copies = {} def grepbody(fn, rev, body): @@ -4424,14 +4416,21 @@ def grep(ui, repo, pattern, *pats, **opt if not opts.get('text') and binary(): ui.write(_(" Binary file matches")) else: - for s, label in l: - ui.write(s, label=label) + displaymatches(l) ui.write(eol) found = True if opts.get('files_with_matches'): break return found + def displaymatches(l): + p = 0 + for s, e in l.findpos(): + ui.write(l.line[p:s]) + ui.write(l.line[s:e], label='grep.match') + p = e + ui.write(l.line[p:]) + skip = {} revfiles = {} matchfn = scmutil.match(repo[None], pats, opts)