2ba881379ab251a69443745e30c2c91b471edcd7 — Michael Rees 3 years ago 1a6f74e
Add example diff application
2 files changed, 47 insertions(+), 0 deletions(-)

M src/simplediff.nim
M README.md => README.md +2 -0
@@ 31,6 31,8 @@ for diff in stringDiff("the word is blue", "the word is red", seps={' '}):

Other convenience wrappers may be added in the future! Feel free to request one or submit a patch.

See the `when isMainModule` block within `simplediff.nim` for an example of using these functions to build a simple diff application.

## Contributing

Contributions are welcome! Please send patches, questions, requests, etc. to my [public inbox](mailto:~reesmichael1/public-inbox@lists.sr.ht).

M src/simplediff.nim => src/simplediff.nim +45 -0
@@ 71,3 71,48 @@ proc stringDiff*(s1, s2: string, seps: set[char] = Newlines): seq[Diff[string]] 
  ## Each entry of the returned seq is an instruction describing the
  ## shortest method of changing s1 into s2.
  return diff(split(s1, seps = seps), split(s2, seps = seps))

when isMainModule:
  import parseopt
  import terminal

  var p = initOptParser()
  var oldFile, newFile: string
  var argsSeen = 0
  while true:
    case p.kind
    of cmdEnd: break
    of cmdShortOption, cmdLongOption:
      stdout.write "unrecognized option: " & p.key & "\n"
    of cmdArgument:
      if argsSeen == 0:
        oldFile = p.key
        argsSeen += 1
      elif argsSeen == 1:
        newFile = p.key
        argsSeen += 1
        argsSeen += 1

  if argsSeen != 2:
    stdout.write "usage: simplediff [old] [new]\n"

  let diffed = stringDiff(readFile(oldFile), readFile(newFile))

  for entry in diffed:
    case entry.kind
    of NoChange: discard
    of Insertion:
      for line in entry.tokens:
        writeStyled("++++ " & line & "\n", {styleBright})
    of Deletion:
      for line in entry.tokens:
        writeStyled("---- " & line & "\n", {styleBright})