diff --git a/docs/architecture.md b/docs/architecture.md index 5bd99de..faf9efa 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -609,9 +609,17 @@ Two consequences worth keeping: whose children are symlinks to checkouts is a collector, and testing `IsDir` alone hid it. -The read is synchronous. A listing is one `Stat` per entry, the cost -`resolveProject` already pays on every pick, and doing it inline keeps the -explorer free of the message round-trip an async read would need. +The read is synchronous. A listing is one `ReadDir`, plus a `Stat` and an +`EvalSymlinks` for each symlink, and doing it inline keeps the explorer free +of the message round-trip an async read would need. + +The listing is re-read while the explorer is open. The frame tick calls +`dirList.refresh`, which re-reads once the rows are older than `dirRefresh` +and keeps the cursor on the same name. Without it, a directory made in +another pane stayed invisible until the explorer was closed and reopened. +`TestBrowserShowsADirectoryMadeWhileOpen` pins both halves. The cost of the +synchronous read now recurs: a directory on a slow mount stalls the UI on +every re-read while the explorer shows it, not only when it is opened. Validation lives in `browserKey`, not the form: the explorer is still on screen and one keystroke from the right directory, so an error belongs there. diff --git a/internal/ui/app.go b/internal/ui/app.go index 0c450eb..cee1fbb 100644 --- a/internal/ui/app.go +++ b/internal/ui/app.go @@ -82,6 +82,9 @@ func (m Model) route(msg tea.Msg) (tea.Model, tea.Cmd) { m.resizePane() m.sweepExited() m.dropDeadAttachment() + if m.browser != nil { + m.browser.list.refresh(time.Time(msg)) + } return m, frameTick() case tea.MouseMsg: diff --git a/internal/ui/browser_test.go b/internal/ui/browser_test.go index 2b24288..4d2b4b6 100644 --- a/internal/ui/browser_test.go +++ b/internal/ui/browser_test.go @@ -5,6 +5,7 @@ import ( "path/filepath" "strings" "testing" + "time" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" @@ -235,3 +236,61 @@ func names(d *dirList) string { // ansiOf renders text in one style, so a test can look for that exact styled // run rather than for a bare colour code that another role might share. func ansiOf(style lipgloss.Style, text string) string { return style.Render(text) } + +// TestBrowserShowsADirectoryMadeWhileOpen is the reported bug: a directory +// created in another pane stayed invisible until the explorer was reopened. A +// frame tick older than dirRefresh re-reads the listing, and the cursor stays +// on the name it was on even though the new row sorts above it. +func TestBrowserShowsADirectoryMadeWhileOpen(t *testing.T) { + root := t.TempDir() + if err := os.Mkdir(filepath.Join(root, "proj"), 0o755); err != nil { + t.Fatal(err) + } + m := Model{browser: newBrowser(root, 10)} + if err := os.Mkdir(filepath.Join(root, "added"), 0o755); err != nil { + t.Fatal(err) + } + + tick := func(after time.Duration) []dirRow { + next, _ := m.Update(frameMsg(m.browser.list.readAt.Add(after))) + m = next.(Model) + return m.browser.list.rows + } + if rows := tick(dirRefresh / 2); len(rows) != 1 { + t.Fatalf("re-read before dirRefresh: rows = %v", rows) + } + if rows := tick(dirRefresh); len(rows) != 2 || rows[0].name != "added" { + t.Fatalf("after dirRefresh, rows = %v, want added then proj", rows) + } + if got := m.browser.list.path(); got != filepath.Join(root, "proj") { + t.Errorf("cursor moved to %q, want it to stay on proj", got) + } +} + +// TestBrowserRefreshSurvivesTheCursorRowGoing covers the other way a re-read +// changes the rows: the highlighted directory is removed. The cursor keeps its +// old position, clamped, so it lands on the row that took the gone one's place +// rather than jumping to either end — and never indexes past the end. +func TestBrowserRefreshSurvivesTheCursorRowGoing(t *testing.T) { + root := t.TempDir() + for _, name := range []string{"a", "b", "c", "d"} { + if err := os.Mkdir(filepath.Join(root, name), 0o755); err != nil { + t.Fatal(err) + } + } + d := readDirList(root, 10) + removeAndRefresh := func(name, want string) { + t.Helper() + if err := os.Remove(filepath.Join(root, name)); err != nil { + t.Fatal(err) + } + d.refresh(d.readAt.Add(dirRefresh)) + if got := d.path(); got != filepath.Join(root, want) { + t.Errorf("removed %s under the cursor: cursor on %q, want %s", name, got, want) + } + } + d.move(1) + removeAndRefresh("b", "c") // a middle row: the next one moves up into place + d.move(1) + removeAndRefresh("d", "c") // the last row: the cursor clamps to the new end +} diff --git a/internal/ui/dirlist.go b/internal/ui/dirlist.go index 0c206bf..4b84cd2 100644 --- a/internal/ui/dirlist.go +++ b/internal/ui/dirlist.go @@ -8,8 +8,16 @@ import ( "path/filepath" "sort" "strings" + "time" ) +// dirRefresh is how old a listing may get while the explorer stays open. A +// directory made in another pane has to appear without closing the explorer, +// and a second reads as live to a person switching panes. The frame tick is +// far shorter than this, and re-reading on every frame would repeat a stat per +// symlink many times a second with nothing new to show. +const dirRefresh = time.Second + // A project is always a directory, so the listing shows directories only. // Files cannot be selected and cannot be descended into, and a list that // offers neither is a list of things to scroll past. @@ -25,15 +33,18 @@ type dirRow struct { // dirList is one directory on screen: its selectable entries and the cursor. // -// The read is synchronous. A directory listing is a stat per entry, the same -// cost resolveProject already pays on every pick, and doing it inline keeps -// the explorer free of the message round-trip an async read would need. +// The read is synchronous: one ReadDir, plus a Stat and an EvalSymlinks for +// each symlink. Doing it inline keeps the explorer free of the message +// round-trip an async read would need. refresh repeats it while the explorer +// is open, so a directory on a slow mount stalls the UI on each re-read, not +// only on the first. type dirList struct { dir string rows []dirRow cursor int visible int err error + readAt time.Time // when rows were read, so refresh knows their age } // readDirList lists the directories inside dir, following symlinks. @@ -41,7 +52,7 @@ type dirList struct { // An unreadable directory is recorded rather than returned: the explorer has // to keep rendering, and a caller that cannot draw the error cannot report it. func readDirList(dir string, visible int) *dirList { - d := &dirList{dir: dir, visible: max(visible, 1)} + d := &dirList{dir: dir, visible: max(visible, 1), readAt: time.Now()} entries, err := os.ReadDir(dir) if err != nil { d.err = err @@ -120,6 +131,29 @@ func (d *dirList) parent() { *d = *readDirList(up, d.visible) } +// refresh re-reads the directory once the listing is older than dirRefresh. +// The cursor stays on the same name, because a row appearing above it would +// otherwise move the highlight onto a different directory under the user. A +// name that has gone keeps the cursor at its old position, clamped. +func (d *dirList) refresh(now time.Time) { + if now.Sub(d.readAt) < dirRefresh { + return + } + var name string + if len(d.rows) > 0 { + name = d.rows[d.cursor].name + } + fresh := readDirList(d.dir, d.visible) + fresh.cursor = clamp(d.cursor, 0, max(len(fresh.rows)-1, 0)) + for i, r := range fresh.rows { + if r.name == name { + fresh.cursor = i + break + } + } + *d = *fresh +} + // view renders the rows, scrolling only far enough to keep the cursor on // screen. inner is the width available inside the modal's padding. func (d *dirList) view(s styleSet, inner int) string {