diff --git a/README.md b/README.md index 485c11c..7914b69 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ _sortplz_ is a simple file sorter. It takes targeted files, and moves them to su - 2022.10.04.3 -> Added colored output & partially-successful "silent" flag. - 2022.11.04.1 -> Added recursion; added name-string sorting; "silent" issues may be fixed? - 2022.11.13.1 -> Fixes from Nim Discord community. +- 2022.12.13.1 -> Attempted bugfixes ## TODO diff --git a/src/config.nim b/src/config.nim index a0f5b94..8d9a45f 100644 --- a/src/config.nim +++ b/src/config.nim @@ -1,5 +1,5 @@ const pkgTitle* = "sortplz" - pkgVersion* = "2022.11.13.1" + pkgVersion* = "2022.12.13.1" pkgAuthor* = "Michael Adams, unquietwiki@gmail.com" pkgDescription* = "Sort files into subdirectories, based on given criteria." diff --git a/src/sortplz.nim b/src/sortplz.nim index 72d9225..3d92793 100644 --- a/src/sortplz.nim +++ b/src/sortplz.nim @@ -24,37 +24,44 @@ var names: seq[string] silent: bool = false recurse: bool = false + colors: bool = false + +# Unless macro +template unless(a:bool, body: varargs[untyped]): untyped = + if not a: + block: + body # === Function to handle file moves === proc moveThisFile(f: string, newdir: string) = - if not silent: - stdout.styledWriteLine(fgGreen, "Moving file: ", f.unixToNativePath()) + unless silent: + stdout.styledWriteLine(fgGreen, "Moving file: " & f.unixToNativePath()) os.moveFile(f, newdir & os.DirSep & os.lastPathPart(f)) # === Function to handle directory moves === proc dirProc(value: string): string = # Does the source directory exist? - if not os.dirExists(fromdir): - stdout.styledWriteLine(fgRed, "Failed to find source directory: ", fromdir) + unless os.dirExists(fromdir): + stdout.styledWriteLine(fgRed, "Failed to find source directory: " & fromdir) quit(1) # Does the destination directory exist? var newdir: string = os.joinPath(todir, value) - if not silent: - stdout.styledWriteLine(fgYellow, "Creating directory: ", newdir) + unless silent: + stdout.styledWriteLine(fgYellow, "Creating directory: " & newdir) os.createDir(newdir) # Return newdir as a value for the calling procs to use - if not silent: - stdout.styledWriteLine(fgCyan, "Moving files to ", newdir) + unless silent: + stdout.styledWriteLine(fgCyan, "Moving files to " & newdir) return newdir # === Function to process & sort directory by name-strings === proc processDirNameString(ns: string) = # prepare the destination directory - if not silent: - stdout.styledWriteLine(fgCyan, "Processing name-string: ", ns) + unless silent: + stdout.styledWriteLine(fgCyan, "Processing name-string: " & ns) var newdir = dirProc(ns) # Process the list of files in the source directory @@ -70,8 +77,8 @@ proc processDirNameString(ns: string) = proc processDirExt(ext: string) = # prepare the destination directory - if not silent: - stdout.styledWriteLine(fgCyan, "Processing extension: ", ext) + unless silent: + stdout.styledWriteLine(fgCyan, "Processing extension: " & ext) var newdir = dirProc(ext) # Process the list of files in the source directory @@ -86,9 +93,9 @@ proc processDirExt(ext: string) = # === Functions to display command line information === proc writeVersion() = stdout.styledWriteLine(fg8Bit, "==============================================================") - stdout.styledWriteLine(fgBlue, name, " ", version) + stdout.styledWriteLine(fgBlue, name & " " & version) stdout.styledWriteLine(fgMagenta, description) - stdout.styledWriteLine(fgCyan, "Maintainer(s): ", author) + stdout.styledWriteLine(fgCyan, "Maintainer(s): " & author) stdout.styledWriteLine(fg8Bit, "==============================================================") proc writeHelp() = @@ -104,9 +111,13 @@ for kind, key, val in getopt(): case key of "silent", "s": silent = true + of "colors", "c": + # TODO: we need a template / macro to use this + colors = true of "recurse", "r": recurse = true - stdout.styledWriteLine(fgRed, "Recursion enabled!") + unless silent: + stdout.styledWriteLine(fgRed, "Recursion enabled!") of "help", "h": writeHelp() quit(0) @@ -114,21 +125,21 @@ for kind, key, val in getopt(): writeVersion() quit(0) of "ext", "e": - if not silent: - stdout.styledWriteLine(fgBlue, "Loading extension: ", val) - exts.add(val) + unless silent: + stdout.styledWriteLine(fgBlue, "Loading extension: " & val) + exts.add(val) of "name", "n": - if not silent: - stdout.styledWriteLine(fgBlue, "Considering name-string: ", val) - names.add(val) + unless silent: + stdout.styledWriteLine(fgBlue, "Considering name-string: " & val) + names.add(val) of "fromdir", "f": if val.len > 0: fromdir = val - if not silent: - stdout.styledWriteLine(fgCyan, "Source directory: ", fromdir) + unless silent: + stdout.styledWriteLine(fgCyan, "Source directory: " & fromdir) of "todir", "t": if val.len > 0: todir = val - if not silent: - stdout.styledWriteLine(fgYellow, "Destination directory: ", todir) + unless silent: + stdout.styledWriteLine(fgYellow, "Destination directory: " & todir) of cmdArgument: # TODO: there should be a list of extensions, vs -e discard