From e4fc240441d9031776409b0bfa64e68c6a0ae704 Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Thu, 21 Sep 2023 13:43:14 -0500 Subject: [PATCH] refactor: abstract tmux/project --- .gitignore | 2 ++ src/project.nim | 13 +++---------- src/tsm.nim | 28 ++++++---------------------- src/tui.nim | 12 ++++++------ src/utils.nim | 44 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 61 insertions(+), 38 deletions(-) create mode 100644 src/utils.nim diff --git a/.gitignore b/.gitignore index 82f0815..e2e287e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ /src/tui /dist /bin +# atlas +src/nim.cfg diff --git a/src/project.nim b/src/project.nim index 379d2f4..90ef0c8 100644 --- a/src/project.nim +++ b/src/project.nim @@ -1,10 +1,6 @@ -import std/[algorithm, os, osproc, strutils, tables, times] - - -proc listTmuxSessions*(): seq[string] = - let (output, _) = execCmdEx("tmux list-sessions -F '#S'") - return output.splitLines() +import std/[algorithm, os, strutils, tables, times] +import utils type Project* = object @@ -20,14 +16,12 @@ proc newProject(path: string, sessions: seq[string]): Project = proc name*(p: Project): string = splitPath(p.location)[1].replace(".", "_") - proc findProjects*(open: bool = false): tuple[header: string, projects: OrderedTable[string, Project]] = ## get a table of possible project paths let tsmDirs = getEnv("TSM_DIRS") - sessions = listTmuxSessions() if tsmDirs == "": echo "Please set $TSM_DIRS to a colon-delimited list of paths" @@ -36,7 +30,7 @@ proc findProjects*(open: bool = false): tuple[header: string, var projects: seq[Project] for devDir in tsmDirs.split(":"): for d in walkDir(devDir): - let p = newProject(d.path, sessions) + let p = newProject(d.path, tmux.sessions) if open and p.open: projects.add p else: projects.add p @@ -59,4 +53,3 @@ proc findProjects*(open: bool = false): tuple[header: string, if len(result.projects) != len(projects): echo "there may be nonunique entries in the project names" - diff --git a/src/tsm.nim b/src/tsm.nim index df2d5c1..999ef29 100644 --- a/src/tsm.nim +++ b/src/tsm.nim @@ -1,36 +1,20 @@ -import std/[os, osproc, strformat, tables] +import std/[tables] -import tui, project - -proc checkExe(names: varargs[string]) = - for name in names: - if findExe(name) == "": - echo "tsm requires " & name - -template tmux(cmd: string) = - discard execCmd("tmux " & cmd) +import tui, project, utils proc tsm() = - checkExe "tmux" - let project = selectProject() selected = project.name - if existsEnv("TMUX"): - if selected notin listTmuxSessions(): - tmux &"new-session -d -s {selected} -c {project.location}" - else: - tmux &"switch-client -t {selected}" + if selected notin tmux.sessions: + tmux.new project.name, project.location else: - if selected notin listTmuxSessions(): - tmux &"new-session -s {selected} -c {project.location}" - else: - tmux &"attach -t {selected}" + tmux.attach project.name when isMainModule: import cligen - const vsn = staticExec "git describe --tags --always HEAD" + const vsn = staticExec "git describe --tags --always HEAD --match 'v*'" clCfg.version = vsn if clCfg.helpAttr.len == 0: diff --git a/src/tui.nim b/src/tui.nim index ad5044e..5c2f781 100644 --- a/src/tui.nim +++ b/src/tui.nim @@ -12,9 +12,9 @@ proc exitProc() {.noconv.} = illwillDeinit() showCursor() -template withfgColor(ForegroundColor, body: untyped) = +template withfgColor(fgColor, body: untyped) = var tb = state.buffer - tb.setForegroundColor(ForegroundColor, bright = true) + tb.setForegroundColor(fgColor, bright = true) body tb.setForegroundColor(fgWhite, bright = true) @@ -100,8 +100,9 @@ proc getProject(): Project = proc clip(s: string): string = let maxWidth = state.window.width - 2 - result = if s.len > maxWidth: - s[0..^state.window.width] + result = + if s.len > maxWidth: + s[0..^state.window.width] else: s proc displayProject(tb: var TerminalBuffer, x, y: int, project: Project) = @@ -155,7 +156,6 @@ when defined(debug): for i, line in lines: tb.write(x, y+i, line) - proc draw() = var tb = state.buffer @@ -164,7 +164,7 @@ proc draw() = tb.setForegroundColor(fgWhite, bright = true) let - (x1, x2, y1, _) = state.window.coord.values() + (x1, x2, y1, y2) = state.window.coord.values() maxWidth = x2 - x1 - 4 when defined(debug): diff --git a/src/utils.nim b/src/utils.nim new file mode 100644 index 0000000..dd77448 --- /dev/null +++ b/src/utils.nim @@ -0,0 +1,44 @@ +import std/[os, osproc, strformat, strutils] + +type + Tmux = object + active: bool + sessions*: seq[string] + +proc checkExe(names: varargs[string]) = + for name in names: + if findExe(name) == "": + echo "tsm requires " & name + +checkExe "tmux" + +proc cmdGet(tmux: Tmux, args: string): string = + let (output, code) = execCmdEx("tmux " & args) + if code != 0: + echo "ERROR: failed to run: tmux ", args, "see below for error" + echo output + quit QuitFailure + return output + +template cmd(tmux: Tmux, args: string) = + discard execCmd("tmux " & args) + +proc newTmux(): Tmux = + result.active = existsEnv("TMUX") + result.sessions = ( + result.cmdGet "list-sessions -F '#S'" + ).strip().split("\n") + +proc attach*(t: Tmux, session: string) = + let args = + if t.active:"switch-client -t" + else: "attach -t" + t.cmd fmt"{args} {session}" + +proc new*(t: Tmux, session: string, loc: string) = + let args = + if t.active: "new-session -d" + else: "new-session" + t.cmd fmt"{args} -s {session} -c {loc}" + +let tmux* = newTmux()