From 8c866db5f020cbdbe9ea9123ce4b92478b41ede5 Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Sat, 14 Sep 2024 10:15:14 -0500 Subject: [PATCH] refactor!: use ini/cfg --- README.md | 4 ++-- src/config.nim | 39 ++++++++++++++++++++++++--------------- src/project.nim | 20 ++++++++------------ 3 files changed, 34 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index faf7fa8..3d0040c 100644 --- a/README.md +++ b/README.md @@ -18,13 +18,13 @@ nimble install https://github.com/daylinmorgan/tsm ## Usage To configure `tsm` export the below environment variables: -> `TSM_DIRS`: a colon-delimited set of parent directories to find projects. \ +> `TSM_PATHS`: a colon-delimited set of parent directories to find projects. \ > `TSM_HEIGHT`: integer specifying number of rows in terminal (default: 15) For example in your rc file: ```sh -export TSM_DIRS="$HOME/projects/personal:$HOME/projects/work" +export TSM_PATHS="$HOME/projects/personal:$HOME/projects/work" ``` To make full use of `tsm` you should also add a new key binding to your `tmux.conf`. diff --git a/src/config.nim b/src/config.nim index c95431c..97cb290 100644 --- a/src/config.nim +++ b/src/config.nim @@ -1,34 +1,43 @@ -import std/[os, sequtils, streams, strformat, strutils] -import yaml +import std/[os, sequtils, strformat, strutils, parsecfg, tables] import term type TsmConfig* = object - dirs*: seq[string] + paths*: seq[string] sessions*: seq[Session] Session = object - name*, dir*: string + name*, path*: string + +var configPath = getEnv("TSM_CONFIG", getConfigDir() / "tsm" / "config.ini") proc sessionNames*(tc: TsmConfig): seq[string] = tc.sessions.mapIt(it.name) +template check(cond: bool, msg: string) = + if not cond: + termQuit fmt"failed to load config file\npath: {configPath}\nmessage: " & msg + proc loadConfigFile(): TsmConfig = - let configPath = getEnv("TSM_CONFIG", getConfigDir() / "tsm" / "config.yml") - try: - var s = newFileStream(configPath) - load(s, result) - s.close() - except: - termError fmt( - "failed to load config file\npath: {configPath}\nmessage: {getCurrentExceptionMsg()}" - ) + let dict = loadConfig(configPath) + for k, v in dict.pairs: + if k == "paths": + for path, v2 in v.pairs: + check v2 == "", fmt"unexpected value in [paths] section {v}" + result.paths.add path + else: + check k.startsWith("session."), fmt"unexpected config section: {k}" + let name = k.replace("session.") + check v.hasKey("path"), fmt"expected value for path in section: {k}" + let path = v["path"] + result.sessions.add Session(name:name, path:path) proc loadTsmConfig*(): TsmConfig = result = loadConfigFile() - let tsmDirs = getEnv("TSM_DIRS") + let tsmDirs = getEnv("TSM_PATHS") if tsmDirs != "": - result.dirs = tsmDirs.split(":") + result.paths = tsmDirs.split(":") when isMainModule: echo loadConfigFile() + diff --git a/src/project.nim b/src/project.nim index 6c08dfe..0d80daa 100644 --- a/src/project.nim +++ b/src/project.nim @@ -29,9 +29,9 @@ proc newUnknownProject(name: string): Project = result.open = true proc getTsmDirs(): seq[string] = - let tsmDirs = getEnv("TSM_DIRS") + let tsmDirs = getEnv("TSM_PATHS") if tsmDirs == "": - termQuit "Please set [yellow]$TSM_DIRS[/] to a colon-delimited list of paths" + termQuit "Please set [yellow]$TSM_PATHS[/] to a colon-delimited list of paths" result = tsmDirs.split(":") proc findDuplicateProjects( @@ -69,7 +69,7 @@ proc findProjects*(open: bool = false): seq[Project] = var candidates: Table[string, seq[string]] var sessions = tmux.sessions.toHashSet() - for devDir in tsmConfig.dirs: + for devDir in tsmConfig.paths: for (kind, path) in walkDir(devDir): if ({kind} * {pcFile, pcLinkToFile}).len > 0: continue @@ -89,7 +89,7 @@ proc findProjects*(open: bool = false): seq[Project] = for session in tsmConfig.sessions: if session.name notin sessions: result.add newProject( - path = session.dir, open = false, name = session.name, named = true + path = session.path, open = false, name = session.name, named = true ) if open: @@ -102,14 +102,10 @@ proc findProjects*(open: bool = false): seq[Project] = if result == 0: result = if y.name in sessionNames: - if x.name in sessionNames: - cmp(y.name, x.name) - else: - 1 - elif x.name in sessionNames: - -1 - else: - cmp(y.updated, x.updated) + if x.name in sessionNames: cmp(y.name, x.name) + else: 1 + elif x.name in sessionNames: -1 + else: cmp(y.updated, x.updated) if sessions.len > 0: result = sessions.toSeq().mapIt(newUnknownProject(it)) & result