diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a2bba4..903ffe6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file. - `c` copies the complete loaded file preview to the terminal clipboard without line numbers or display wrapping; while a preview is open, drift releases the mouse to the terminal so its text can also be selected directly ### Fixed +- opening a directory near the bottom of the local or remote browser now scrolls enough to show all newly loaded children when they fit, or keeps the directory at the top when they do not - a running connect or sync could only be hidden, not stopped. Esc still hides the overlay. `q` and `Ctrl+C` cancel it. Files already transferred stay, remaining work is skipped. A hidden overlay keeps `[q] cancel` on the status line - unified diff `@@` headers sat after their context lines, so a hunk starting at line 1 showed a block of unchanged lines and then the marker. The header is the first row of the hunk now, with context underneath - tabbing through the diff file list could punch large black holes in the layout when a file used CRLF line endings. A leftover `\r` sent the terminal cursor back to column 0, so the padded diff background painted over the file list. Line splits now drop CR, and the renderer ignores any that remain diff --git a/internal/tui/browser/remote.go b/internal/tui/browser/remote.go index 8d1d87a..b232e50 100644 --- a/internal/tui/browser/remote.go +++ b/internal/tui/browser/remote.go @@ -138,6 +138,7 @@ func (m *Model) applyRemoteChildrenLoaded(msg MsgRemoteChildrenLoaded) { return } parent := m.remoteEntries[idx] + revealChildren := m.remoteCursor == idx parent.Expanded = false if msg.Err != nil { m.remoteStatus = "Remote error: " + msg.Err.Error() @@ -156,6 +157,23 @@ func (m *Model) applyRemoteChildrenLoaded(msg MsgRemoteChildrenLoaded) { newEntries = append(newEntries, m.remoteEntries[idx+1:]...) m.remoteEntries = newEntries m.remoteStatus = "Remote loaded: " + parent.Name + + // A remote read can finish after the user has moved elsewhere. Reveal the + // children only while the directory that started the read is still active. + if revealChildren && len(msg.Children) > 0 { + viewportHeight := m.viewportHeight() + if len(msg.Children)+1 >= viewportHeight { + m.remoteOffset = idx + } else { + minimumOffset := idx + len(msg.Children) - viewportHeight + 1 + if m.remoteOffset < minimumOffset { + m.remoteOffset = minimumOffset + } + if m.remoteOffset > idx { + m.remoteOffset = idx + } + } + } m.clampRemoteScroll() } diff --git a/internal/tui/browser/remote_test.go b/internal/tui/browser/remote_test.go index 152e61a..62eddb9 100644 --- a/internal/tui/browser/remote_test.go +++ b/internal/tui/browser/remote_test.go @@ -42,6 +42,64 @@ func TestRemoteBrowserQueuesOnlyOneDirectoryRead(t *testing.T) { } } +func TestRemoteBrowserRevealsLoadedDirectoryChildren(t *testing.T) { + entries := []*fs.FileEntry{ + {Name: "a", Path: "/a", Kind: fs.EntryDir}, + {Name: "b", Path: "/b", Kind: fs.EntryDir}, + {Name: "c", Path: "/c", Kind: fs.EntryDir}, + {Name: "d", Path: "/d", Kind: fs.EntryDir}, + {Name: "target", Path: "/target", Kind: fs.EntryDir, Expanded: true}, + } + model := Model{ + Height: 11, // five entry rows + remoteEntries: entries, + remoteCursor: 4, + remoteReading: true, + } + + model.applyRemoteChildrenLoaded(MsgRemoteChildrenLoaded{ + ParentPath: "/target", + Children: []*fs.FileEntry{ + {Name: "one", Path: "/target/one", Kind: fs.EntryFile}, + {Name: "two", Path: "/target/two", Kind: fs.EntryFile}, + {Name: "three", Path: "/target/three", Kind: fs.EntryFile}, + }, + }) + + if model.remoteOffset != 3 { + t.Fatalf("remote offset = %d, want 3 so the directory and all children are visible", model.remoteOffset) + } +} + +func TestRemoteBrowserDoesNotJumpAfterCursorLeavesLoadingDirectory(t *testing.T) { + entries := []*fs.FileEntry{ + {Name: "active", Path: "/active", Kind: fs.EntryDir}, + {Name: "b", Path: "/b", Kind: fs.EntryDir}, + {Name: "c", Path: "/c", Kind: fs.EntryDir}, + {Name: "d", Path: "/d", Kind: fs.EntryDir}, + {Name: "loading", Path: "/loading", Kind: fs.EntryDir, Expanded: true}, + } + model := Model{ + Height: 11, // five entry rows + remoteEntries: entries, + remoteCursor: 0, + remoteReading: true, + } + + model.applyRemoteChildrenLoaded(MsgRemoteChildrenLoaded{ + ParentPath: "/loading", + Children: []*fs.FileEntry{ + {Name: "one", Path: "/loading/one", Kind: fs.EntryFile}, + {Name: "two", Path: "/loading/two", Kind: fs.EntryFile}, + {Name: "three", Path: "/loading/three", Kind: fs.EntryFile}, + }, + }) + + if model.remoteOffset != 0 { + t.Fatalf("remote offset = %d, want 0 after the cursor left the loading directory", model.remoteOffset) + } +} + func TestRemoteBrowserBlocksDirectoryReadDuringPreviewRead(t *testing.T) { model := Model{ activePane: PaneRemote, diff --git a/internal/tui/browser/tree.go b/internal/tui/browser/tree.go index 95ac70c..c595195 100644 --- a/internal/tui/browser/tree.go +++ b/internal/tui/browser/tree.go @@ -31,6 +31,25 @@ func (m *Model) expandAt(i int) error { newEntries = append(newEntries, children...) newEntries = append(newEntries, m.entries[i+1:]...) m.entries = newEntries + + // Keep the directory and as many of its newly inserted children visible as + // the viewport allows. If the whole group fits, move only far enough to + // reveal its last child. Larger groups start at the directory itself. + if len(children) > 0 { + viewportHeight := m.viewportHeight() + if len(children)+1 >= viewportHeight { + m.offset = i + } else { + minimumOffset := i + len(children) - viewportHeight + 1 + if m.offset < minimumOffset { + m.offset = minimumOffset + } + if m.offset > i { + m.offset = i + } + } + m.clampLocalOffset() + } return nil } diff --git a/internal/tui/browser/tree_test.go b/internal/tui/browser/tree_test.go new file mode 100644 index 0000000..1cdff83 --- /dev/null +++ b/internal/tui/browser/tree_test.go @@ -0,0 +1,55 @@ +package browser + +import ( + "os" + "path/filepath" + "testing" +) + +func TestExpandAtRevealsAllChildrenWhenTheyFit(t *testing.T) { + model := localExpansionTestModel(t, 3) + + if err := model.expandAt(model.cursor); err != nil { + t.Fatalf("expand directory: %v", err) + } + + if model.offset != 3 { + t.Fatalf("offset = %d, want 3 so the directory and all children are visible", model.offset) + } +} + +func TestExpandAtPinsDirectoryToTopWhenChildrenDoNotFit(t *testing.T) { + model := localExpansionTestModel(t, 6) + + if err := model.expandAt(model.cursor); err != nil { + t.Fatalf("expand directory: %v", err) + } + + if model.offset != model.cursor { + t.Fatalf("offset = %d, want directory index %d at the top", model.offset, model.cursor) + } +} + +func localExpansionTestModel(t *testing.T, childCount int) Model { + t.Helper() + root := t.TempDir() + for _, name := range []string{"a", "b", "c", "d", "target"} { + if err := os.Mkdir(filepath.Join(root, name), 0o755); err != nil { + t.Fatalf("create directory %s: %v", name, err) + } + } + for i := range childCount { + name := string(rune('a'+i)) + ".txt" + if err := os.WriteFile(filepath.Join(root, "target", name), []byte(name), 0o644); err != nil { + t.Fatalf("create child %s: %v", name, err) + } + } + + model, err := New(root) + if err != nil { + t.Fatalf("create browser model: %v", err) + } + model.Height = 11 // five entry rows + model.cursor = 4 + return model +}