all repos — fae @ 398bf2b834381ff3c0606ebb6259b711f573808d

A minuscule find and edit utility.

fae.nim

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
import 
  packages/nim-sgregex/sgregex,
  std/exitprocs,
  parseopt,
  os,
  terminal,
  strutils,
  times

type
  StringBounds = array[0..1, int]
  StringMatches = object
    start: int
    finish: int
    captures: seq[string]
  FaeOptions = object
    regex: string
    insensitive: bool
    recursive: bool
    filter: string
    substitute: string
    directory: string
    apply: bool
    test: bool
    silent: bool

addExitProc(resetAttributes)

const version = "1.1.0"

const usage = """FAE v""" & version & """ - Find & Edit Utility
  (c) 2015-2020 Fabio Cevasco

  Usage:
    fae <pattern> <replacement> [option1 option2 ...]

  Where:
    <pattern>           A regular expression to search for
    <replacement>      An optional replacement string

  Options:
    -a, --apply         Substitute all occurrences of <pattern> with <replacement> in all files
                        without asking for confirmation.
    -d, --directory     Search in the specified directory (default: .)
    -f, --filter        Specify a regular expression to filter file paths.
    -h, --help          Display this message.
    -i, --insensitive   Case-insensitive matching.
    -r, --recursive     Search directories recursively.
    -s, --silent        Do not display matches.
    -t, --test          Do not perform substitutions, just print results.
    -v, --version       Display the program version.
"""

proc flags(options: FaeOptions): string = 
  if options.insensitive:
    "i"
  else:
    ""
    
proc matchBounds(str, expr: string, start = 0, options: FaeOptions): StringBounds = 
  if start > str.len-2:
    return [-1, -1]
  let s = str.substr(start)
  let c = s.search(expr, options.flags)
  if c[0].len > 0:
    let match = c[0]
    let mstart = strutils.find(s, match)
    if mstart < 0:
      return [-1, -1]
    let mfinish = mstart + match.len-1
    result = [mstart+start, mfinish+start]
  else:
    result = [-1, -1]

proc matchCaptures(str, expr: string, start = 0, options: FaeOptions): StringMatches =
  let s = str.substr(start)
  let c = s.search(expr, options.flags)
  let match = c.len 
  result = StringMatches(start: match-c[0].len+start, finish: match-1+start, captures: c)

proc matchBoundsRec(str, regex: string, start = 0, matches: var seq[StringBounds], options: FaeOptions) =
  let match = str.matchBounds(regex, start, options)
  if match[0] >= 0:
    matches.add(match)
    matchBoundsRec(str, regex, match[1]+1, matches, options)

proc rawReplace(str: var string, sub: string, start, finish: int) =
  str.delete(start, finish)
  str.insert(sub, start)


proc replace(str, regex: string, substitute: var string, start = 0, options: FaeOptions): string =
  return sgregex.replace(str, regex, substitute, options.flags)

#proc old_replace(str, regex: string, substitute: var string, start = 0): string =
#  var newstr = str
#  let match = str.matchCaptures(regex, start)
#  if match.finish >= 0:
#    for i in 1..9:
#      # Substitute captures
#      var submatches = newSeq[StringBounds](0)
#      substitute.matchBoundsRec("\\\\" & $i, 0, submatches)
#      for submatch in submatches:
#        var capture = match.captures[i]
#        if capture.len > 0:
#          substitute.rawReplace(substr(capture, 0, (capture.len-1).int), submatch[0], submatch[1])
#    newstr.rawReplace(substitute, match.start, match.finish)
#  return newstr

proc displayMatch(str: string, start, finish: int, color = fgYellow, lineN: int, silent = false) =
  if silent:
    return
  let max_extra_chars = 20
  let context_start = max(start-max_extra_chars, 0)
  let context_finish = min(finish+max_extra_chars, str.len)
  let match: string = str.substr(start, finish)
  var context: string = str.substr(context_start, context_finish)
  if context_start > 2:
    context = "..." & context
  if context_finish < str.len + 3:
    context = context & "..."
  let match_context_start:int = strutils.find(context, match, start-context_start)
  let match_context_finish:int = match_context_start+match.len
  #let lineN = $str.countLines(0, finish+1)
  stdout.write(" ")
  setForegroundColor(color, true)
  #for i in 0..lineN:
  stdout.write(lineN)
  resetAttributes()
  stdout.write(": ")
  #context = strutils.replace(context, "\n", " ")
  for i in 0 .. (context.len):
    if i == match_context_start:
      setForegroundColor(color, true)
    if i < context.len:
      stdout.write(context[i])
    if i == match_context_finish:
      resetAttributes()
  stdout.write("\p")

proc displayFile(str: string, silent = false) =
  if silent:
    return
  stdout.write "["
  setForegroundColor(fgYellow, true)
  for i in 0..str.len-1:
    stdout.write(str[i])
  resetAttributes()
  stdout.write "]"

proc confirm(msg: string): bool = 
  stdout.write(msg)
  var answer = stdin.readLine()
  if answer.match("y(es)?", "i"):
    return true
  elif answer.match("n(o)?", "i"):
    return false
  else:
    return confirm(msg)

proc processFile(f:string, options: FaeOptions): array[0..1, int] =
  var matchesN = 0
  var subsN = 0
  var contents = ""
  var contentsLen = 0
  var lineN = 0
  var fileLines = newSeq[string]()
  var hasSubstitutions = false
  var file: File
  if not file.open(f):
    raise newException(IOError, "Unable to open file '$1'" % f)
  while file.readline(contents):
    lineN.inc
    contentsLen = contents.len
    fileLines.add contents
    var match = matchBounds(contents, options.regex, 0, options)
    var matchstart, matchend: int
    var offset = 0
    while match[0] >= 0:
      matchesN.inc
      matchstart = match[0] 
      matchend = match[1] 
      if options.substitute != "":
        displayFile(f)
        displayMatch(contents, matchstart, matchend, fgRed, lineN)
        var substitute = options.substitute
        var replacement = contents.replace(options.regex, substitute, matchstart, options)
        offset = substitute.len-(matchend-matchstart+1)
        for i in 0..(f.len+1):
          stdout.write(" ")
        displayMatch(replacement, matchstart, matchend+offset, fgYellow, lineN)
        if (options.apply or confirm("Confirm replacement? [y/n] ")):
          hasSubstitutions = true
          subsN.inc
          contents = replacement
          fileLines[fileLines.high] = replacement
      else:
        displayFile(f, silent = options.silent)
        displayMatch(contents, matchstart, matchend, fgYellow, lineN, silent = options.silent)
      match = matchBounds(contents, options.regex, matchend+offset+1, options)
  file.close()
  if (not options.test) and (options.substitute != "") and hasSubstitutions: 
    f.writefile(fileLines.join("\p"))
  return [matchesN, subsN]

## MAIN

## Processing Options

var duration = cpuTime()

var options = FaeOptions(regex: "", insensitive: false, recursive: false, filter: "", substitute: "", directory: ".", apply: false, test: false, silent: false)

for kind, key, val in getOpt():
  case kind:
    of cmdArgument:
      if options.regex == "":
        options.regex = key
      elif options.substitute == "":
        options.substitute = key
      elif options.regex == "" and options.substitute == "":
        quit("Too many arguments", 1)
    of cmdLongOption, cmdShortOption:
      case key:
        of "recursive", "r":
          options.recursive = true
        of "filter", "f":
          options.filter = val
        of "directory", "d":
          options.directory = val
        of "apply", "a":
          options.apply = true
        of "test", "t":
          options.test = true
        of "help", "h":
          echo usage
          quit(0)
        of "version", "v":
          echo version
          quit(0)
        of "insensitive", "i":
          options.insensitive = true
        of "silent", "s":
          options.silent = true
        else:
          discard
    else:
      discard

if options.regex == "":
  echo usage
  quit(0)

## Processing

var count = 0
var matchesN = 0
var subsN = 0
var res: array[0..1, int]

if options.recursive:
  for f in walkDirRec(options.directory):
    if options.filter == "" or f.match(options.filter):
      try:
        count.inc
        res = processFile(f, options)
        matchesN = matchesN + res[0]
        subsN = subsN + res[1]
      except:
        stderr.writeLine getCurrentExceptionMsg()
        continue
else:
  for kind, f in walkDir(options.directory):
    if kind == pcFile and (options.filter == "" or f.match(options.filter)):
      try:
        count.inc
        res = processFile(f, options)
        matchesN = matchesN + res[0]
        subsN = subsN + res[1]
      except:
        echo matchesN, "-------"
        stderr.writeLine getCurrentExceptionMsg()
        continue
if options.substitute != "":
  echo "=== ", count, " files processed - ", matchesN, " matches, ", subsN, " substitutions (", (cpuTime()-duration), " seconds)."
else:
  echo "=== ", count, " files processed - ", matchesN, " matches (", (cpuTime()-duration), " seconds)."