From c2d4d186fec347cc9f6599e5bcd202546a957d07 Mon Sep 17 00:00:00 2001 From: Jeremie Fraeys Date: Fri, 8 Aug 2025 11:45:25 -0400 Subject: [PATCH] Refactor folders and add metadata table for cached session choices --- command_builder.lua | 34 ++++++++++++++++++++++----------- init.lua | 6 +++--- ui.lua | 28 ++++++++++++++------------- utils.lua | 2 +- workspace.lua => workspaces.lua | 6 +++--- 5 files changed, 45 insertions(+), 31 deletions(-) rename workspace.lua => workspaces.lua (87%) diff --git a/command_builder.lua b/command_builder.lua index dae6031..6208721 100644 --- a/command_builder.lua +++ b/command_builder.lua @@ -1,12 +1,13 @@ local wezterm = require("wezterm") -local State = require("state") -local utils = require("utils") +local State = require("plugins.sessionizer.state") +local utils = require("plugins.sessionizer.utils") local M = {} -- Cache tool availability and scan results local _has_fd = nil local _scan_cache = {} +local _meta_lookup = {} -- Check for `fd` binary without spawning full shell local function has_fd() @@ -52,7 +53,6 @@ function M.build_cmd(base) return cmd end --- Scan the base directory and return a structured list of paths function M.scan_base(base) local key = base.path .. ":" .. (base.max_depth or "default") if _scan_cache[key] then @@ -65,18 +65,30 @@ function M.scan_base(base) return {} end - local res = {} + local choices = {} + for _, line in ipairs(wezterm.split_by_newlines(out)) do - table.insert(res, { - id = line, - label = line:gsub(wezterm.home_dir, "~"), - workspace = utils.basename(line), -- used for workspace naming - title = utils.basename(line), -- optional: used for tab titles + local id = line + local label = line:gsub(wezterm.home_dir, "~") + + table.insert(choices, { + id = id, + label = label, }) + + -- store metadata separately + _meta_lookup[id] = { + workspace = utils.basename(line), + title = utils.basename(line), + path = line, + } end - _scan_cache[key] = res - return res + _scan_cache[key] = choices + return choices end +-- Export metadata so other modules (like ui.lua) can use it +M.meta_lookup = _meta_lookup + return M diff --git a/init.lua b/init.lua index f6c5ba0..ae01098 100644 --- a/init.lua +++ b/init.lua @@ -1,7 +1,7 @@ local wezterm = require("wezterm") -local state = require("sessionizer.state") -local ui = require("sessionizer.ui") -local workspaces = require("sessionizer.workspaces") +local state = require("plugins.sessionizer.state") +local ui = require("plugins.sessionizer.ui") +local workspaces = require("plugins.sessionizer.workspaces") local M = {} diff --git a/ui.lua b/ui.lua index 963dc94..46a614b 100644 --- a/ui.lua +++ b/ui.lua @@ -1,6 +1,7 @@ local wezterm = require("wezterm") local act = wezterm.action -local workspace = require("workspaces") +local command_builder = require("plugins.sessionizer.command_builder") +local workspace = require("plugins.sessionizer.workspaces") local M = {} @@ -15,15 +16,18 @@ local function switch_logic(win, pane, id, label) return end - -- Expand ~ to home directory reliably - local cwd = wezterm.expand_path(id) + local metadata = command_builder.meta_lookup[id] or {} + + -- Determine workspace name: use metadata.workspace or fallback to basename of id + local workspace_name = metadata.workspace or wezterm.basename(id) + local title_label = metadata.title or ("Workspace: " .. label) win:perform_action( act.SwitchToWorkspace({ - name = id, -- use the full path or unique ID as workspace name + name = workspace_name, -- Use workspace name from metadata or fallback spawn = { - label = "Workspace: " .. label, - cwd = cwd, + label = title_label, -- Title shown on tab or workspace label + cwd = id, -- Start cwd for workspace }, }), pane @@ -37,21 +41,19 @@ function M.make_switcher() local choices = workspace.all_dirs() if #choices == 0 then - -- Notify user visually that no workspaces are available wezterm.toast_notification({ title = "Sessionizer", - message = "No workspaces found", - timeout_milliseconds = 3000, + message = "No projects found", + timeout_milliseconds = 2000, }) return end win:perform_action( act.InputSelector({ - title = "WezTerm Sessionizer", - fuzzy = true, -- Enable fuzzy search - -- Case-insensitive search (default is true, but explicit is nice) - fuzzy_match_algorithm = "fzy", + title = "Sessionizer", + fuzzy = true, + fuzzy_description = "Fuzzy search projects: ", choices = choices, action = wezterm.action_callback(switch_logic), }), diff --git a/utils.lua b/utils.lua index cc3bbe0..fdbd34a 100644 --- a/utils.lua +++ b/utils.lua @@ -1,5 +1,5 @@ local wezterm = require("wezterm") -local State = require("state") +local State = require("plugins.sessionizer.state") local M = {} diff --git a/workspace.lua b/workspaces.lua similarity index 87% rename from workspace.lua rename to workspaces.lua index f7e5499..35a363e 100644 --- a/workspace.lua +++ b/workspaces.lua @@ -1,7 +1,7 @@ local wezterm = require("wezterm") -local state = require("state") -local command_builder = require("command_builder") -local utils = require("utils") +local state = require("plugins.sessionizer.state") +local command_builder = require("plugins.sessionizer.command_builder") +local utils = require("plugins.sessionizer.utils") local M = {}