cmc/learn
My personal learning area.
clone: git clone https://gitbay.org/cmc/learn.git
main: nim/colordiff/colordiff.nim · raw
1import os, strutils, terminal
2
3proc readLines(path: string): seq[string] =
4 result = @[]
5 for line in lines(path):
6 result.add(line)
7
8proc diffFiles(fileA, fileB: string) =
9 let
10 linesA = readLines(fileA)
11 linesB = readLines(fileB)
12
13 var
14 i = 0
15 j = 0
16
17 while i < linesA.len or j < linesB.len:
18 if i < linesA.len and j < linesB.len:
19 if linesA[i] == linesB[j]:
20 echo linesA[i] # identical, no color
21 i.inc
22 j.inc
23 else:
24 setForegroundColor(fgRed)
25 echo "- ", linesA[i]
26 resetAttributes()
27 setForegroundColor(fgGreen)
28 echo "+ ", linesB[j]
29 resetAttributes()
30 i.inc
31 j.inc
32 elif i < linesA.len:
33 setForegroundColor(fgRed)
34 echo "- ", linesA[i]
35 resetAttributes()
36 i.inc
37 elif j < linesB.len:
38 setForegroundColor(fgGreen)
39 echo "+ ", linesB[j]
40 resetAttributes()
41 j.inc
42
43when isMainModule:
44 if paramCount() != 2:
45 echo "Usage: colordiff <file1> <file2>"
46 quit(1)
47 diffFiles(paramStr(1), paramStr(2))