Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tab backwards #28

Merged
merged 1 commit into from
May 22, 2024
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
47 changes: 39 additions & 8 deletions ui/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ui
import (
"fmt"
"os/exec"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -88,6 +89,28 @@ func (m *Diff) windowSizeUpdate(msg tea.WindowSizeMsg) tea.Cmd {
}
}

func (m Diff) FileHeights() []int {
heights := []int{0}
for i := range m.files {
height := m.files[i].Height(m.viewport.Width) - 1
total := height + heights[len(heights)-1]
heights = append(heights, total)
}
return heights
}

func (m Diff) getFileIndexInViewport() int {
index := slices.IndexFunc(m.FileHeights(), func(i int) bool {
offset := m.viewport.YOffset - 1
return offset-i <= -2
}) - 1

if index >= 0 && len(m.files) > 0 {
return index
}
return m.currentFile
}

func (m Diff) Update(msg tea.Msg) (Diff, tea.Cmd) {
var (
cmd tea.Cmd
Expand All @@ -102,19 +125,25 @@ func (m Diff) Update(msg tea.Msg) (Diff, tea.Cmd) {
cmd = m.Tick(true)
cmds = append(cmds, cmd)
}
if k == "tab" {
heights := []int{0}
for i := 0; i < len(m.files); i++ {
height := lipgloss.Height(m.files[i].View(m.viewport.Width)) - 1
total := height + heights[len(heights)-1]
heights = append(heights, total)
if k == "shift+tab" {
// decrement the current file only if the first line of the
// file is visible in the viewport. If the viewport has scrolled
// past the top of the file, shift+tab should scroll to the top
// of the file.
if slices.Contains(m.FileHeights(), m.viewport.YOffset) {
m.currentFile -= 1
if m.currentFile < 0 || m.viewport.AtTop() {
m.currentFile = len(m.files) - 1
}
}

m.viewport.SetYOffset(m.FileHeights()[m.currentFile])
}
if k == "tab" {
m.currentFile += 1
if m.currentFile == len(m.files) || m.viewport.AtBottom() {
m.currentFile = 0
}
m.viewport.SetYOffset(heights[m.currentFile])
m.viewport.SetYOffset(m.FileHeights()[m.currentFile])
}
case TickMsg:
diff, _ := parser.ParseDiff(m.gitDiffRaw())
Expand All @@ -130,6 +159,8 @@ func (m Diff) Update(msg tea.Msg) (Diff, tea.Cmd) {
cmds = append(cmds, cmd)
}

m.currentFile = m.getFileIndexInViewport()

m.viewport, cmd = m.viewport.Update(msg)
cmds = append(cmds, cmd)

Expand Down
4 changes: 4 additions & 0 deletions ui/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ func NewFile(file parser.File) File {

var lineNumberStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#aaa"))

func (m File) Height(viewportWidth int) int {
return lipgloss.Height(m.View(viewportWidth))
}

func (m File) View(viewportWidth int) string {
var content strings.Builder
content.WriteString(fileStyle.Width(viewportWidth).Render(m.Name))
Expand Down
Loading