mirror of
https://github.com/daylinmorgan/tsm.git
synced 2024-12-22 05:00:44 -06:00
feat: add support for config and named sessions
This commit is contained in:
parent
70a828b91d
commit
e58307358d
3 changed files with 24 additions and 12 deletions
|
@ -3,12 +3,12 @@ import std/[os, parsecfg, sequtils, tables, strutils]
|
||||||
import term
|
import term
|
||||||
|
|
||||||
type
|
type
|
||||||
TsmConfig = object
|
TsmConfig* = object
|
||||||
dirs: seq[string]
|
dirs*: seq[string]
|
||||||
sessions: seq[Session]
|
sessions*: seq[Session]
|
||||||
|
|
||||||
Session = object
|
Session = object
|
||||||
name, dir: string
|
name*, dir*: string
|
||||||
|
|
||||||
proc loadConfigFile(): TsmConfig =
|
proc loadConfigFile(): TsmConfig =
|
||||||
let configPath = getHomeDir() / ".config/tsm/config.ini"
|
let configPath = getHomeDir() / ".config/tsm/config.ini"
|
||||||
|
@ -23,7 +23,7 @@ proc loadConfigFile(): TsmConfig =
|
||||||
termError "[dirs] table should only contain a list of paths"
|
termError "[dirs] table should only contain a list of paths"
|
||||||
result.dirs.add key
|
result.dirs.add key
|
||||||
|
|
||||||
proc loadTsmConfig(): TsmConfig =
|
proc loadTsmConfig*(): TsmConfig =
|
||||||
result = loadConfigFile()
|
result = loadConfigFile()
|
||||||
let tsmDirs = getEnv("TSM_DIRS")
|
let tsmDirs = getEnv("TSM_DIRS")
|
||||||
if tsmDirs != "":
|
if tsmDirs != "":
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
import std/[algorithm, os, sequtils, sets, strutils, sugar, tables, times]
|
import std/[algorithm, os, sequtils, sets, strutils, sugar, tables, times]
|
||||||
import tmuxutils, term
|
import tmuxutils, term, config
|
||||||
|
|
||||||
type
|
type
|
||||||
Project* = object
|
Project* = object
|
||||||
|
named*: bool
|
||||||
name*: string
|
name*: string
|
||||||
location*: string
|
location*: string
|
||||||
updated*: Time
|
updated*: Time
|
||||||
|
@ -11,13 +12,15 @@ type
|
||||||
|
|
||||||
proc pathToName(path: string): string = splitPath(path)[1].replace(".", "_")
|
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.location = path
|
||||||
result.name =
|
result.name =
|
||||||
if name != "": name
|
if name != "": name
|
||||||
else: path.pathToName()
|
else: path.pathToName()
|
||||||
result.updated = getLastModificationTime(path)
|
result.updated = getLastModificationTime(path)
|
||||||
result.open = open
|
result.open = open
|
||||||
|
result.named = named
|
||||||
|
|
||||||
proc newUnknownProject(name: string): Project =
|
proc newUnknownProject(name: string): Project =
|
||||||
result.name = name
|
result.name = name
|
||||||
|
@ -29,8 +32,9 @@ proc getTsmDirs(): seq[string] =
|
||||||
termQuit "Please set [yellow]$TSM_DIRS[/] to a colon-delimited list of paths"
|
termQuit "Please set [yellow]$TSM_DIRS[/] to a colon-delimited list of paths"
|
||||||
result = tsmDirs.split(":")
|
result = tsmDirs.split(":")
|
||||||
|
|
||||||
proc findDuplicateProjects(paths: seq[string],
|
proc findDuplicateProjects(
|
||||||
sessions: var HashSet[string]): seq[Project] =
|
paths: seq[string], sessions: var HashSet[string]
|
||||||
|
): seq[Project] =
|
||||||
var candidates: Table[string, seq[string]]
|
var candidates: Table[string, seq[string]]
|
||||||
for p in paths:
|
for p in paths:
|
||||||
candidates[p] = p.split(DirSep)
|
candidates[p] = p.split(DirSep)
|
||||||
|
@ -51,10 +55,11 @@ proc findDuplicateProjects(paths: seq[string],
|
||||||
termQuit "failed to deduplicate these paths:" & paths.join(", ")
|
termQuit "failed to deduplicate these paths:" & paths.join(", ")
|
||||||
|
|
||||||
proc findProjects*(open: bool = false): seq[Project] =
|
proc findProjects*(open: bool = false): seq[Project] =
|
||||||
|
let tsmConfig = loadTsmConfig()
|
||||||
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 getTsmDirs():
|
for devDir in tsmConfig.dirs:
|
||||||
for path in walkDir(devDir):
|
for path in walkDir(devDir):
|
||||||
if ({path.kind} * {pcFile, pcLinkToFile}).len > 0: continue
|
if ({path.kind} * {pcFile, pcLinkToFile}).len > 0: continue
|
||||||
let name = path.path.splitPath.tail
|
let name = path.path.splitPath.tail
|
||||||
|
@ -71,6 +76,11 @@ proc findProjects*(open: bool = false): seq[Project] =
|
||||||
else:
|
else:
|
||||||
result &= findDuplicateProjects(paths, sessions)
|
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:
|
if open:
|
||||||
result = result.filterIt(it.open)
|
result = result.filterIt(it.open)
|
||||||
|
|
||||||
|
@ -80,6 +90,7 @@ proc findProjects*(open: bool = false): seq[Project] =
|
||||||
if result == 0:
|
if result == 0:
|
||||||
result = cmp(y.updated, x.updated)
|
result = 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
|
||||||
|
|
||||||
|
|
|
@ -128,7 +128,8 @@ proc clip(s: string): string =
|
||||||
else: s
|
else: s
|
||||||
|
|
||||||
proc highlight(p: Project): string =
|
proc highlight(p: Project): string =
|
||||||
if p.location == "": "green"
|
if p.named: "red"
|
||||||
|
elif p.location == "": "green"
|
||||||
elif p.open: "yellow"
|
elif p.open: "yellow"
|
||||||
else: "default"
|
else: "default"
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue