Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions internal/ui/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
59 changes: 59 additions & 0 deletions internal/ui/browser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"path/filepath"
"strings"
"testing"
"time"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
Expand Down Expand Up @@ -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
}
42 changes: 38 additions & 4 deletions internal/ui/dirlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -25,23 +33,26 @@ 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.
//
// 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
Expand Down Expand Up @@ -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 {
Expand Down
Loading