nimgrep version 1

This commit is contained in:
Araq 2015-12-05 10:26:47 +01:00
commit 68cbb4d2b4

View file

@ -11,7 +11,7 @@ import
os, strutils, parseopt, pegs, re, terminal os, strutils, parseopt, pegs, re, terminal
const const
Version = "0.9" Version = "1.0"
Usage = "nimgrep - Nim Grep Utility Version " & Version & """ Usage = "nimgrep - Nim Grep Utility Version " & Version & """
(c) 2012 Andreas Rumpf (c) 2012 Andreas Rumpf
@ -56,6 +56,7 @@ var
proc ask(msg: string): string = proc ask(msg: string): string =
stdout.write(msg) stdout.write(msg)
stdout.flushFile()
result = stdin.readLine() result = stdin.readLine()
proc confirm: TConfirmEnum = proc confirm: TConfirmEnum =
@ -108,18 +109,21 @@ proc highlight(s, match, repl: string, t: tuple[first, last: int],
writeColored(match) writeColored(match)
for i in t.last+1 .. y: stdout.write(s[i]) for i in t.last+1 .. y: stdout.write(s[i])
stdout.write("\n") stdout.write("\n")
stdout.flushFile()
if showRepl: if showRepl:
stdout.write(spaces(alignment-1), "-> ") stdout.write(spaces(alignment-1), "-> ")
for i in x .. t.first-1: stdout.write(s[i]) for i in x .. t.first-1: stdout.write(s[i])
writeColored(repl) writeColored(repl)
for i in t.last+1 .. y: stdout.write(s[i]) for i in t.last+1 .. y: stdout.write(s[i])
stdout.write("\n") stdout.write("\n")
stdout.flushFile()
proc processFile(filename: string) = proc processFile(filename: string) =
var filenameShown = false var filenameShown = false
template beforeHighlight = template beforeHighlight =
if not filenameShown and optVerbose notin options: if not filenameShown and optVerbose notin options:
stdout.writeLine(filename) stdout.writeLine(filename)
stdout.flushFile()
filenameShown = true filenameShown = true
var buffer: string var buffer: string
@ -128,7 +132,9 @@ proc processFile(filename: string) =
except IOError: except IOError:
echo "cannot open file: ", filename echo "cannot open file: ", filename
return return
if optVerbose in options: stdout.writeLine(filename) if optVerbose in options:
stdout.writeLine(filename)
stdout.flushFile()
var pegp: TPeg var pegp: TPeg
var rep: Regex var rep: Regex
var result: string var result: string
@ -254,10 +260,12 @@ proc walker(dir: string) =
proc writeHelp() = proc writeHelp() =
stdout.write(Usage) stdout.write(Usage)
stdout.flushFile()
quit(0) quit(0)
proc writeVersion() = proc writeVersion() =
stdout.write(Version & "\n") stdout.write(Version & "\n")
stdout.flushFile()
quit(0) quit(0)
proc checkOptions(subset: TOptions, a, b: string) = proc checkOptions(subset: TOptions, a, b: string) =
@ -291,7 +299,7 @@ for kind, key, val in getopt():
of "word", "w": incl(options, optWord) of "word", "w": incl(options, optWord)
of "ignorecase", "i": incl(options, optIgnoreCase) of "ignorecase", "i": incl(options, optIgnoreCase)
of "ignorestyle", "y": incl(options, optIgnoreStyle) of "ignorestyle", "y": incl(options, optIgnoreStyle)
of "ext": extensions = val.split('|') of "ext": extensions.add val.split('|')
of "nocolor": useWriteStyled = false of "nocolor": useWriteStyled = false
of "verbose": incl(options, optVerbose) of "verbose": incl(options, optVerbose)
of "help", "h": writeHelp() of "help", "h": writeHelp()