feat: add support for config and named sessions

This commit is contained in:
Daylin Morgan 2024-03-18 16:43:38 -05:00
parent 70a828b91d
commit e58307358d
Signed by: daylin
GPG key ID: 950D13E9719334AD
3 changed files with 24 additions and 12 deletions

View file

@ -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 != "":

View file

@ -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

View file

@ -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"