From 07dd5a5238e0ddc67b24772be7b1f742693b4535 Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Mon, 30 Sep 2024 18:13:55 -0500 Subject: [PATCH] refactor: drop cligen and update hwylterm --- nimble.lock | 14 ++----------- nix/flake.nix | 2 +- src/project.nim | 4 ++-- src/selector.nim | 9 ++++++-- src/tsm.nim | 54 +++++++++++++++++++++++++++++++++++------------- tsm.nimble | 1 - 6 files changed, 52 insertions(+), 32 deletions(-) diff --git a/nimble.lock b/nimble.lock index d356842..43aae3e 100644 --- a/nimble.lock +++ b/nimble.lock @@ -1,24 +1,14 @@ { "version": 2, "packages": { - "cligen": { - "version": "1.7.5", - "vcsRevision": "85daafd76b0104387f50ff3f75c829b4c2804412", - "url": "https://github.com/c-blake/cligen.git", - "downloadMethod": "git", - "dependencies": [], - "checksums": { - "sha1": "f3ffe7329c8db755677d3ca377d02ff176cec8b1" - } - }, "hwylterm": { "version": "0.1.0", - "vcsRevision": "c2bcfd1f73dda97bd3e209c89e2abfe588f0977c", + "vcsRevision": "337083afe13bf0f1bd024b870c2174a901e8ce06", "url": "https://github.com/daylinmorgan/hwylterm", "downloadMethod": "git", "dependencies": [], "checksums": { - "sha1": "1932229840c893c39acca0b50def19913678f5f0" + "sha1": "a995a81f5e4af2991c79ed04b02f17d47dd509e0" } }, "illwill": { diff --git a/nix/flake.nix b/nix/flake.nix index 4325391..d12c908 100644 --- a/nix/flake.nix +++ b/nix/flake.nix @@ -43,7 +43,7 @@ pname = "tsm"; version = "2024.1001-unstable"; src = ../.; - nimbleDepsHash = "sha256-8noTwYwtaPaF9iGq4EZhWMi709l9e66CJn8vm4aIyO4="; + nimbleDepsHash = "sha256-YtW7jv2D/o6z6vgx8cKfqGzs/Yyd6s/Cl5Xs7yavA18="; nimFlags = [ "-d:TsmVersion=v${version}" ]; diff --git a/src/project.nim b/src/project.nim index a586a4b..c446cd6 100644 --- a/src/project.nim +++ b/src/project.nim @@ -10,7 +10,7 @@ type open*: bool matched*: bool -proc pathToName(path: string): string = +proc pathToName*(path: string): string = splitPath(path)[1].replace(".", "_") proc newProject*(path: string, open: bool, name = "", named: bool = false): Project = @@ -19,7 +19,7 @@ proc newProject*(path: string, open: bool, name = "", named: bool = false): Proj if name != "": name else: - path.pathToName() + pathToName(path) result.updated = getLastModificationTime(path) result.open = open result.named = named diff --git a/src/selector.nim b/src/selector.nim index 858b86c..b41104e 100644 --- a/src/selector.nim +++ b/src/selector.nim @@ -1,4 +1,4 @@ -import std/[enumerate, os, strformat, strutils, terminal, sequtils] +import std/[enumerate, os, strformat, strutils, terminal] from illwill import illwillDeinit, illwillInit, getKey, Key import term, project @@ -44,7 +44,12 @@ func addDivider(b: var Buffer) = b.addLine "─".repeat(b.width - 2) proc addInput(b: var Buffer) = - b.addLine "$ " & state.input + var line = "$ " + if state.input != "": + line.add state.input + else: + line.add $pathToName(getCurrentDir()).bb("faint") + b.addLine line func numLines(b: Buffer): int = b.buffer.count '\n' diff --git a/src/tsm.nim b/src/tsm.nim index c9850d3..7741581 100644 --- a/src/tsm.nim +++ b/src/tsm.nim @@ -1,6 +1,4 @@ -import std/[tables] - -import selector, project, tmuxutils +import ./[selector, project, tmuxutils] # TODO: add option to only opened configured sessions proc tsm(open: bool = false) = @@ -14,16 +12,44 @@ proc tsm(open: bool = false) = else: tmux.attach project.name -proc getVersion(): string = - const tsmVersion {.strdefine.} = "unknown" - const gitVersion = staticExec "git describe --tags --always HEAD --match 'v*'" - when tsmVersion != "unknown": tsmVersion - else: gitVersion - when isMainModule: - import cligen, hwylterm, hwylterm/cli - clCfg.version = getVersion() - hwylCli(clCfg) - let clUse* = $bb("$command $args\n${doc}[bold]Options[/]:\n$options") - dispatch(tsm, usage = clUse, short = {"version": 'v'}) + import std/parseopt + import hwylterm, hwylterm/cli + + const tsmVersion {.strdefine.} = staticExec "git describe --tags --always HEAD --match 'v*'" + proc help() = + echo newHwylCli( + """tmux session manager + +[bold]tsm[/] [[[faint]-h|-v|-o[/]]""", + + flags = [ + ("h","help","show this help"), + ("v","version", "print version"), + ("o","open", "only search open sessions") + ] + ) + var open: bool + var p = initOptParser( + shortNoVal = {'h', 'v', 'o'}, + longNoVal = @["open"] + ) + for kind, key, val in p.getOpt(): + case kind: + of cmdEnd: + break + of cmdArgument: + echo bb"[red]Error[/]: unexpected positional argument ", bbfmt"[bold]{key}[/]" + of cmdShortOption, cmdLongOption: + case key: + of "help", "h": + help(); quit 0 + of "version", "v": + echo "tsm: " & tsmVersion; quit 0 + of "open", "o": + open = true + else: + echo bbfmt"[red]Error[/]: unknown key value pair", bbfmt"[b]key[/]: {key}, [b]value[/]: {val}" + + tsm(open) diff --git a/tsm.nimble b/tsm.nimble index cd4035e..8e946f9 100644 --- a/tsm.nimble +++ b/tsm.nimble @@ -12,7 +12,6 @@ binDir = "bin" requires "nim >= 2.0.0" requires "illwill >= 0.4.1" -requires "cligen" requires "https://github.com/daylinmorgan/hwylterm#HEAD" requires "https://github.com/usu-dev/usu-nim"