refactor: drop table for seq

This commit is contained in:
Daylin Morgan 2023-09-21 14:18:59 -05:00
parent e4fc240441
commit f2ddcac75f
Signed by: daylin
GPG key ID: C1E52E7DD81DF79F
2 changed files with 20 additions and 23 deletions

View file

@ -1,4 +1,4 @@
import std/[algorithm, os, strutils, tables, times] import std/[algorithm, os, strutils, times]
import utils import utils
@ -16,10 +16,9 @@ proc newProject(path: string, sessions: seq[string]): Project =
proc name*(p: Project): string = splitPath(p.location)[1].replace(".", "_") proc name*(p: Project): string = splitPath(p.location)[1].replace(".", "_")
proc findProjects*(open: bool = false): tuple[header: string, proc findProjects*(open: bool = false): seq[Project] =
projects: OrderedTable[string, Project]] =
## get a table of possible project paths ## get a table of possible project paths
# TODO: improve this to handle duplicate entries by appending parent?
let let
tsmDirs = getEnv("TSM_DIRS") tsmDirs = getEnv("TSM_DIRS")
@ -27,29 +26,29 @@ proc findProjects*(open: bool = false): tuple[header: string,
echo "Please set $TSM_DIRS to a colon-delimited list of paths" echo "Please set $TSM_DIRS to a colon-delimited list of paths"
quit 1 quit 1
var projects: seq[Project] # TODO: only return directories
for devDir in tsmDirs.split(":"): for devDir in tsmDirs.split(":"):
for d in walkDir(devDir): for d in walkDir(devDir):
let p = newProject(d.path, tmux.sessions) let p = newProject(d.path, tmux.sessions)
if open and p.open: projects.add p if open and p.open: result.add p
else: else:
projects.add p result.add p
if len(projects) == 0: if len(result) == 0:
echo "nothing to select" echo "nothing to select"
quit 1 quit 1
# TODO: use the input as a first filter? # TODO: use the input as a first filter?
# favor open projects then by update time # favor open projects then by update time
projects.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)
for p in projects: # for p in projects:
result.projects[p.name] = p # result.projects[p.name] = p
if len(result.projects) != len(projects): # if len(result.projects) != len(projects):
echo "there may be nonunique entries in the project names" # echo "there may be nonunique entries in the project names"

View file

@ -1,4 +1,4 @@
import std/[enumerate, os, sequtils, strformat, strutils, tables] import std/[enumerate, os, sequtils, strformat, strutils]
import illwill import illwill
import project import project
@ -36,10 +36,13 @@ type
window: Window window: Window
cursor: Cursor cursor: Cursor
projectIdx: Natural projectIdx: Natural
projects: seq[Project]
# TODO: don't need top level projects # TODO: don't need top level projects
let (_, projects) = findProjects() # let (_, projects) = findProjects()
var state = State() var state = State()
state.projects = findProjects()
proc values(c: Coord): (int, int, int, int) = (c.x1, c.x2, c.y1, c.y2) proc values(c: Coord): (int, int, int, int) = (c.x1, c.x2, c.y1, c.y2)
@ -51,7 +54,7 @@ proc scrollUp() =
dec state.projectIdx dec state.projectIdx
proc scrollDown() = proc scrollDown() =
if (projects.len - state.projectIdx) > state.window.height + 1: if (state.projects.len - state.projectIdx) > state.window.height + 1:
inc state.projectIdx inc state.projectIdx
proc up() = proc up() =
@ -83,14 +86,14 @@ proc sortProjects(): seq[Project] =
rest: seq[Project] rest: seq[Project]
if state.input != "": if state.input != "":
for name, project in projects: for project in state.projects:
if project.name.startsWith(state.input): if project.name.startsWith(state.input):
priority &= project.match() priority &= project.match()
else: else:
rest &= project rest &= project
return priority & rest return priority & rest
else: else:
return projects.values().toSeq() return state.projects.toSeq()
proc getProject(): Project = proc getProject(): Project =
let projects = sortProjects() let projects = sortProjects()
@ -192,17 +195,12 @@ proc update(c: var Cursor, min, max: Natural) =
proc getCoords(): Coord = proc getCoords(): Coord =
var width, height: Natural var width, height: Natural
let (termWidth, termHeight) = terminalSize() let (termWidth, termHeight) = terminalSize()
width = if termWidth > 65: 65 else: termWidth width = if termWidth > 65: 65 else: termWidth
height = if termHeight > 20: 20 else: termHeight height = if termHeight > 20: 20 else: termHeight
# fullscreen type behavior # fullscreen type behavior
result.x1 = ((termWidth - width)/2).int result.x1 = ((termWidth - width)/2).int
result.y1 = ((termHeight - height)/2).int result.y1 = ((termHeight - height)/2).int
result.x2 = result.x1 + width result.x2 = result.x1 + width
result.y2 = result.y1 + height result.y2 = result.y1 + height