mirror of
https://github.com/daylinmorgan/tsm.git
synced 2024-11-16 01:18:31 -06:00
refactor!: use ini/cfg
This commit is contained in:
parent
fcdd419c31
commit
8c866db5f0
3 changed files with 34 additions and 29 deletions
|
@ -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`.
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue