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:
+ p.next()
+ case p.kind
+ of cmdEnd: break
+ of cmdShortOption, cmdLongOption:
+ stdout.write "unrecognized option: " & p.key & "\n"
+ quit(1)
+ of cmdArgument:
+ if argsSeen == 0:
+ oldFile = p.key
+ argsSeen += 1
+ elif argsSeen == 1:
+ newFile = p.key
+ argsSeen += 1
+ else:
+ argsSeen += 1
+
+ if argsSeen != 2:
+ stdout.write "usage: simplediff [old] [new]\n"
+ quit(1)
+
+ let diffed = stringDiff(readFile(oldFile), readFile(newFile))
+
+ for entry in diffed:
+ case entry.kind
+ of NoChange: discard
+ of Insertion:
+ for line in entry.tokens:
+ setForegroundColor(fgGreen)
+ writeStyled("++++ " & line & "\n", {styleBright})
+ setForegroundColor(fgDefault)
+ of Deletion:
+ for line in entry.tokens:
+ setForegroundColor(fgRed)
+ writeStyled("---- " & line & "\n", {styleBright})
+ setForegroundColor(fgDefault)