Commit 30d59f06f4
30d59f06f46ed48d9de1c375cd285f59f46a4747
parent: 3fed2f0b63
Verified · cmc
cmc <hello@cleberg.net> · 2025-06-04 18:48 UTC
feat: add nim examples
Layout: unified · split
nim/colordiff/colordiff.nim
added
+47
| @@ -0,0 +1,47 @@ |
| 1 | import os, strutils, terminal |
| 2 | |
| 3 | proc readLines(path: string): seq[string] = |
| 4 | result = @[] |
| 5 | for line in lines(path): |
| 6 | result.add(line) |
| 7 | |
| 8 | proc 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 | |
| 43 | when isMainModule: |
| 44 | if paramCount() != 2: |
| 45 | echo "Usage: colordiff <file1> <file2>" |
| 46 | quit(1) |
| 47 | diffFiles(paramStr(1), paramStr(2)) |
| \ No newline at end of file |
nim/colordiff/file1.txt
added
+6
| @@ -0,0 +1,6 @@ |
| 1 | Line one |
| 2 | Line two |
| 3 | Line three |
| 4 | Line four |
| 5 | Line six |
| 6 | Line eight |
| \ No newline at end of file |
nim/colordiff/file2.txt
added
+7
| @@ -0,0 +1,7 @@ |
| 1 | Line one |
| 2 | Line 2 |
| 3 | Line three |
| 4 | Line five |
| 5 | Line six |
| 6 | Line seven |
| 7 | Line eight |
| \ No newline at end of file |
nim/hello_world.nim
added
+5
| @@ -0,0 +1,5 @@ |
| 1 | # to build: |
| 2 | # nim c -d:release hello_world.nim |
| 3 | # ./hello_world.nim |
| 4 | |
| 5 | echo "Hello from Nim!" |