summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorTimothee Cour <timothee.cour2@gmail.com>2018-04-12 22:42:42 -0700
committerAndreas Rumpf <rumpf_a@web.de>2018-04-13 07:42:42 +0200
commit44ee3c27c08d3908b336581f67680d26b9f8a528 (patch)
tree35d060b454dff578626bb96dfd58069e03528666
parentf6df2d9956a79eda048639107274ce60cbcf3542 (diff)
downloadNim-44ee3c27c08d3908b336581f67680d26b9f8a528.tar.gz
[nimgrep] add option --oneline to keep file:line info when piping (#7592)
through grep
-rw-r--r--tools/nimgrep.nim18
1 files changed, 12 insertions, 6 deletions
diff --git a/tools/nimgrep.nim b/tools/nimgrep.nim
index e9c1b26fa..8dc076ad9 100644
--- a/tools/nimgrep.nim
+++ b/tools/nimgrep.nim
@@ -33,6 +33,7 @@ Options:
   --ignoreStyle, -y   be style insensitive
   --ext:EX1|EX2|...   only search the files with the given extension(s)
   --nocolor           output will be given without any colours.
+  --oneline           show file on each matched line
   --verbose           be verbose: list every processed file
   --help, -h          shows this help
   --version, -v       shows the version
@@ -56,6 +57,7 @@ var
   extensions: seq[string] = @[]
   options: TOptions = {optRegex}
   useWriteStyled = true
+  oneline = false
 
 proc ask(msg: string): string =
   stdout.write(msg)
@@ -103,9 +105,12 @@ proc writeColored(s: string) =
     stdout.write(s)
 
 proc highlight(s, match, repl: string, t: tuple[first, last: int],
-               line: int, showRepl: bool) =
+               filename:string, line: int, showRepl: bool) =
   const alignment = 6
-  stdout.write(line.`$`.align(alignment), ": ")
+  if oneline:
+    stdout.write(filename, ":", line, ": ")
+  else:
+    stdout.write(line.`$`.align(alignment), ": ")
   var x = beforePattern(s, t.first)
   var y = afterPattern(s, t.last)
   for i in x .. t.first-1: stdout.write(s[i])
@@ -124,7 +129,7 @@ proc highlight(s, match, repl: string, t: tuple[first, last: int],
 proc processFile(pattern; filename: string) =
   var filenameShown = false
   template beforeHighlight =
-    if not filenameShown and optVerbose notin options:
+    if not filenameShown and optVerbose notin options and not oneline:
       stdout.writeLine(filename)
       stdout.flushFile()
       filenameShown = true
@@ -157,11 +162,11 @@ proc processFile(pattern; filename: string) =
 
     beforeHighlight()
     if optReplace notin options:
-      highlight(buffer, wholeMatch, "", t, line, showRepl=false)
+      highlight(buffer, wholeMatch, "", t, filename, line, showRepl=false)
     else:
       let r = replace(wholeMatch, pattern, replacement % matches)
       if optConfirm in options:
-        highlight(buffer, wholeMatch, r, t, line, showRepl=true)
+        highlight(buffer, wholeMatch, r, t, filename, line, showRepl=true)
         case confirm()
         of ceAbort: quit(0)
         of ceYes: reallyReplace = true
@@ -174,7 +179,7 @@ proc processFile(pattern; filename: string) =
           reallyReplace = false
           options.excl(optConfirm)
       else:
-        highlight(buffer, wholeMatch, r, t, line, showRepl=reallyReplace)
+        highlight(buffer, wholeMatch, r, t, filename, line, showRepl=reallyReplace)
       if reallyReplace:
         result.add(buffer.substr(i, t.first-1))
         result.add(r)
@@ -286,6 +291,7 @@ for kind, key, val in getopt():
     of "ignorestyle", "y": incl(options, optIgnoreStyle)
     of "ext": extensions.add val.split('|')
     of "nocolor": useWriteStyled = false
+    of "oneline": oneline = true
     of "verbose": incl(options, optVerbose)
     of "help", "h": writeHelp()
     of "version", "v": writeVersion()