added terminal.isatty; nimgrep version 0.9

This commit is contained in:
Araq
2012-04-06 21:26:48 +02:00
parent 0f96e72b18
commit ba3f90cc25
3 changed files with 39 additions and 8 deletions

View File

@@ -1,7 +1,7 @@
#
#
# Nimrod's Runtime Library
# (c) Copyright 2009 Andreas Rumpf
# (c) Copyright 2012 Andreas Rumpf
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
@@ -307,6 +307,17 @@ proc setBackgroundColor*(bg: TBackgroundColor, bright=false) =
gBG = ord(bg)
if bright: inc(gBG, 60)
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:
# These should be private, but there is no yet
@@ -317,7 +328,7 @@ proc styledEchoProcessArg*(style: set[TStyle]) = setStyle style
proc styledEchoProcessArg*(color: TForegroundColor) = setForeGroundColor color
proc styledEchoProcessArg*(color: TBackgroundColor) = setBackGroundColor color
macro styledEcho*(m: stmt): stmt =
macro styledEcho*(m: stmt): stmt =
result = newNimNode(nnkStmtList)
for i in countup(1, m.len - 1):

View File

@@ -11,7 +11,7 @@ import
os, strutils, parseopt, pegs, re, terminal
const
Version = "0.8"
Version = "0.9"
Usage = "nimgrep - Nimrod Grep Utility Version " & version & """
(c) 2012 Andreas Rumpf
@@ -32,6 +32,7 @@ Options:
--ignoreCase, -i be case insensitive
--ignoreStyle, -y be style insensitive
--ext:EX1|EX2|... only search the files with the given extension(s)
--verbose be verbose: list every processed file
--help, -h shows this help
--version, -v shows the version
"""
@@ -39,7 +40,7 @@ Options:
type
TOption = enum
optFind, optReplace, optPeg, optRegex, optRecursive, optConfirm, optStdin,
optWord, optIgnoreCase, optIgnoreStyle
optWord, optIgnoreCase, optIgnoreStyle, optVerbose
TOptions = set[TOption]
TConfirmEnum = enum
ceAbort, ceYes, ceAll, ceNo, ceNone
@@ -50,6 +51,7 @@ var
replacement = ""
extensions: seq[string] = @[]
options: TOptions = {optRegex}
useWriteStyled = true
proc ask(msg: string): string =
stdout.write(msg)
@@ -89,6 +91,12 @@ proc afterPattern(s: string, last: int): int =
inc(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],
line: int, showRepl: bool) =
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 y = afterPattern(s, t.last)
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])
stdout.write("\n")
if showRepl:
stdout.write(repeatChar(alignment-1), "-> ")
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])
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
try:
buffer = system.readFile(filename)
except EIO:
echo "cannot open file: ", filename
return
stdout.writeln(filename)
if optVerbose in options: stdout.writeln(filename)
var pegp: TPeg
var rep: TRegex
var result: string
@@ -145,6 +159,7 @@ proc processFile(filename: string) =
var wholeMatch = buffer.substr(t.first, t.last)
beforeHighlight()
if optReplace notin options:
highlight(buffer, wholeMatch, "", t, line, showRepl=false)
else:
@@ -276,11 +291,15 @@ for kind, key, val in getopt():
of "ignorecase", "i": incl(options, optIgnoreCase)
of "ignorestyle", "y": incl(options, optIgnoreStyle)
of "ext": extensions = val.split('|')
of "verbose": incl(options, optVerbose)
of "help", "h": writeHelp()
of "version", "v": writeVersion()
else: writeHelp()
of cmdEnd: assert(false) # cannot happen
when defined(posix):
useWriteStyled = terminal.isatty(stdout)
checkOptions({optFind, optReplace}, "find", "replace")
checkOptions({optPeg, optRegex}, "peg", "re")
checkOptions({optIgnoreCase, optIgnoreStyle}, "ignore_case", "ignore_style")

View File

@@ -33,6 +33,7 @@ Library Additions
- Added a new OpenGL wrapper that supports OpenGL up to version 4.2.
- Added a wrapper for ``libsvm``.
- Added a wrapper for ``mongodb``.
- Added ``terminal.isatty``.
Changes affecting backwards compatibility