chore: use nph

This commit is contained in:
Daylin Morgan 2024-09-13 14:47:45 -05:00
parent f9b3470c35
commit fcdd419c31
Signed by: daylin
GPG key ID: 950D13E9719334AD
4 changed files with 74 additions and 65 deletions

View file

@ -1,4 +1,4 @@
import std/[os, sequtils, streams, strformat, strutils, ]
import std/[os, sequtils, streams, strformat, strutils]
import yaml
import term
@ -10,7 +10,7 @@ type
Session = object
name*, dir*: string
proc sessionNames*(tc: TsmConfig): seq[string] =
proc sessionNames*(tc: TsmConfig): seq[string] =
tc.sessions.mapIt(it.name)
proc loadConfigFile(): TsmConfig =

View file

@ -2,7 +2,8 @@ import std/[enumerate, os, strformat, strutils, terminal]
from illwill import illwillDeinit, illwillInit, getKey, Key
import term, project
func toStr(k: Key): string = $chr(ord(k))
func toStr(k: Key): string =
$chr(ord(k))
proc getMaxHeight(): int =
result = 10
@ -13,7 +14,6 @@ proc getMaxHeight(): int =
except ValueError:
termQuit fmt"failed to parse TSM_HEIGHT of `{setting}`, expected integer"
let maxHeight = getMaxHeight()
type
@ -41,7 +41,7 @@ proc addLine(b: var Buffer, text: string) =
b.buffer.add (" " & text).alignLeft(b.width) & "\n"
proc addDivider(b: var Buffer) =
b.addLine "".repeat(b.width-2)
b.addLine "".repeat(b.width - 2)
proc addInput(b: var Buffer) =
b.addLine "$ " & state.input
@ -59,9 +59,7 @@ proc draw(b: var Buffer) =
stdout.writeLine ""
stdout.writeLine "DEBUG INFO -------------"
stdout.writeLine $state.cursor
stdout.writeLine(
alignLeft("Key: " & $(state.lastKey), b.Buffer.width)
)
stdout.writeLine(alignLeft("Key: " & $(state.lastKey), b.Buffer.width))
stdout.cursorUp(b.numLines + 4)
else:
stdout.cursorUp(b.numLines)
@ -72,8 +70,9 @@ proc scrollUp() =
dec state.projectIdx
proc scrollDown() =
if (state.projects.len - state.projectIdx) >
(state.buffer.height - state.buffer.inputPad):
if (state.projects.len - state.projectIdx) > (
state.buffer.height - state.buffer.inputPad
):
inc state.projectIdx
proc up() =
@ -89,7 +88,8 @@ proc down() =
scrollDown()
proc backspace(s: string): string =
if s != "": result = s[0..^2]
if s != "":
result = s[0..^2]
proc match(project: Project): Project =
result = project
@ -97,7 +97,6 @@ proc match(project: Project): Project =
# TODO: convert this into a proper sorter
proc sortProjects(): seq[Project] =
var
priority: seq[Project]
rest: seq[Project]
@ -116,19 +115,23 @@ proc getProject(): Project =
var idx = state.cursor.y - state.cursor.min + state.projectIdx
return projects[idx]
proc clip(s: string): string =
let maxWidth = state.buffer.width - 2
result =
if s.len > maxWidth:
s[0..^maxWidth]
else: s
else:
s
proc highlight(p: Project): string =
if p.location == "": "green"
elif p.open: "yellow"
elif p.named: "bold cyan"
else: "default"
if p.location == "":
"green"
elif p.open:
"yellow"
elif p.named:
"bold cyan"
else:
"default"
proc addProject(b: var Buffer, project: Project, selected: bool) =
let
@ -140,7 +143,8 @@ proc addProject(b: var Buffer, project: Project, selected: bool) =
var displayName = fmt"[red]{input}[/]"
if input.len < name.len:
# bbansi missing add(string, bbstring) interface
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)
else:
b.addLine(cur & $name.bb(project.highlight))
@ -150,8 +154,8 @@ proc addProjectCount(b: var Buffer) =
maxNumProjects = state.buffer.height - state.buffer.inputPad
numProjects = state.projects.len
# TODO: use variables here for readability
let
low= state.projectIdx+1
let
low = state.projectIdx + 1
high = state.projectIdx + min(maxNumProjects, numProjects)
b.addLine $(fmt"[[{low}-{high}/{numProjects}]".bb("faint"))
@ -164,7 +168,8 @@ proc addProjects(b: var Buffer) =
for (i, project) in enumerate(projects[state.projectIdx..^1]):
b.addProject(project, state.cursor.y == numProjects)
inc numProjects
if numProjects > maxNumProjects: break
if numProjects > maxNumProjects:
break
proc reset() =
state.cursor.y = state.cursor.min
@ -180,11 +185,14 @@ proc draw() =
proc update(s: var State) =
s.buffer.width = terminalWidth()
s.buffer.height = min([
terminalHeight(),
maxHeight + state.buffer.inputPad,
state.buffer.inputPad + state.projects.len
])
s.buffer.height =
min(
[
terminalHeight(),
maxHeight + state.buffer.inputPad,
state.buffer.inputPad + state.projects.len
]
)
s.cursor.max = s.buffer.height - state.buffer.inputPad
proc clear(b: var Buffer) =
@ -203,7 +211,6 @@ proc exitProc() {.noconv.} =
showCursor()
proc selectProject*(projects: seq[Project]): Project =
state.projects = projects
illwillInit(fullscreen = false)
setControlCHook(quitProc)
@ -213,15 +220,23 @@ proc selectProject*(projects: seq[Project]): Project =
state.update()
var key = getKey()
case key
of Key.None: discard
of Key.Escape: quitProc()
of Key.None:
discard
of Key.Escape:
quitProc()
of Key.Enter:
exitProc()
return getProject()
of Key.Up: up()
of Key.Down: down()
of Key.CtrlA..Key.CtrlL, Key.CtrlN..Key.CtrlZ, Key.CtrlRightBracket,
Key.CtrlBackslash, Key.Right..Key.F12:
of Key.Up:
up()
of Key.Down:
down()
of
Key.CtrlA..Key.CtrlL,
Key.CtrlN..Key.CtrlZ,
Key.CtrlRightBracket,
Key.CtrlBackslash,
Key.Right..Key.F12:
state.lastKey = key
else:
reset()
@ -237,9 +252,7 @@ proc selectProject*(projects: seq[Project]): Project =
draw()
sleep(10)
when isMainModule:
projects = findProjects(open)
let selected = selectProject(projects)
echo "selected project -> " & $selected.name

View file

@ -21,29 +21,26 @@ proc tmuxError(args: string, output: string = "") =
echo output
quit QuitFailure
proc cmdGet(tmux: Tmux, args: string): string =
let (output, code) = execCmdEx("tmux " & args)
if code == 0: return output
if code == 0:
return output
tmuxError args, output
template cmd(tmux: Tmux, args: string) =
let code = execCmd "tmux " & args
if code != 0: tmuxError(args)
if code != 0:
tmuxError(args)
# discard tmux.cmdGet args
proc newTmux(): Tmux =
result.active = existsEnv("TMUX")
# check if server is active
if execCmdEx("tmux run").exitCode == 0:
result.sessions = (
result.cmdGet "list-sessions -F '#S'"
).strip().split("\n")
result.sessions = (result.cmdGet "list-sessions -F '#S'").strip().split("\n")
proc attach*(t: Tmux, session: string) =
let args =
if t.active: "switch-client -t"
else: "attach -t"
let args = if t.active: "switch-client -t" else: "attach -t"
t.cmd fmt"{args} {session}"
proc new*(t: Tmux, session: string, loc: string) =

View file

@ -20,24 +20,23 @@ when isMainModule:
clCfg.version = vsn
if clCfg.helpAttr.len == 0:
clCfg.helpAttr = {
"cmd" : "\e[1;36m",
"clDescrip": "",
"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
clCfg.helpAttr =
{
"cmd": "\e[1;36m",
"clDescrip": "",
"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(
tsm,
short = {"version": 'v'},
)
dispatch(tsm, short = {"version": 'v'})