Skip to content

Commit

Permalink
attempted bugfixes & new template
Browse files Browse the repository at this point in the history
  • Loading branch information
unquietwiki committed Dec 14, 2022
1 parent 1f310d2 commit a496c9b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 26 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/config.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const
pkgTitle* = "sortplz"
pkgVersion* = "2022.11.13.1"
pkgVersion* = "2022.12.13.1"
pkgAuthor* = "Michael Adams, [email protected]"
pkgDescription* = "Sort files into subdirectories, based on given criteria."
61 changes: 36 additions & 25 deletions src/sortplz.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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() =
Expand All @@ -104,31 +111,35 @@ 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)
of "version", "v":
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
Expand Down

0 comments on commit a496c9b

Please sign in to comment.