From 0bce09952021e83f61c001d16f669148bfd83dda Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Wed, 25 Sep 2024 14:37:59 -0500 Subject: [PATCH] abstractions! --- src/hwylterm/chooser.nim | 32 +++++++++++++++++++++++++----- src/hwylterm/cli.nim | 6 +++--- src/hwylterm/private/tryImport.nim | 6 ++++++ 3 files changed, 36 insertions(+), 8 deletions(-) create mode 100644 src/hwylterm/private/tryImport.nim diff --git a/src/hwylterm/chooser.nim b/src/hwylterm/chooser.nim index f7e0623..ca8d4ca 100644 --- a/src/hwylterm/chooser.nim +++ b/src/hwylterm/chooser.nim @@ -11,11 +11,8 @@ import std/[enumerate, os, strutils, sequtils, sets, terminal] - -template canImport(x): bool = compiles: import x -when not canImport(illwill): {.fatal: "hwylterm/choose requires illwill >= 0.4.1".} -import illwill - +import ./private/tryImport +tryImport(illwill, "hwylterm/choose requires illwill >= 0.4.1") proc exitProc() {.noconv.} = illwillDeInit() @@ -131,6 +128,31 @@ proc choose*[T](things: openArray[T], height: Natural = 6): seq[T] = when isMainModule: + # import std/parseopt + # var + # posArgs: seq[string] + # style: string + # showDebug: bool + # var p = initOptParser() + # for kind, key, val in p.getopt(): + # case kind + # of cmdEnd: + # break + # of cmdShortOption, cmdLongOption: + # case key + # of "help", "h": + # writeHelp() + # of cmdArgument: + # strArgs.add key + # for arg in strArgs: + # let styled = + # if style != "": + # arg.bb(style) + # else: + # arg.bb + # echo styled + # if showDebug: + # echo debug(styled) let items = LowercaseLetters.toSeq() let item = choose(items) echo "selected: ", item diff --git a/src/hwylterm/cli.nim b/src/hwylterm/cli.nim index 4449922..16992c9 100644 --- a/src/hwylterm/cli.nim +++ b/src/hwylterm/cli.nim @@ -3,11 +3,11 @@ Adapter to add hwylterm colors to cligen output. ]## + import std/[tables] import ./bbansi -template canImport(x): bool = compiles: import x -when not canImport(cligen): {.fatal: "hwylterm/cli requires cligen>= 1.7.5".} -import cligen +import ./private/tryImport +tryImport(cligen, "hwylterm/cli requires cligen>= 1.7.5") type diff --git a/src/hwylterm/private/tryImport.nim b/src/hwylterm/private/tryImport.nim new file mode 100644 index 0000000..f8e73c7 --- /dev/null +++ b/src/hwylterm/private/tryImport.nim @@ -0,0 +1,6 @@ +template tryImport*(x, msg) = + const importable = compiles: import x + when not importable: + {.fatal: msg} + else: + import x