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 @@
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))\ No newline at end of file
nim/colordiff/file1.txt added +6
@@ -0,0 +1,6 @@
1Line one
2Line two
3Line three
4Line four
5Line six
6Line eight\ No newline at end of file
nim/colordiff/file2.txt added +7
@@ -0,0 +1,7 @@
1Line one
2Line 2
3Line three
4Line five
5Line six
6Line seven
7Line 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
5echo "Hello from Nim!"