summaryrefslogtreecommitdiff
path: root/nim/colordiff/colordiff.nim
blob: 69920d49787749a17be3b6e7dcc1b1e0f6401b35 (plain) (blame)
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
import os, strutils, terminal

proc readLines(path: string): seq[string] =
  result = @[]
  for line in lines(path):
    result.add(line)

proc diffFiles(fileA, fileB: string) =
  let
    linesA = readLines(fileA)
    linesB = readLines(fileB)

  var
    i = 0
    j = 0

  while i < linesA.len or j < linesB.len:
    if i < linesA.len and j < linesB.len:
      if linesA[i] == linesB[j]:
        echo linesA[i]  # identical, no color
        i.inc
        j.inc
      else:
        setForegroundColor(fgRed)
        echo "- ", linesA[i]
        resetAttributes()
        setForegroundColor(fgGreen)
        echo "+ ", linesB[j]
        resetAttributes()
        i.inc
        j.inc
    elif i < linesA.len:
      setForegroundColor(fgRed)
      echo "- ", linesA[i]
      resetAttributes()
      i.inc
    elif j < linesB.len:
      setForegroundColor(fgGreen)
      echo "+ ", linesB[j]
      resetAttributes()
      j.inc

when isMainModule:
  if paramCount() != 2:
    echo "Usage: colordiff <file1> <file2>"
    quit(1)
  diffFiles(paramStr(1), paramStr(2))