nimgrep --ext support

This commit is contained in:
Araq 2011-02-02 23:03:42 +01:00
commit 1eba1063bd
3 changed files with 33 additions and 8 deletions

View file

@ -1165,7 +1165,7 @@ when defined(macosx):
proc getExecPath2(c: cstring, size: var int32): bool {. proc getExecPath2(c: cstring, size: var int32): bool {.
importc: "_NSGetExecutablePath", header: "<mach-o/dyld.h>".} importc: "_NSGetExecutablePath", header: "<mach-o/dyld.h>".}
proc getApplicationFilename*(): string {.rtl, extern: "nos$1".} = proc getAppFilename*(): string {.rtl, extern: "nos$1".} =
## Returns the filename of the application's executable. ## Returns the filename of the application's executable.
# Linux: /proc/<pid>/exe # Linux: /proc/<pid>/exe
@ -1202,9 +1202,21 @@ proc getApplicationFilename*(): string {.rtl, extern: "nos$1".} =
var x = joinPath(p, result) var x = joinPath(p, result)
if ExistsFile(x): return x if ExistsFile(x): return x
proc getApplicationDir*(): string {.rtl, extern: "nos$1".} = proc getApplicationFilename*(): string {.rtl, extern: "nos$1", deprecated.} =
## Returns the filename of the application's executable.
## **Deprecated since version 0.8.12**: use ``getAppFilename``
## instead.
result = getAppFilename()
proc getApplicationDir*(): string {.rtl, extern: "nos$1", deprecated.} =
## Returns the directory of the application's executable. ## Returns the directory of the application's executable.
result = splitFile(getApplicationFilename()).dir ## **Deprecated since version 0.8.12**: use ``getAppDir``
## instead.
result = splitFile(getAppFilename()).dir
proc getAppDir*(): string {.rtl, extern: "nos$1".} =
## Returns the directory of the application's executable.
result = splitFile(getAppFilename()).dir
proc sleep*(milsecs: int) {.rtl, extern: "nos$1".} = proc sleep*(milsecs: int) {.rtl, extern: "nos$1".} =
## sleeps `milsecs` milliseconds. ## sleeps `milsecs` milliseconds.

View file

@ -16,7 +16,7 @@ const
(c) 2011 Andreas Rumpf (c) 2011 Andreas Rumpf
Usage: Usage:
nimgrep [options] [pattern] (file/directory)* nimgrep [options] [pattern] [replacement] (file/directory)*
Options: Options:
--find, -f find the pattern (default) --find, -f find the pattern (default)
--replace, -r replace the pattern --replace, -r replace the pattern
@ -29,8 +29,9 @@ Options:
--stdin read pattern from stdin (to avoid the shell's confusing --stdin read pattern from stdin (to avoid the shell's confusing
quoting rules) quoting rules)
--word, -w the pattern should have word boundaries --word, -w the pattern should have word boundaries
--ignore_case, -i be case insensitive --ignoreCase, -i be case insensitive
--ignore_style, -y be style insensitive --ignoreStyle, -y be style insensitive
--ext:EX1|EX2|... only search the files with the given extension(s)
--help, -h shows this help --help, -h shows this help
--version, -v shows the version --version, -v shows the version
""" """
@ -47,6 +48,7 @@ var
filenames: seq[string] = @[] filenames: seq[string] = @[]
pattern = "" pattern = ""
replacement = "" replacement = ""
extensions: seq[string] = @[]
options: TOptions options: TOptions
proc ask(msg: string): string = proc ask(msg: string): string =
@ -180,6 +182,10 @@ proc processFile(filename: string) =
else: else:
quit "cannot open file for overwriting: " & filename quit "cannot open file for overwriting: " & filename
proc hasRightExt(filename: string, exts: seq[string]): bool =
var y = splitFile(filename).ext.copy(1) # skip leading '.'
for x in items(exts):
if os.cmpPaths(x, y) == 0: return true
proc walker(dir: string) = proc walker(dir: string) =
var isDir = false var isDir = false
@ -187,6 +193,7 @@ proc walker(dir: string) =
isDir = true isDir = true
case kind case kind
of pcFile: of pcFile:
if extensions.len == 0 or path.hasRightExt(extensions):
processFile(path) processFile(path)
of pcDir: of pcDir:
if optRecursive in options: if optRecursive in options:
@ -224,6 +231,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 "help", "h": writeHelp() of "help", "h": writeHelp()
of "version", "v": writeVersion() of "version", "v": writeVersion()
else: writeHelp() else: writeHelp()
@ -243,7 +251,8 @@ if optStdin in options:
if pattern.len == 0: if pattern.len == 0:
writeHelp() writeHelp()
else: else:
if filenames.len == 0: filenames.add(os.getCurrentDir()) if filenames.len == 0:
filenames.add(os.getCurrentDir())
if optRegex notin options: if optRegex notin options:
if optIgnoreStyle in options: if optIgnoreStyle in options:
pattern = "\\y " & pattern pattern = "\\y " & pattern

View file

@ -28,6 +28,10 @@ Changes affecting backwards compatibility
- Operators starting with ``^`` are now right-associative and have the highest - Operators starting with ``^`` are now right-associative and have the highest
priority. priority.
- Deprecated ``os.getApplicationFilename``: Use ``os.getAppFilename``
instead.
- Deprecated ``os.getApplicationDir``: Use ``os.getAppDir``
instead.
Additions Additions