Skip to content
Open
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
12 changes: 12 additions & 0 deletions pkg/ui/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type KeyMap struct {
PrevFile key.Binding
CtrlD key.Binding
CtrlU key.Binding
ScrollLeft key.Binding
ScrollRight key.Binding
ToggleFileTree key.Binding
Search key.Binding
Quit key.Binding
Expand Down Expand Up @@ -71,6 +73,14 @@ var keys = &KeyMap{
key.WithKeys("ctrl+u"),
key.WithHelp("ctrl+u", "diff up"),
),
ScrollLeft: key.NewBinding(
key.WithKeys("left"),
key.WithHelp("←", "scroll left"),
),
ScrollRight: key.NewBinding(
key.WithKeys("right"),
key.WithHelp("→", "scroll right"),
),
Comment on lines +76 to +83
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these should scroll just 1 column at a time, like how j and k work.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Step is one column per keypress now, matching how j / k step one line. The diffviewer's ScrollLeft / ScrollRight delegate straight to m.vp.ScrollLeft(1) / m.vp.ScrollRight(1).

ToggleFileTree: key.NewBinding(
key.WithKeys("e"),
key.WithHelp("e", "toggle file tree"),
Expand Down Expand Up @@ -124,6 +134,8 @@ func KeyGroups() [][]key.Binding {
keys.PrevFile,
keys.CtrlD,
keys.CtrlU,
keys.ScrollLeft,
keys.ScrollRight,
}, {
keys.ToggleFileTree,
keys.Search,
Expand Down
36 changes: 23 additions & 13 deletions pkg/ui/panes/diffviewer/diffviewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
tea "charm.land/bubbletea/v2"
"charm.land/lipgloss/v2"
"github.com/bluekeyes/go-gitdiff/gitdiff"
"github.com/charmbracelet/x/ansi"

"github.com/dlvhdr/diffnav/pkg/filenode"
"github.com/dlvhdr/diffnav/pkg/icons"
Expand Down Expand Up @@ -80,18 +79,10 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
}

case diffContentMsg:
// Truncate lines to viewport width to prevent ANSI escape overflow.
lines := strings.Split(msg.text, "\n")
for i, line := range lines {
if lipgloss.Width(line) > m.vp.Width() && m.vp.Width() > 0 {
lines[i] = ansi.Truncate(line, m.vp.Width(), "")
}
}
diff := strings.Join(lines, "\n")
if _, ok := m.cache[msg.cacheKey]; ok {
m.cache[msg.cacheKey].diff = diff
m.cache[msg.cacheKey].diff = msg.text
}
m.vp.SetContent(diff)
m.vp.SetContent(msg.text)
}

return m, tea.Batch(cmds...)
Expand Down Expand Up @@ -291,6 +282,16 @@ func (m *Model) ScrollTop() {
m.vp.GotoTop()
}

// ScrollLeft scrolls the viewport one column toward column 0.
func (m *Model) ScrollLeft() {
m.vp.ScrollLeft(1)
}

// ScrollRight scrolls the viewport one column away from column 0.
func (m *Model) ScrollRight() {
m.vp.ScrollRight(1)
}

func diffFile(node *cachedNode, width int, sideBySide bool) tea.Cmd {
if width == 0 || node == nil || len(node.files) != 1 {
return nil
Expand All @@ -304,7 +305,14 @@ func diffFile(node *cachedNode, width int, sideBySide bool) tea.Cmd {
args := []string{
"--paging=never",
fmt.Sprintf("-w=%d", width),
fmt.Sprintf("--max-line-length=%d", width),
// Disable hard truncation and let delta's own line-wrapping (active
// in side-by-side mode) carry the full line through. With
// `--max-line-length=<width>` and the default `--wrap-max-lines=2`,
// long lines were being clipped at the viewport before we saw
// them. Anything still wider than the viewport gets clipped with
// a visible "…" marker in the `diffContentMsg` handler.
"--max-line-length=0",
"--wrap-max-lines=unlimited",
}
if useSideBySide {
args = append(args, "--side-by-side")
Expand Down Expand Up @@ -340,7 +348,9 @@ func diffDir(dir *cachedNode, width int, sideBySide bool, preamble string) tea.C
fmt.Sprintf("--file-style='%s bold %s'", c, c),
fmt.Sprintf("--file-decoration-style='%s box %s'", c, c),
fmt.Sprintf("-w=%d", width),
fmt.Sprintf("--max-line-length=%d", width),
// See `diffFile` for why these are set this way.
"--max-line-length=0",
"--wrap-max-lines=unlimited",
}
if useSideBySide {
args = append(args, "--side-by-side")
Expand Down
8 changes: 8 additions & 0 deletions pkg/ui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,14 @@ func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
} else {
m.diffViewer.ScrollTop()
}
case key.Matches(msg, keys.ScrollLeft):
if m.activePanel != FileTreePanel {
m.diffViewer.ScrollLeft()
}
case key.Matches(msg, keys.ScrollRight):
if m.activePanel != FileTreePanel {
m.diffViewer.ScrollRight()
}
case key.Matches(msg, keys.Copy):
cmd = m.fileTree.CopyCurrNodePath()
if cmd != nil {
Expand Down
Loading