renamed writeln to writeLine in tools

This commit is contained in:
patrick dw 2015-06-19 01:36:57 -05:00
commit f71eaf66be
3 changed files with 50 additions and 50 deletions

View file

@ -19,9 +19,9 @@ proc process(dir, infile: string, outfile: File,
ig <- (comment / \s+)* """: ig <- (comment / \s+)* """:
# follow the include file: # follow the include file:
if process(dir, matches[0], outfile, processed) == prAddIncludeDir: if process(dir, matches[0], outfile, processed) == prAddIncludeDir:
writeln(outfile, line) writeLine(outfile, line)
else: else:
writeln(outfile, line) writeLine(outfile, line)
proc main(dir, outfile: string) = proc main(dir, outfile: string) =
var o: File var o: File

View file

@ -21,10 +21,10 @@ Options:
--find, -f find the pattern (default) --find, -f find the pattern (default)
--replace, -r replace the pattern --replace, -r replace the pattern
--peg pattern is a peg --peg pattern is a peg
--re pattern is a regular expression (default); extended --re pattern is a regular expression (default); extended
syntax for the regular expression is always turned on syntax for the regular expression is always turned on
--recursive process directories recursively --recursive process directories recursively
--confirm confirm each occurrence/replacement; there is a chance --confirm confirm each occurrence/replacement; there is a chance
to abort any time without touching the file to abort any time without touching the file
--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)
@ -39,13 +39,13 @@ Options:
""" """
type type
TOption = enum TOption = enum
optFind, optReplace, optPeg, optRegex, optRecursive, optConfirm, optStdin, optFind, optReplace, optPeg, optRegex, optRecursive, optConfirm, optStdin,
optWord, optIgnoreCase, optIgnoreStyle, optVerbose optWord, optIgnoreCase, optIgnoreStyle, optVerbose
TOptions = set[TOption] TOptions = set[TOption]
TConfirmEnum = enum TConfirmEnum = enum
ceAbort, ceYes, ceAll, ceNo, ceNone ceAbort, ceYes, ceAll, ceNo, ceNone
var var
filenames: seq[string] = @[] filenames: seq[string] = @[]
pattern = "" pattern = ""
@ -58,34 +58,34 @@ proc ask(msg: string): string =
stdout.write(msg) stdout.write(msg)
result = stdin.readLine() result = stdin.readLine()
proc confirm: TConfirmEnum = proc confirm: TConfirmEnum =
while true: while true:
case normalize(ask(" [a]bort; [y]es, a[l]l, [n]o, non[e]: ")) case normalize(ask(" [a]bort; [y]es, a[l]l, [n]o, non[e]: "))
of "a", "abort": return ceAbort of "a", "abort": return ceAbort
of "y", "yes": return ceYes of "y", "yes": return ceYes
of "l", "all": return ceAll of "l", "all": return ceAll
of "n", "no": return ceNo of "n", "no": return ceNo
of "e", "none": return ceNone of "e", "none": return ceNone
else: discard else: discard
proc countLines(s: string, first, last: int): int = proc countLines(s: string, first, last: int): int =
var i = first var i = first
while i <= last: while i <= last:
if s[i] == '\13': if s[i] == '\13':
inc result inc result
if i < last and s[i+1] == '\10': inc(i) if i < last and s[i+1] == '\10': inc(i)
elif s[i] == '\10': elif s[i] == '\10':
inc result inc result
inc i inc i
proc beforePattern(s: string, first: int): int = proc beforePattern(s: string, first: int): int =
result = first-1 result = first-1
while result >= 0: while result >= 0:
if s[result] in NewLines: break if s[result] in NewLines: break
dec(result) dec(result)
inc(result) inc(result)
proc afterPattern(s: string, last: int): int = proc afterPattern(s: string, last: int): int =
result = last+1 result = last+1
while result < s.len: while result < s.len:
if s[result] in NewLines: break if s[result] in NewLines: break
@ -99,7 +99,7 @@ proc writeColored(s: string) =
stdout.write(s) 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
stdout.write(line.`$`.align(alignment), ": ") stdout.write(line.`$`.align(alignment), ": ")
var x = beforePattern(s, t.first) var x = beforePattern(s, t.first)
@ -118,17 +118,17 @@ proc highlight(s, match, repl: string, t: tuple[first, last: int],
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.writeln(filename) stdout.writeLine(filename)
filenameShown = true filenameShown = true
var buffer: string var buffer: string
try: try:
buffer = system.readFile(filename) buffer = system.readFile(filename)
except IOError: except IOError:
echo "cannot open file: ", filename echo "cannot open file: ", filename
return return
if optVerbose in options: stdout.writeln(filename) if optVerbose in options: stdout.writeLine(filename)
var pegp: TPeg var pegp: TPeg
var rep: Regex var rep: Regex
var result: string var result: string
@ -140,10 +140,10 @@ proc processFile(filename: string) =
rep = re(pattern) rep = re(pattern)
else: else:
pegp = peg(pattern) pegp = peg(pattern)
if optReplace in options: if optReplace in options:
result = newStringOfCap(buffer.len) result = newStringOfCap(buffer.len)
var line = 1 var line = 1
var i = 0 var i = 0
var matches: array[0..re.MaxSubpatterns-1, string] var matches: array[0..re.MaxSubpatterns-1, string]
@ -157,24 +157,24 @@ proc processFile(filename: string) =
t = findBounds(buffer, rep, matches, i) t = findBounds(buffer, rep, matches, i)
if t.first <= 0: break if t.first <= 0: break
inc(line, countLines(buffer, i, t.first-1)) inc(line, countLines(buffer, i, t.first-1))
var wholeMatch = buffer.substr(t.first, t.last) var wholeMatch = buffer.substr(t.first, t.last)
beforeHighlight() 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:
var r: string var r: string
if optRegex notin options: if optRegex notin options:
r = replace(wholeMatch, pegp, replacement % matches) r = replace(wholeMatch, pegp, replacement % matches)
else: else:
r = replace(wholeMatch, rep, replacement % matches) r = replace(wholeMatch, rep, replacement % matches)
if optConfirm in options: if optConfirm in options:
highlight(buffer, wholeMatch, r, t, line, showRepl=true) highlight(buffer, wholeMatch, r, t, line, showRepl=true)
case confirm() case confirm()
of ceAbort: quit(0) of ceAbort: quit(0)
of ceYes: reallyReplace = true of ceYes: reallyReplace = true
of ceAll: of ceAll:
reallyReplace = true reallyReplace = true
options.excl(optConfirm) options.excl(optConfirm)
of ceNo: of ceNo:
@ -203,11 +203,11 @@ proc processFile(filename: string) =
proc hasRightExt(filename: string, exts: seq[string]): bool = proc hasRightExt(filename: string, exts: seq[string]): bool =
var y = splitFile(filename).ext.substr(1) # skip leading '.' var y = splitFile(filename).ext.substr(1) # skip leading '.'
for x in items(exts): for x in items(exts):
if os.cmpPaths(x, y) == 0: return true if os.cmpPaths(x, y) == 0: return true
proc styleInsensitive(s: string): string = proc styleInsensitive(s: string): string =
template addx: stmt = template addx: stmt =
result.add(s[i]) result.add(s[i])
inc(i) inc(i)
result = "" result = ""
@ -215,7 +215,7 @@ proc styleInsensitive(s: string): string =
var brackets = 0 var brackets = 0
while i < s.len: while i < s.len:
case s[i] case s[i]
of 'A'..'Z', 'a'..'z', '0'..'9': of 'A'..'Z', 'a'..'z', '0'..'9':
addx() addx()
if brackets == 0: result.add("_?") if brackets == 0: result.add("_?")
of '_': of '_':
@ -234,29 +234,29 @@ proc styleInsensitive(s: string): string =
while s[i] != '>' and s[i] != '\0': addx() while s[i] != '>' and s[i] != '\0': addx()
of '\\': of '\\':
addx() addx()
if s[i] in strutils.Digits: if s[i] in strutils.Digits:
while s[i] in strutils.Digits: addx() while s[i] in strutils.Digits: addx()
else: else:
addx() addx()
else: addx() else: addx()
proc walker(dir: string) = proc walker(dir: string) =
for kind, path in walkDir(dir): for kind, path in walkDir(dir):
case kind case kind
of pcFile: of pcFile:
if extensions.len == 0 or path.hasRightExt(extensions): if extensions.len == 0 or path.hasRightExt(extensions):
processFile(path) processFile(path)
of pcDir: of pcDir:
if optRecursive in options: if optRecursive in options:
walker(path) walker(path)
else: discard else: discard
if existsFile(dir): processFile(dir) if existsFile(dir): processFile(dir)
proc writeHelp() = proc writeHelp() =
stdout.write(Usage) stdout.write(Usage)
quit(0) quit(0)
proc writeVersion() = proc writeVersion() =
stdout.write(Version & "\n") stdout.write(Version & "\n")
quit(0) quit(0)
@ -267,9 +267,9 @@ proc checkOptions(subset: TOptions, a, b: string) =
for kind, key, val in getopt(): for kind, key, val in getopt():
case kind case kind
of cmdArgument: of cmdArgument:
if options.contains(optStdin): if options.contains(optStdin):
filenames.add(key) filenames.add(key)
elif pattern.len == 0: elif pattern.len == 0:
pattern = key pattern = key
elif options.contains(optReplace) and replacement.len == 0: elif options.contains(optReplace) and replacement.len == 0:
replacement = key replacement = key
@ -306,7 +306,7 @@ 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")
if optStdin in options: if optStdin in options:
pattern = ask("pattern [ENTER to exit]: ") pattern = ask("pattern [ENTER to exit]: ")
if isNil(pattern) or pattern.len == 0: quit(0) if isNil(pattern) or pattern.len == 0: quit(0)
if optReplace in options: if optReplace in options:
@ -314,18 +314,18 @@ if optStdin in options:
if pattern.len == 0: if pattern.len == 0:
writeHelp() writeHelp()
else: else:
if filenames.len == 0: if filenames.len == 0:
filenames.add(os.getCurrentDir()) filenames.add(os.getCurrentDir())
if optRegex notin options: if optRegex notin options:
if optWord in options: if optWord in options:
pattern = r"(^ / !\letter)(" & pattern & r") !\letter" pattern = r"(^ / !\letter)(" & pattern & r") !\letter"
if optIgnoreStyle in options: if optIgnoreStyle in options:
pattern = "\\y " & pattern pattern = "\\y " & pattern
elif optIgnoreCase in options: elif optIgnoreCase in options:
pattern = "\\i " & pattern pattern = "\\i " & pattern
else: else:
if optIgnoreStyle in options: if optIgnoreStyle in options:
pattern = styleInsensitive(pattern) pattern = styleInsensitive(pattern)
if optWord in options: if optWord in options:
pattern = r"\b (:?" & pattern & r") \b" pattern = r"\b (:?" & pattern & r") \b"

View file

@ -126,10 +126,10 @@ proc parseCmdLine(c: var TConfigData) =
break break
of cmdLongOption, cmdShortOption: of cmdLongOption, cmdShortOption:
case normalize(key) case normalize(key)
of "help", "h": of "help", "h":
stdout.write(usage) stdout.write(usage)
quit(0) quit(0)
of "version", "v": of "version", "v":
stdout.write(version & "\n") stdout.write(version & "\n")
quit(0) quit(0)
of "o", "output": c.outdir = val of "o", "output": c.outdir = val
@ -431,7 +431,7 @@ proc buildWebsite(c: var TConfigData) =
if not existsDir("web/upload"): if not existsDir("web/upload"):
createDir("web/upload") createDir("web/upload")
if open(f, outfile, fmWrite): if open(f, outfile, fmWrite):
writeln(f, generateHTMLPage(c, file, content, rss)) writeLine(f, generateHTMLPage(c, file, content, rss))
close(f) close(f)
else: else:
quit("[Error] cannot write file: " & outfile) quit("[Error] cannot write file: " & outfile)