added terminal.isatty; nimgrep version 0.9
This commit is contained in:
parent
0f96e72b18
commit
ba3f90cc25
3 changed files with 39 additions and 8 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
#
|
#
|
||||||
#
|
#
|
||||||
# Nimrod's Runtime Library
|
# Nimrod's Runtime Library
|
||||||
# (c) Copyright 2009 Andreas Rumpf
|
# (c) Copyright 2012 Andreas Rumpf
|
||||||
#
|
#
|
||||||
# See the file "copying.txt", included in this
|
# See the file "copying.txt", included in this
|
||||||
# distribution, for details about the copyright.
|
# distribution, for details about the copyright.
|
||||||
|
|
@ -308,6 +308,17 @@ proc setBackgroundColor*(bg: TBackgroundColor, bright=false) =
|
||||||
if bright: inc(gBG, 60)
|
if bright: inc(gBG, 60)
|
||||||
stdout.write("\e[" & $gBG & 'm')
|
stdout.write("\e[" & $gBG & 'm')
|
||||||
|
|
||||||
|
proc isatty*(f: TFile): bool =
|
||||||
|
## returns true if `f` is associated with a terminal device.
|
||||||
|
when defined(posix):
|
||||||
|
proc isatty(fildes: TFileHandle): cint {.
|
||||||
|
importc: "isatty", header: "<unistd.h>".}
|
||||||
|
else:
|
||||||
|
proc isatty(fildes: TFileHandle): cint {.
|
||||||
|
importc: "_isatty", header: "<io.h>".}
|
||||||
|
|
||||||
|
result = isatty(fileHandle(f)) != 0'i32
|
||||||
|
|
||||||
# XXX:
|
# XXX:
|
||||||
# These should be private, but there is no yet
|
# These should be private, but there is no yet
|
||||||
# facility for binding local symbols within macros
|
# facility for binding local symbols within macros
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ import
|
||||||
os, strutils, parseopt, pegs, re, terminal
|
os, strutils, parseopt, pegs, re, terminal
|
||||||
|
|
||||||
const
|
const
|
||||||
Version = "0.8"
|
Version = "0.9"
|
||||||
Usage = "nimgrep - Nimrod Grep Utility Version " & version & """
|
Usage = "nimgrep - Nimrod Grep Utility Version " & version & """
|
||||||
|
|
||||||
(c) 2012 Andreas Rumpf
|
(c) 2012 Andreas Rumpf
|
||||||
|
|
@ -32,6 +32,7 @@ Options:
|
||||||
--ignoreCase, -i be case insensitive
|
--ignoreCase, -i be case insensitive
|
||||||
--ignoreStyle, -y be style insensitive
|
--ignoreStyle, -y be style insensitive
|
||||||
--ext:EX1|EX2|... only search the files with the given extension(s)
|
--ext:EX1|EX2|... only search the files with the given extension(s)
|
||||||
|
--verbose be verbose: list every processed file
|
||||||
--help, -h shows this help
|
--help, -h shows this help
|
||||||
--version, -v shows the version
|
--version, -v shows the version
|
||||||
"""
|
"""
|
||||||
|
|
@ -39,7 +40,7 @@ Options:
|
||||||
type
|
type
|
||||||
TOption = enum
|
TOption = enum
|
||||||
optFind, optReplace, optPeg, optRegex, optRecursive, optConfirm, optStdin,
|
optFind, optReplace, optPeg, optRegex, optRecursive, optConfirm, optStdin,
|
||||||
optWord, optIgnoreCase, optIgnoreStyle
|
optWord, optIgnoreCase, optIgnoreStyle, optVerbose
|
||||||
TOptions = set[TOption]
|
TOptions = set[TOption]
|
||||||
TConfirmEnum = enum
|
TConfirmEnum = enum
|
||||||
ceAbort, ceYes, ceAll, ceNo, ceNone
|
ceAbort, ceYes, ceAll, ceNo, ceNone
|
||||||
|
|
@ -50,6 +51,7 @@ var
|
||||||
replacement = ""
|
replacement = ""
|
||||||
extensions: seq[string] = @[]
|
extensions: seq[string] = @[]
|
||||||
options: TOptions = {optRegex}
|
options: TOptions = {optRegex}
|
||||||
|
useWriteStyled = true
|
||||||
|
|
||||||
proc ask(msg: string): string =
|
proc ask(msg: string): string =
|
||||||
stdout.write(msg)
|
stdout.write(msg)
|
||||||
|
|
@ -89,6 +91,12 @@ proc afterPattern(s: string, last: int): int =
|
||||||
inc(result)
|
inc(result)
|
||||||
dec(result)
|
dec(result)
|
||||||
|
|
||||||
|
proc writeColored(s: string) =
|
||||||
|
if useWriteStyled:
|
||||||
|
terminal.WriteStyled(s, {styleUnderscore, styleBright})
|
||||||
|
else:
|
||||||
|
stdout.write(s)
|
||||||
|
|
||||||
proc highlight(s, match, repl: string, t: tuple[first, last: int],
|
proc highlight(s, match, repl: string, t: tuple[first, last: int],
|
||||||
line: int, showRepl: bool) =
|
line: int, showRepl: bool) =
|
||||||
const alignment = 6
|
const alignment = 6
|
||||||
|
|
@ -96,24 +104,30 @@ proc highlight(s, match, repl: string, t: tuple[first, last: int],
|
||||||
var x = beforePattern(s, t.first)
|
var x = beforePattern(s, t.first)
|
||||||
var y = afterPattern(s, t.last)
|
var y = afterPattern(s, t.last)
|
||||||
for i in x .. t.first-1: stdout.write(s[i])
|
for i in x .. t.first-1: stdout.write(s[i])
|
||||||
terminal.WriteStyled(match, {styleUnderscore, styleBright})
|
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")
|
||||||
if showRepl:
|
if showRepl:
|
||||||
stdout.write(repeatChar(alignment-1), "-> ")
|
stdout.write(repeatChar(alignment-1), "-> ")
|
||||||
for i in x .. t.first-1: stdout.write(s[i])
|
for i in x .. t.first-1: stdout.write(s[i])
|
||||||
terminal.WriteStyled(repl, {styleUnderscore, styleBright})
|
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")
|
||||||
|
|
||||||
proc processFile(filename: string) =
|
proc processFile(filename: string) =
|
||||||
|
var filenameShown = false
|
||||||
|
template beforeHighlight =
|
||||||
|
if not filenameShown and optVerbose notin options:
|
||||||
|
stdout.writeln(filename)
|
||||||
|
filenameShown = true
|
||||||
|
|
||||||
var buffer: string
|
var buffer: string
|
||||||
try:
|
try:
|
||||||
buffer = system.readFile(filename)
|
buffer = system.readFile(filename)
|
||||||
except EIO:
|
except EIO:
|
||||||
echo "cannot open file: ", filename
|
echo "cannot open file: ", filename
|
||||||
return
|
return
|
||||||
stdout.writeln(filename)
|
if optVerbose in options: stdout.writeln(filename)
|
||||||
var pegp: TPeg
|
var pegp: TPeg
|
||||||
var rep: TRegex
|
var rep: TRegex
|
||||||
var result: string
|
var result: string
|
||||||
|
|
@ -145,6 +159,7 @@ proc processFile(filename: string) =
|
||||||
|
|
||||||
var wholeMatch = buffer.substr(t.first, t.last)
|
var wholeMatch = buffer.substr(t.first, t.last)
|
||||||
|
|
||||||
|
beforeHighlight()
|
||||||
if optReplace notin options:
|
if optReplace notin options:
|
||||||
highlight(buffer, wholeMatch, "", t, line, showRepl=false)
|
highlight(buffer, wholeMatch, "", t, line, showRepl=false)
|
||||||
else:
|
else:
|
||||||
|
|
@ -276,11 +291,15 @@ for kind, key, val in getopt():
|
||||||
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 = val.split('|')
|
||||||
|
of "verbose": incl(options, optVerbose)
|
||||||
of "help", "h": writeHelp()
|
of "help", "h": writeHelp()
|
||||||
of "version", "v": writeVersion()
|
of "version", "v": writeVersion()
|
||||||
else: writeHelp()
|
else: writeHelp()
|
||||||
of cmdEnd: assert(false) # cannot happen
|
of cmdEnd: assert(false) # cannot happen
|
||||||
|
|
||||||
|
when defined(posix):
|
||||||
|
useWriteStyled = terminal.isatty(stdout)
|
||||||
|
|
||||||
checkOptions({optFind, optReplace}, "find", "replace")
|
checkOptions({optFind, optReplace}, "find", "replace")
|
||||||
checkOptions({optPeg, optRegex}, "peg", "re")
|
checkOptions({optPeg, optRegex}, "peg", "re")
|
||||||
checkOptions({optIgnoreCase, optIgnoreStyle}, "ignore_case", "ignore_style")
|
checkOptions({optIgnoreCase, optIgnoreStyle}, "ignore_case", "ignore_style")
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,7 @@ Library Additions
|
||||||
- Added a new OpenGL wrapper that supports OpenGL up to version 4.2.
|
- Added a new OpenGL wrapper that supports OpenGL up to version 4.2.
|
||||||
- Added a wrapper for ``libsvm``.
|
- Added a wrapper for ``libsvm``.
|
||||||
- Added a wrapper for ``mongodb``.
|
- Added a wrapper for ``mongodb``.
|
||||||
|
- Added ``terminal.isatty``.
|
||||||
|
|
||||||
|
|
||||||
Changes affecting backwards compatibility
|
Changes affecting backwards compatibility
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue