Skip to content

Commit d46a7df

Browse files
committed
chore: replace unmaintained argument parser for a faster (maintained) one; feat: huge speed improvement when converting directories
1 parent 4317abd commit d46a7df

4 files changed

Lines changed: 88 additions & 76 deletions

File tree

dye.nimble

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version = "1.1.4"
1+
version = "1.1.5"
22
author = "Luke"
33
description = "An image colorizer"
44
license = "GPL-3.0-or-later"
@@ -9,7 +9,7 @@ requires "nim >= 1.4.8"
99
requires "progress"
1010
requires "pixie#head"
1111
requires "chroma#head"
12-
requires "commandeer#head"
12+
requires "therapist"
1313
#requires "nimoji"
1414

1515

lib/args

459 KB
Binary file not shown.

lib/args.nim

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import os, strutils, strformat, therapist
2+
3+
const versionNum = staticRead(fmt"../dye.nimble").splitLines()[0].split("=")[1]
4+
.strip().replace("\"", "")
5+
6+
const release = defined(release)
7+
8+
var bType: string
9+
10+
if release:
11+
bType = "release"
12+
else:
13+
bType = "debug"
14+
15+
let list = (
16+
help: newHelpArg()
17+
)
18+
19+
let flip = (
20+
help: newHelpArg(),
21+
bar: newCountArg(@["--bar", "-b"], multi=false, help="Show a progress bar"),
22+
output: newStringArg(@["--output", "-o"], help="The file to write to", optional=true, defaultVal="null"),
23+
file: newPathArg(@["<file>"], help="The file (or folder of files) to convert")
24+
)
25+
26+
let luma = (
27+
help: newHelpArg(),
28+
bar: newCountArg(@["--bar", "-b"], multi=false, help="Show a progress bar"),
29+
output: newStringArg(@["--output", "-o"], help="The file to write to", optional=true, defaultVal="null"),
30+
file: newPathArg(@["<file>"], help="The file (or folder of files) to convert")
31+
)
32+
33+
let args* = (
34+
list: newCommandArg(@["list", "ls"], list, help="List all palettes"),
35+
luma: newCommandArg(@["luma", "l"], luma, help="Invert the luminance of an image"),
36+
flip: newCommandArg(@["flip", "f"], flip, help="Flip the colors of an image"),
37+
bar: newCountArg(@["--bar", "-b"], multi=false, help="Show a progress bar"),
38+
output: newStringArg(@["--output", "-o"], help="The file to write to", optional=true, defaultVal="null"),
39+
file: newPathArg(@["<file>"], help="The file (or folder of files) to convert", optional=true),
40+
palette: newStringArg(@["--palette", "-p"], help="The palette to use"),
41+
version: newCountArg(@["--version", "-v"], multi=false, help="Show dye version information"),
42+
help: newHelpArg()
43+
)
44+
45+
args.parseOrQuit(prolog="Dye. The ultrafast image colorizer", command="dye")
46+
47+
if args.version.seen:
48+
echo "dye v$#\nrelease: $#" % @[versionNum, $release]
49+
quit(0)
50+
51+
export therapist

src/dye.nim

Lines changed: 35 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -5,74 +5,30 @@ import os,
55
strutils,
66
progress,
77
terminal,
8-
commandeer,
98
strformat
109

11-
const versionNum = staticRead(fmt"../dye.nimble").splitLines()[0].split("=")[1]
12-
.strip().replace("\"", "")
10+
include ../lib/args
1311

14-
const release = defined(release)
15-
16-
var bType: string
17-
18-
if release:
19-
bType = "release"
20-
else:
21-
bType = "debug"
22-
23-
commandline:
24-
arguments dyefile, string
25-
option dyebar, bool, "bar", "b"
26-
option dyeoutfile, string, "out", "o", "null"
27-
option palette, string, "palette", "p"
28-
exitoption "v", "version", "dye v$#\nbuild: $#" % @[versionNum, bType]
29-
subcommand flip, "flip", "f":
30-
arguments flipfile, string
31-
option flipoutfile, string, "out", "o", "null"
32-
option flipbar, bool, "bar", "b"
33-
subcommand luma, "luma", "l":
34-
arguments lumafile, string
35-
option lumaoutfile, string, "out", "o", "null"
36-
option lumabar, bool, "bar", "b"
37-
subcommand helpcmd, "help", "h":
38-
option command, string, "command", "c", "null"
39-
subcommand list, "list", "l":
40-
option x, bool, "X", "x", false # This doesnt actually do anything.
41-
42-
discard x
43-
44-
import ../lib/[colors, palettes, help]
45-
46-
if helpcmd:
47-
if command != "null":
48-
for k, v in helps.fieldPairs:
49-
if k == command:
50-
echo v
51-
quit 0
52-
stdout.styledWriteLine(fgRed, "Error: ", fgWhite, "Unknown command: " & command)
53-
quit 0
54-
else:
55-
echo helps.all
56-
quit 0
57-
58-
var outfile: string
59-
var bar: bool
60-
61-
if flip:
62-
outfile = flipoutfile
63-
bar = flipbar
64-
elif luma:
65-
outfile = lumaoutfile
66-
bar = lumabar
67-
else:
68-
outfile = dyeoutfile
69-
bar = dyebar
12+
import ../lib/[colors, palettes]
7013

7114
var
7215
flipName: string
7316
convName: string
7417
lumaName: string
7518

19+
var outfile: string
20+
var fileDir: string
21+
22+
if args.flip.seen:
23+
outfile = flip.output.value
24+
fileDir = flip.file.value
25+
elif args.luma.seen:
26+
outfile = luma.output.value
27+
fileDir = luma.file.value
28+
else:
29+
outfile = args.output.value
30+
fileDir = args.file.value
31+
7632
proc fileName(file: string): void =
7733
if outfile != "null":
7834
flipName = outfile & ".png"
@@ -83,7 +39,12 @@ proc fileName(file: string): void =
8339
convName = "conv-" & getFilename(file) & ".png"
8440
lumaName = "luma-" & getFilename(file) & ".png"
8541

86-
var imgs: seq[string]
42+
var files: seq[string]
43+
if dirExists(fileDir):
44+
for file in walkDir(fileDir):
45+
files.add(file.path)
46+
else:
47+
files.add(fileDir)
8748

8849
proc flipCol(imgPath: string, bar: bool): void =
8950
stdout.styledWriteLine(fgYellow, "Converting: ", fgWhite, splitFile(
@@ -190,39 +151,39 @@ proc col(imgPath: string, bar: bool, colors: seq[string]): void =
190151
stdout.styledWriteLine(fgGreen, "Completed: ", fgWhite, splitFile(
191152
imgPath).name & splitFile(imgPath).ext & "\n")
192153

193-
if flip:
194-
for img in flipfile:
154+
if args.flip.seen:
155+
for img in files:
195156
try:
196-
flipCol(img, bar)
157+
flipCol(img, flip.bar.seen)
197158
except:
198159
stdout.styledWriteLine(fgRed, "Error: ", fgWhite, getCurrentExceptionMsg())
199160
continue
200-
elif luma:
201-
for img in lumafile:
161+
elif args.luma.seen:
162+
for img in files:
202163
try:
203-
lumaCol(img, bar)
164+
lumaCol(img, luma.bar.seen)
204165
except:
205166
stdout.styledWriteLine(fgRed, "Error: ", fgWhite, getCurrentExceptionMsg())
206167
continue
207-
elif list:
168+
elif args.list.seen:
208169
for k, v in pal.fieldPairs:
209170
discard v
210171
echo k
211172
else:
212173
var cols: seq[string]
213-
if "," in palette:
214-
cols = palette.parseColors()
215-
elif fileExists(palette):
216-
cols = readFile(palette).parseColors()
174+
if "," in args.palette.value:
175+
cols = args.palette.value.parseColors()
176+
elif fileExists(args.palette.value):
177+
cols = readFile(args.palette.value).parseColors()
217178
else:
218179
for k, v in pal.fieldPairs:
219-
if palette == k:
180+
if args.palette.value == k:
220181
cols = v
221182
break
222183
cols = cols.rmTag()
223-
for img in dyefile:
184+
for img in files:
224185
try:
225-
col(img, bar, cols)
186+
col(img, args.bar.seen, cols)
226187
except:
227188
stdout.styledWriteLine(fgRed, "Error: ", fgWhite, getCurrentExceptionMsg())
228189
continue

0 commit comments

Comments
 (0)