commit 9192f9e6bc31960f2056c2d174b5f447246617bf from: Brett Fisher date: Fri Jul 3 04:33:59 2026 UTC bin/browsefiles.lua: Refactor for consistency with scripts/modules. - Add colors, display, input, TITLE require and constant. - Replace hardcoded title string with TITLE constant. - Add opening, closing, and error status_line messages. - Replace mixed clear-and-position/io.read error handling with status_line/sleep pattern. - Replace loop state variable start_dir with current_dir. - Clean up commented-out and dead code. - Normalize whitespace in function definitions and comments. commit - 915d416f97e8de5013529b6896c9e61b2000868b commit + 9192f9e6bc31960f2056c2d174b5f447246617bf blob - e52072a07fcbd56998341eb97613c9a8d621058c blob + 3abcd44b25b07172e0d7dc5268fbf703f3e45a5f --- bin/browsefiles.lua +++ bin/browsefiles.lua @@ -1,7 +1,7 @@ #!/usr/bin/env lua5.4 -- lua file browser script --- Enforce Lua 5.4+ +-- Enforce Lua 5.4 if not _VERSION:match("5.4") then io.stderr:write("Error: Lua 5.4 required (got " .. _VERSION .. ").\n") os.exit(1) @@ -13,18 +13,25 @@ if not ok then error("LuaFileSystem not found.") end +local colors = require("aestel.colors") +local display = require("aestel.display") +local input = require("aestel.input") local navigate = require("aestel.navigate") local opener = require("aestel.opener") -local input = require("aestel.input") --- Utility: Normalize and validate start dir +local TITLE = "File Browser" + +-- Normalize and validate start dir local function get_valid_start_dir(raw_dir) + raw_dir = raw_dir or lfs.currentdir() or os.getenv("PWD") or os.getenv("HOME") + if not raw_dir then return nil, "No valid start directory (HOME/PWD not set)." end local home_dir = os.getenv("HOME") + if not home_dir then return nil, "HOME environment variable not set." end @@ -51,7 +58,6 @@ local function get_valid_start_dir(raw_dir) return normalized end --- Main implementation local function browse_and_open() local start_dir, err = get_valid_start_dir(arg[1]) if not start_dir then @@ -60,39 +66,36 @@ local function browse_and_open() end local home_dir = os.getenv("HOME") + local current_dir = start_dir - while true do -- LOOP: Repeat browse until quit - local selected = navigate.browse(start_dir, home_dir, 4, "📂 File Browser") + while true do + local selected = navigate.browse(current_dir, home_dir, nil, TITLE) if not selected then - break -- Quit: User pressed q or Esc + display.status_line("... Exiting " .. TITLE, colors.REVERSE) + os.execute("sleep 2") + display.clear_screen() + return end - -- Try to open - local opener_result = opener.open(selected) - local open_ok, open_err = opener_result - +-- -- Try to open +-- local opener_result = opener.open(selected) +-- local open_ok, open_err = opener_result + + local open_ok, open_err = opener.open(selected) if open_ok then - -- Success: No wait, loop back to browser + display.status_line("... Opening " .. selected, colors.REVERSE) else - -- Error: Clear for visibility + msg + wait, then loop back - input.clear_and_position(1, 1) - io.write("Failed to open '" .. selected .. "': " .. tostring(open_err or "Unknown error") .. "\n") - io.write("Press Enter to return to browser...\n") - io.flush() - io.read() -- Wait for Enter + display.status_line("... Failed to open'" .. selected .. "': " .. tostring(open_err or "Unknown error"), colors.REVERSE) end - - -- Next browse starts from current shell cwd (e.g., if cd in shell, respects it) - start_dir = lfs.currentdir() or home_dir -- Fallback to home if nil + os.execute("sleep 2") + current_dir = lfs.currentdir() or home_dir end end --- Run with error handling local success, err = pcall(browse_and_open) if not success then - input.disable_raw() -- Ensure terminal restored if browse crashes early + input.disable_raw() io.stderr:write("Error: " .. tostring(err) .. "\n") os.exit(1) end -input.disable_raw()