From 95f63f3ca271aac5dba22a45081848d0add9b7c4 Mon Sep 17 00:00:00 2001 From: moculus Date: Sun, 8 Jul 2007 23:46:59 +0000 Subject: [PATCH] added grep command --HG-- branch : pmacs2 --- method.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/method.py b/method.py index b1f0427..dee18fc 100644 --- a/method.py +++ b/method.py @@ -1398,3 +1398,30 @@ class RegisterRestore(Method): if len(name) > self.MAX_REG: name = name[0:self.MAX_REG] + '...' w.set_error('Restored %r from register %r' % (text2, name2)) + +class Grep(Method): + '''help here''' + args = [Argument('pattern', datatype="str", prompt="Pattern: ")] + def _execute(self, w, **vargs): + cmd = ("/usr/bin/grep", '-E', '-n', vargs['pattern'], '-') + pipe = popen2.Popen3(cmd, capturestderr=True) + pid = pipe.pid + + indata = w.buffer.make_string() + pipe.tochild.write(indata) + pipe.tochild.close() + + outdata = pipe.fromchild.read() + errdata = pipe.childerr.read() + status = pipe.wait() >> 8 + + if status == 0: + num = len(outdata.split('\n')) + w.application.data_buffer("*Grep*", outdata, switch_to=True) + w.application.set_error("Grep: %d matches found" % num) + elif status == 1: + w.application.set_error("Grep: no matches found") + else: + w.application.data_buffer("*Grep*", outdata, switch_to=True) + w.set_error("Grep: error (exited with status %d)" % status) + \ No newline at end of file