mirror of
https://github.com/daylinmorgan/tsm.git
synced 2024-12-22 13:10:43 -06:00
feat: include additional open tmux sessions
This commit is contained in:
parent
cca580fd12
commit
6e31c14f7d
1 changed files with 43 additions and 29 deletions
|
@ -1,46 +1,58 @@
|
||||||
import std/[algorithm, os, strutils, times]
|
import std/[algorithm, os, sequtils, sets, strutils, tables, times]
|
||||||
|
|
||||||
|
import bbansi
|
||||||
import utils
|
import utils
|
||||||
|
|
||||||
type
|
type
|
||||||
Project* = object
|
Project* = object
|
||||||
|
name*: string
|
||||||
location*: string
|
location*: string
|
||||||
updated*: Time
|
updated*: Time
|
||||||
open*: bool
|
open*: bool
|
||||||
matched*: bool
|
matched*: bool
|
||||||
|
|
||||||
proc newProject(path: string, sessions: seq[string]): Project =
|
proc pathToName(path: string): string = splitPath(path)[1].replace(".", "_")
|
||||||
result.location = path
|
|
||||||
result.updated = getLastModificationTime(path)
|
|
||||||
result.open = splitPath(path)[1].replace(".", "_") in sessions
|
|
||||||
|
|
||||||
proc name*(p: Project): string = splitPath(p.location)[1].replace(".", "_")
|
proc newProject(path: string, open: bool): Project =
|
||||||
|
result.location = path
|
||||||
|
result.name = path.pathToName()
|
||||||
|
result.updated = getLastModificationTime(path)
|
||||||
|
result.open = open
|
||||||
|
|
||||||
|
proc newUnknownProject(name: string): Project =
|
||||||
|
result.name = name
|
||||||
|
|
||||||
|
proc getTsmDirs(): seq[string] =
|
||||||
|
let tsmDirs = getEnv("TSM_DIRS")
|
||||||
|
if tsmDirs == "":
|
||||||
|
bbEcho "[red]Please set [cyan]$TSM_DIRS[/] to a colon-delimited list of paths"
|
||||||
|
quit QuitFailure
|
||||||
|
result = tsmDirs.split(":")
|
||||||
|
|
||||||
proc findProjects*(open: bool = false): seq[Project] =
|
proc findProjects*(open: bool = false): seq[Project] =
|
||||||
## get a table of possible project paths
|
var candidates: Table[string, seq[string]]
|
||||||
# TODO: improve this to handle duplicate entries by appending parent?
|
var sessions = tmux.sessions.toHashSet()
|
||||||
let
|
|
||||||
tsmDirs = getEnv("TSM_DIRS")
|
|
||||||
|
|
||||||
if tsmDirs == "":
|
for devDir in getTsmDirs():
|
||||||
echo "Please set $TSM_DIRS to a colon-delimited list of paths"
|
|
||||||
quit 1
|
|
||||||
|
|
||||||
# TODO: only return directories
|
|
||||||
for devDir in tsmDirs.split(":"):
|
|
||||||
for path in walkDir(devDir):
|
for path in walkDir(devDir):
|
||||||
if path.kind == pcFile or path.kind == pcLinkToFile: continue
|
if ({path.kind} * {pcFile, pcLinkToFile}).len > 0: continue
|
||||||
let p = newProject(path.path, tmux.sessions)
|
let name = path.path.tailDir()
|
||||||
if open:
|
if name in candidates:
|
||||||
if p.open: result.add p
|
candidates[name].add path.path
|
||||||
else:
|
else:
|
||||||
result.add p
|
candidates[name] = @[path.path]
|
||||||
|
|
||||||
if len(result) == 0:
|
# TODO: improve this to handle duplicate entries by appending parent?
|
||||||
echo "nothing to select"
|
for name, paths in candidates:
|
||||||
quit 1
|
if len(paths) == 1:
|
||||||
|
let path = paths[0]
|
||||||
|
let open = path.pathToName in sessions
|
||||||
|
result.add newProject(path, open)
|
||||||
|
if open:
|
||||||
|
sessions.excl toHashSet([path.pathToName])
|
||||||
|
|
||||||
# TODO: use the input as a first filter?
|
if open:
|
||||||
|
result = result.filterIt(it.open)
|
||||||
|
|
||||||
# favor open projects then by update time
|
# favor open projects then by update time
|
||||||
result.sort do (x, y: Project) -> int:
|
result.sort do (x, y: Project) -> int:
|
||||||
|
@ -48,9 +60,11 @@ 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)
|
||||||
|
|
||||||
# for p in projects:
|
if sessions.len > 0:
|
||||||
# result.projects[p.name] = p
|
result = sessions.toSeq().mapIt(newUnknownProject(it)) & result
|
||||||
|
|
||||||
|
if len(result) == 0:
|
||||||
|
echo "nothing to select"
|
||||||
|
quit 1
|
||||||
|
|
||||||
# if len(result.projects) != len(projects):
|
|
||||||
# echo "there may be nonunique entries in the project names"
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue