From f2ddcac75f288c5a3580f94635db3884cfcd442d Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Thu, 21 Sep 2023 14:18:59 -0500 Subject: [PATCH] refactor: drop table for seq --- src/project.nim | 25 ++++++++++++------------- src/tui.nim | 18 ++++++++---------- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/src/project.nim b/src/project.nim index 90ef0c8..7675048 100644 --- a/src/project.nim +++ b/src/project.nim @@ -1,4 +1,4 @@ -import std/[algorithm, os, strutils, tables, times] +import std/[algorithm, os, strutils, times] 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 findProjects*(open: bool = false): tuple[header: string, - projects: OrderedTable[string, Project]] = +proc findProjects*(open: bool = false): seq[Project] = ## get a table of possible project paths - + # TODO: improve this to handle duplicate entries by appending parent? let 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" quit 1 - var projects: seq[Project] + # TODO: only return directories for devDir in tsmDirs.split(":"): for d in walkDir(devDir): let p = newProject(d.path, tmux.sessions) - if open and p.open: projects.add p + if open and p.open: result.add p else: - projects.add p + result.add p - if len(projects) == 0: + if len(result) == 0: echo "nothing to select" quit 1 # TODO: use the input as a first filter? # 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) if result == 0: result = cmp(y.updated, x.updated) - for p in projects: - result.projects[p.name] = p + # for p in projects: + # result.projects[p.name] = p - if len(result.projects) != len(projects): - echo "there may be nonunique entries in the project names" + # if len(result.projects) != len(projects): + # echo "there may be nonunique entries in the project names" diff --git a/src/tui.nim b/src/tui.nim index 5c2f781..347132b 100644 --- a/src/tui.nim +++ b/src/tui.nim @@ -1,4 +1,4 @@ -import std/[enumerate, os, sequtils, strformat, strutils, tables] +import std/[enumerate, os, sequtils, strformat, strutils] import illwill import project @@ -36,10 +36,13 @@ type window: Window cursor: Cursor projectIdx: Natural + projects: seq[Project] + # TODO: don't need top level projects -let (_, projects) = findProjects() +# let (_, projects) = findProjects() var state = State() +state.projects = findProjects() 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 proc scrollDown() = - if (projects.len - state.projectIdx) > state.window.height + 1: + if (state.projects.len - state.projectIdx) > state.window.height + 1: inc state.projectIdx proc up() = @@ -83,14 +86,14 @@ proc sortProjects(): seq[Project] = rest: seq[Project] if state.input != "": - for name, project in projects: + for project in state.projects: if project.name.startsWith(state.input): priority &= project.match() else: rest &= project return priority & rest else: - return projects.values().toSeq() + return state.projects.toSeq() proc getProject(): Project = let projects = sortProjects() @@ -192,17 +195,12 @@ proc update(c: var Cursor, min, max: Natural) = proc getCoords(): Coord = var width, height: Natural let (termWidth, termHeight) = terminalSize() - - width = if termWidth > 65: 65 else: termWidth height = if termHeight > 20: 20 else: termHeight - # fullscreen type behavior result.x1 = ((termWidth - width)/2).int result.y1 = ((termHeight - height)/2).int - - result.x2 = result.x1 + width result.y2 = result.y1 + height