From e58307358dbe50330a86ea617d9aab875d2e82ed Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Mon, 18 Mar 2024 16:43:38 -0500 Subject: [PATCH] feat: add support for config and named sessions --- src/config.nim | 10 +++++----- src/project.nim | 21 ++++++++++++++++----- src/selector.nim | 5 +++-- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/config.nim b/src/config.nim index e09edef..c05ee17 100644 --- a/src/config.nim +++ b/src/config.nim @@ -3,12 +3,12 @@ import std/[os, parsecfg, sequtils, tables, strutils] import term type - TsmConfig = object - dirs: seq[string] - sessions: seq[Session] + TsmConfig* = object + dirs*: seq[string] + sessions*: seq[Session] Session = object - name, dir: string + name*, dir*: string proc loadConfigFile(): TsmConfig = let configPath = getHomeDir() / ".config/tsm/config.ini" @@ -23,7 +23,7 @@ proc loadConfigFile(): TsmConfig = termError "[dirs] table should only contain a list of paths" result.dirs.add key -proc loadTsmConfig(): TsmConfig = +proc loadTsmConfig*(): TsmConfig = result = loadConfigFile() let tsmDirs = getEnv("TSM_DIRS") if tsmDirs != "": diff --git a/src/project.nim b/src/project.nim index 1c7a475..26b6434 100644 --- a/src/project.nim +++ b/src/project.nim @@ -1,8 +1,9 @@ import std/[algorithm, os, sequtils, sets, strutils, sugar, tables, times] -import tmuxutils, term +import tmuxutils, term, config type Project* = object + named*: bool name*: string location*: string updated*: Time @@ -11,13 +12,15 @@ type proc pathToName(path: string): string = splitPath(path)[1].replace(".", "_") -proc newProject(path: string, open: bool, name = ""): Project = +proc newProject(path: string, open: bool, name = "", + named: bool = false): Project = result.location = path result.name = if name != "": name else: path.pathToName() result.updated = getLastModificationTime(path) result.open = open + result.named = named proc newUnknownProject(name: string): Project = result.name = name @@ -29,8 +32,9 @@ proc getTsmDirs(): seq[string] = termQuit "Please set [yellow]$TSM_DIRS[/] to a colon-delimited list of paths" result = tsmDirs.split(":") -proc findDuplicateProjects(paths: seq[string], - sessions: var HashSet[string]): seq[Project] = +proc findDuplicateProjects( + paths: seq[string], sessions: var HashSet[string] +): seq[Project] = var candidates: Table[string, seq[string]] for p in paths: candidates[p] = p.split(DirSep) @@ -51,10 +55,11 @@ proc findDuplicateProjects(paths: seq[string], termQuit "failed to deduplicate these paths:" & paths.join(", ") proc findProjects*(open: bool = false): seq[Project] = + let tsmConfig = loadTsmConfig() var candidates: Table[string, seq[string]] var sessions = tmux.sessions.toHashSet() - for devDir in getTsmDirs(): + for devDir in tsmConfig.dirs: for path in walkDir(devDir): if ({path.kind} * {pcFile, pcLinkToFile}).len > 0: continue let name = path.path.splitPath.tail @@ -71,6 +76,11 @@ proc findProjects*(open: bool = false): seq[Project] = else: result &= findDuplicateProjects(paths, sessions) + for session in tsmConfig.sessions: + if session.name notin sessions: + result.add newProject(path = session.dir, open = false, + name = session.name, named = true) + if open: result = result.filterIt(it.open) @@ -80,6 +90,7 @@ proc findProjects*(open: bool = false): seq[Project] = if result == 0: result = cmp(y.updated, x.updated) + if sessions.len > 0: result = sessions.toSeq().mapIt(newUnknownProject(it)) & result diff --git a/src/selector.nim b/src/selector.nim index 822f34b..84a0cb1 100644 --- a/src/selector.nim +++ b/src/selector.nim @@ -14,7 +14,7 @@ proc getMaxHeight(): int = try: result = parseInt(setting) except ValueError: - termQuit fmt"failed to parse TSM_HEIGHT of `{setting}`, expected integer" + termQuit fmt"failed to parse TSM_HEIGHT of `{setting}`, expected integer" let maxHeight = getMaxHeight() @@ -128,7 +128,8 @@ proc clip(s: string): string = else: s proc highlight(p: Project): string = - if p.location == "": "green" + if p.named: "red" + elif p.location == "": "green" elif p.open: "yellow" else: "default"