mirror of
https://github.com/daylinmorgan/tsm.git
synced 2024-12-22 05:00:44 -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
|
## Usage
|
||||||
|
|
||||||
To configure `tsm` export the below environment variables:
|
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)
|
> `TSM_HEIGHT`: integer specifying number of rows in terminal (default: 15)
|
||||||
|
|
||||||
For example in your rc file:
|
For example in your rc file:
|
||||||
|
|
||||||
```sh
|
```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`.
|
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 std/[os, sequtils, strformat, strutils, parsecfg, tables]
|
||||||
import yaml
|
|
||||||
import term
|
import term
|
||||||
|
|
||||||
type
|
type
|
||||||
TsmConfig* = object
|
TsmConfig* = object
|
||||||
dirs*: seq[string]
|
paths*: seq[string]
|
||||||
sessions*: seq[Session]
|
sessions*: seq[Session]
|
||||||
|
|
||||||
Session = object
|
Session = object
|
||||||
name*, dir*: string
|
name*, path*: string
|
||||||
|
|
||||||
|
var configPath = getEnv("TSM_CONFIG", getConfigDir() / "tsm" / "config.ini")
|
||||||
|
|
||||||
proc sessionNames*(tc: TsmConfig): seq[string] =
|
proc sessionNames*(tc: TsmConfig): seq[string] =
|
||||||
tc.sessions.mapIt(it.name)
|
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 =
|
proc loadConfigFile(): TsmConfig =
|
||||||
let configPath = getEnv("TSM_CONFIG", getConfigDir() / "tsm" / "config.yml")
|
let dict = loadConfig(configPath)
|
||||||
try:
|
for k, v in dict.pairs:
|
||||||
var s = newFileStream(configPath)
|
if k == "paths":
|
||||||
load(s, result)
|
for path, v2 in v.pairs:
|
||||||
s.close()
|
check v2 == "", fmt"unexpected value in [paths] section {v}"
|
||||||
except:
|
result.paths.add path
|
||||||
termError fmt(
|
else:
|
||||||
"failed to load config file\npath: {configPath}\nmessage: {getCurrentExceptionMsg()}"
|
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 =
|
proc loadTsmConfig*(): TsmConfig =
|
||||||
result = loadConfigFile()
|
result = loadConfigFile()
|
||||||
let tsmDirs = getEnv("TSM_DIRS")
|
let tsmDirs = getEnv("TSM_PATHS")
|
||||||
if tsmDirs != "":
|
if tsmDirs != "":
|
||||||
result.dirs = tsmDirs.split(":")
|
result.paths = tsmDirs.split(":")
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
echo loadConfigFile()
|
echo loadConfigFile()
|
||||||
|
|
||||||
|
|
|
@ -29,9 +29,9 @@ proc newUnknownProject(name: string): Project =
|
||||||
result.open = true
|
result.open = true
|
||||||
|
|
||||||
proc getTsmDirs(): seq[string] =
|
proc getTsmDirs(): seq[string] =
|
||||||
let tsmDirs = getEnv("TSM_DIRS")
|
let tsmDirs = getEnv("TSM_PATHS")
|
||||||
if tsmDirs == "":
|
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(":")
|
result = tsmDirs.split(":")
|
||||||
|
|
||||||
proc findDuplicateProjects(
|
proc findDuplicateProjects(
|
||||||
|
@ -69,7 +69,7 @@ proc findProjects*(open: bool = false): seq[Project] =
|
||||||
var candidates: Table[string, seq[string]]
|
var candidates: Table[string, seq[string]]
|
||||||
var sessions = tmux.sessions.toHashSet()
|
var sessions = tmux.sessions.toHashSet()
|
||||||
|
|
||||||
for devDir in tsmConfig.dirs:
|
for devDir in tsmConfig.paths:
|
||||||
for (kind, path) in walkDir(devDir):
|
for (kind, path) in walkDir(devDir):
|
||||||
if ({kind} * {pcFile, pcLinkToFile}).len > 0:
|
if ({kind} * {pcFile, pcLinkToFile}).len > 0:
|
||||||
continue
|
continue
|
||||||
|
@ -89,7 +89,7 @@ proc findProjects*(open: bool = false): seq[Project] =
|
||||||
for session in tsmConfig.sessions:
|
for session in tsmConfig.sessions:
|
||||||
if session.name notin sessions:
|
if session.name notin sessions:
|
||||||
result.add newProject(
|
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:
|
if open:
|
||||||
|
@ -102,14 +102,10 @@ proc findProjects*(open: bool = false): seq[Project] =
|
||||||
if result == 0:
|
if result == 0:
|
||||||
result =
|
result =
|
||||||
if y.name in sessionNames:
|
if y.name in sessionNames:
|
||||||
if x.name in sessionNames:
|
if x.name in sessionNames: cmp(y.name, x.name)
|
||||||
cmp(y.name, x.name)
|
else: 1
|
||||||
else:
|
elif x.name in sessionNames: -1
|
||||||
1
|
else: cmp(y.updated, x.updated)
|
||||||
elif x.name in sessionNames:
|
|
||||||
-1
|
|
||||||
else:
|
|
||||||
cmp(y.updated, x.updated)
|
|
||||||
|
|
||||||
if sessions.len > 0:
|
if sessions.len > 0:
|
||||||
result = sessions.toSeq().mapIt(newUnknownProject(it)) & result
|
result = sessions.toSeq().mapIt(newUnknownProject(it)) & result
|
||||||
|
|
Loading…
Reference in a new issue