refactor!: drop usu for yaml

This commit is contained in:
Daylin Morgan 2024-09-13 12:29:41 -05:00
parent dc9c8dacef
commit 1a510de961
Signed by: daylin
GPG key ID: 950D13E9719334AD
6 changed files with 72 additions and 57 deletions

View file

@ -11,6 +11,16 @@
"sha1": "b338433f9a7a1b788b7583674c2b14096ced29ee" "sha1": "b338433f9a7a1b788b7583674c2b14096ced29ee"
} }
}, },
"cligen": {
"version": "1.7.0",
"vcsRevision": "4193f802796f15559c81c6dd56724d6f20345917",
"url": "https://github.com/c-blake/cligen.git",
"downloadMethod": "git",
"dependencies": [],
"checksums": {
"sha1": "300bd7fdb6e48d2d98e34ed0661206b50331e99c"
}
},
"illwill": { "illwill": {
"version": "0.4.1", "version": "0.4.1",
"vcsRevision": "99a120f7f69868b94f5d35ce7e21dd12535de70c", "vcsRevision": "99a120f7f69868b94f5d35ce7e21dd12535de70c",
@ -21,24 +31,14 @@
"sha1": "9c58351502f89a16caf031cbd1992ad3fdfd3c67" "sha1": "9c58351502f89a16caf031cbd1992ad3fdfd3c67"
} }
}, },
"usu": { "yaml": {
"version": "0.1.0", "version": "2.1.1",
"vcsRevision": "25574d165bb7f60900b36590bc49f3d90e47cea4", "vcsRevision": "48a90e36e82bd97457dae87e86efe423dcd3bb40",
"url": "https://github.com/usu-dev/usu-nim", "url": "https://github.com/flyx/NimYAML",
"downloadMethod": "git", "downloadMethod": "git",
"dependencies": [], "dependencies": [],
"checksums": { "checksums": {
"sha1": "61a8c13946e3eea9dbe04a6141ed93811563026c" "sha1": "302727fcd74c79d0697a4e909d26455d61a5b979"
}
},
"cligen": {
"version": "1.7.0",
"vcsRevision": "4193f802796f15559c81c6dd56724d6f20345917",
"url": "https://github.com/c-blake/cligen.git",
"downloadMethod": "git",
"dependencies": [],
"checksums": {
"sha1": "300bd7fdb6e48d2d98e34ed0661206b50331e99c"
} }
} }
}, },

View file

@ -1,5 +1,6 @@
import std/[os, tables, strutils] import std/[os, streams, strformat, strutils]
import usu import yaml
import term
type type
TsmConfig* = object TsmConfig* = object
@ -9,19 +10,16 @@ type
Session = object Session = object
name*, dir*: string name*, dir*: string
# TODO: update when the API for usu is complete
proc loadConfigFile(): TsmConfig = proc loadConfigFile(): TsmConfig =
let configPath = getEnv("TSM_CONFIG", getConfigDir() / "tsm" / "config.usu") let configPath = getEnv("TSM_CONFIG", getConfigDir() / "tsm" / "config.yml")
if fileExists configPath: try:
let usuNode = parseUsu(readFile configPath) var s = newFileStream(configPath)
let topFields = usuNode.fields load(s, result)
if "dirs" in topFields: s.close()
for dir in usuNode.fields["dirs"].elems: except:
result.dirs.add dir.value termError fmt(
if "sessions" in topFields: "failed to load config file\npath: {configPath}\nmessage: {getCurrentExceptionMsg()}"
for session in usuNode.fields["sessions"].elems: )
result.sessions.add Session(name: session.fields["name"].value,
dir: session.fields["dir"].value)
proc loadTsmConfig*(): TsmConfig = proc loadTsmConfig*(): TsmConfig =
result = loadConfigFile() result = loadConfigFile()
@ -31,4 +29,3 @@ proc loadTsmConfig*(): TsmConfig =
when isMainModule: when isMainModule:
echo loadConfigFile() echo loadConfigFile()

View file

@ -12,8 +12,12 @@ 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 = "", proc newProject(
named: bool = false): Project = 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
@ -78,19 +82,23 @@ 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(path = session.dir, open = false, result.add newProject(
name = session.name, named = true) path = session.dir,
open = false,
name = session.name,
named = true
)
if open: if open:
result = result.filterIt(it.open) result = result.filterIt(it.open)
# favor open projects then by update time # default order
# open -> configured -> mod time
result.sort do (x, y: Project) -> int: result.sort do (x, y: Project) -> int:
result = cmp(y.open, x.open) result = cmp(y.open, x.open)
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

View file

@ -1,9 +1,6 @@
import std/[enumerate, os, strformat, strutils, terminal] import std/[enumerate, os, strformat, strutils, terminal]
from illwill import illwillDeinit, illwillInit, getKey, Key from illwill import illwillDeinit, illwillInit, getKey, Key
import term import term, project
import project
func toStr(k: Key): string = $chr(ord(k)) func toStr(k: Key): string = $chr(ord(k))
@ -75,8 +72,8 @@ proc scrollUp() =
dec state.projectIdx dec state.projectIdx
proc scrollDown() = proc scrollDown() =
if (state.projects.len - state.projectIdx) > (state.buffer.height - if (state.projects.len - state.projectIdx) >
state.buffer.inputPad): (state.buffer.height - state.buffer.inputPad):
inc state.projectIdx inc state.projectIdx
proc up() = proc up() =
@ -143,7 +140,7 @@ proc addProject(b: var Buffer, project: Project, selected: bool) =
var displayName = fmt"[red]{input}[/]" var displayName = fmt"[red]{input}[/]"
if input.len < name.len: if input.len < name.len:
# bbansi missing add(string, bbstring) interface # bbansi missing add(string, bbstring) interface
displayName = displayName & fmt"[{project.highlight}]{name[input.len..^1]}[/{project.highlight}]" displayName &= fmt"[{project.highlight}]{name[input.len..^1]}[/{project.highlight}]"
b.addLine(cur & $displayName.bb) b.addLine(cur & $displayName.bb)
else: else:
b.addLine(cur & $name.bb(project.highlight)) b.addLine(cur & $name.bb(project.highlight))
@ -152,6 +149,7 @@ proc addProjectCount(b: var Buffer) =
let let
maxNumProjects = state.buffer.height - state.buffer.inputPad maxNumProjects = state.buffer.height - state.buffer.inputPad
numProjects = state.projects.len numProjects = state.projects.len
# TODO: use variables here for readability
b.addLine $(fmt"[[{state.projectIdx+1}-{state.projectIdx + min(maxNumProjects, numProjects)}/{numProjects}]".bb("faint")) b.addLine $(fmt"[[{state.projectIdx+1}-{state.projectIdx + min(maxNumProjects, numProjects)}/{numProjects}]".bb("faint"))
proc addProjects(b: var Buffer) = proc addProjects(b: var Buffer) =
@ -201,9 +199,9 @@ proc exitProc() {.noconv.} =
state.buffer.clear state.buffer.clear
showCursor() showCursor()
proc selectProject*(open: bool = false): Project = proc selectProject*(projects: seq[Project]): Project =
state.projects = findProjects(open) state.projects = projects
illwillInit(fullscreen = false) illwillInit(fullscreen = false)
setControlCHook(quitProc) setControlCHook(quitProc)
hideCursor() hideCursor()
@ -217,10 +215,8 @@ proc selectProject*(open: bool = false): Project =
of Key.Enter: of Key.Enter:
exitProc() exitProc()
return getProject() return getProject()
of Key.Up: of Key.Up: up()
up() of Key.Down: down()
of Key.Down:
down()
of Key.CtrlA..Key.CtrlL, Key.CtrlN..Key.CtrlZ, Key.CtrlRightBracket, of Key.CtrlA..Key.CtrlL, Key.CtrlN..Key.CtrlZ, Key.CtrlRightBracket,
Key.CtrlBackslash, Key.Right..Key.F12: Key.CtrlBackslash, Key.Right..Key.F12:
state.lastKey = key state.lastKey = key
@ -240,6 +236,7 @@ proc selectProject*(open: bool = false): Project =
when isMainModule: when isMainModule:
let selected = selectProject() projects = findProjects(open)
let selected = selectProject(projects)
echo "selected project -> " & $selected.name echo "selected project -> " & $selected.name

View file

@ -5,11 +5,12 @@ import selector, project, tmuxutils
# TODO: add option to only opened configured sessions # TODO: add option to only opened configured sessions
proc tsm(open: bool = false) = proc tsm(open: bool = false) =
let let
project = selectProject(open) projects = findProjects(open)
project = selectProject projects
selected = project.name selected = project.name
if selected notin tmux.sessions: if selected notin tmux.sessions:
tmux.new project.name, project.location tmux.new(project.name, project.location)
else: else:
tmux.attach project.name tmux.attach project.name
@ -19,10 +20,22 @@ when isMainModule:
clCfg.version = vsn clCfg.version = vsn
if clCfg.helpAttr.len == 0: if clCfg.helpAttr.len == 0:
clCfg.helpAttr = {"cmd": "\e[1;36m", "clDescrip": "", "clDflVal": "\e[33m", clCfg.helpAttr = {
"clOptKeys": "\e[32m", "clValType": "\e[31m", "args": "\e[3m"}.toTable "cmd" : "\e[1;36m",
clCfg.helpAttrOff = {"cmd": "\e[m", "clDescrip": "\e[m", "clDflVal": "\e[m", "clDescrip": "",
"clOptKeys": "\e[m", "clValType": "\e[m", "args": "\e[m"}.toTable "clDflVal" : "\e[33m",
"clOptKeys": "\e[32m",
"clValType": "\e[31m",
"args" : "\e[3m"
}.toTable
clCfg.helpAttrOff = {
"cmd" : "\e[m",
"clDescrip": "\e[m",
"clDflVal" : "\e[m",
"clOptKeys": "\e[m",
"clValType": "\e[m",
"args" : "\e[m"
}.toTable
dispatch( dispatch(
tsm, tsm,

View file

@ -14,5 +14,5 @@ requires "nim >= 2.0.0"
requires "illwill >= 0.4.1" requires "illwill >= 0.4.1"
requires "cligen" requires "cligen"
requires "https://github.com/daylinmorgan/bbansi >= 0.1.1" requires "https://github.com/daylinmorgan/bbansi >= 0.1.1"
requires "https://github.com/usu-dev/usu-nim" requires "yaml"