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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions internal/tui/browser/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
}

Expand Down
58 changes: 58 additions & 0 deletions internal/tui/browser/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 19 additions & 0 deletions internal/tui/browser/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
55 changes: 55 additions & 0 deletions internal/tui/browser/tree_test.go
Original file line number Diff line number Diff line change
@@ -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
}
Loading