Added support for filtered thargets.
h3rald h3rald@h3rald.com
Mon, 11 Oct 2021 07:54:23 +0200
1 files changed,
18 insertions(+),
13 deletions(-)
jump to
M
src/pls.nim
→
src/pls.nim
@@ -17,13 +17,15 @@ (c) 2021 $4
Usage: pls <action> [<thing>] Executes <action> (on <thing>). + <thing> can contain a start and/or leading * to perform + simple searches. Options: --help, -h Displays this message. --actions, -a[:<query>] Displays all known actions, optionally matching <query>. - <query> can contain start and/or leading * for simple searches. + <query> can contain a start and/or leading * for simple searches. --things, -t[:<query>] Displays all known things, optionally matching <query> - <query> can contain start and/or leading * for simple searches. + <query> can contain a start and/or leading * for simple searches. --full, -f If -a or -t are specified, display all properties for each action or thing. --version, -v Displays the version of the application.@@ -195,7 +197,8 @@ cmd = cmd.replace(PEG_PLACEHOLDER) do (m: int, n: int, c: openArray[string]) -> string:
return resolvePlaceholder(c[0], thing) result = execShellCmd cmd -proc show*(t, query="", full = false): void = +proc filterItems*(t: string, query=""): seq[string] = + result = newSeq[string]() var endsWith = false var startsWith = false var contains = false@@ -218,11 +221,15 @@ return true
else: return str == q for s, props in DATA[t].pairs: - if not s.filter(): - continue + if s.filter(): + result.add(s) + +proc show*(t: string, query="", full = false): void = + let keys = filterItems(t, query) + for s in keys: if OPT_FULL: echo "\n$1:" % s - for key, val in props.pairs: + for key, val in DATA[t][s].pairs: echo " $1: $2" % [key, val] else: echo "- $1" % s@@ -271,13 +278,11 @@ else:
echo USAGE quit(0) elif ARGS.len < 2: - if DATA["things"].len == 0: - echo "(!) No targets defined - nothing to do." - quit(0) for key in DATA["things"].keys: execute(ARGS[0], key) else: - try: - execute(ARGS[0], ARGS[1]) - except: - echo "(!) " & getCurrentExceptionMsg() + for thing in filterItems("things", ARGS[1]): + try: + execute(ARGS[0], thing) + except: + echo "(!) " & getCurrentExceptionMsg()