Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/v2-exp' into v2-area
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 committed Feb 11, 2025
2 parents 06f1729 + 4491afa commit 468abfc
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 82 deletions.
72 changes: 36 additions & 36 deletions filepicker/filepicker.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ func New() Model {
FileAllowed: true,
AutoHeight: true,
height: 0,
max: 0,
min: 0,
maxIdx: 0,
minIdx: 0,
selectedStack: newStack(),
minStack: newStack(),
maxStack: newStack(),
Expand Down Expand Up @@ -147,8 +147,8 @@ type Model struct {
selected int
selectedStack stack

min int
max int
minIdx int
maxIdx int
maxStack stack
minStack stack

Expand Down Expand Up @@ -182,10 +182,10 @@ func newStack() stack {
}
}

func (m *Model) pushView(selected, min, max int) {
func (m *Model) pushView(selected, minIdx, maxIdx int) {
m.selectedStack.Push(selected)
m.minStack.Push(min)
m.maxStack.Push(max)
m.minStack.Push(minIdx)
m.maxStack.Push(maxIdx)
}

func (m *Model) popView() (int, int, int) {
Expand Down Expand Up @@ -245,72 +245,72 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
break
}
m.files = msg.entries
m.max = max(m.max, m.Height()-1)
m.maxIdx = max(m.maxIdx, m.Height()-1)
case tea.WindowSizeMsg:
if m.AutoHeight {
m.SetHeight(msg.Height - marginBottom)
}
m.max = m.Height() - 1
m.maxIdx = m.Height() - 1
case tea.KeyPressMsg:
switch {
case key.Matches(msg, m.KeyMap.GoToTop):
m.selected = 0
m.min = 0
m.max = m.Height() - 1
m.minIdx = 0
m.maxIdx = m.Height() - 1
case key.Matches(msg, m.KeyMap.GoToLast):
m.selected = len(m.files) - 1
m.min = len(m.files) - m.Height()
m.max = len(m.files) - 1
m.minIdx = len(m.files) - m.Height()
m.maxIdx = len(m.files) - 1
case key.Matches(msg, m.KeyMap.Down):
m.selected++
if m.selected >= len(m.files) {
m.selected = len(m.files) - 1
}
if m.selected > m.max {
m.min++
m.max++
if m.selected > m.maxIdx {
m.minIdx++
m.maxIdx++
}
case key.Matches(msg, m.KeyMap.Up):
m.selected--
if m.selected < 0 {
m.selected = 0
}
if m.selected < m.min {
m.min--
m.max--
if m.selected < m.minIdx {
m.minIdx--
m.maxIdx--
}
case key.Matches(msg, m.KeyMap.PageDown):
m.selected += m.Height()
if m.selected >= len(m.files) {
m.selected = len(m.files) - 1
}
m.min += m.Height()
m.max += m.Height()
m.minIdx += m.Height()
m.maxIdx += m.Height()

if m.max >= len(m.files) {
m.max = len(m.files) - 1
m.min = m.max - m.Height()
if m.maxIdx >= len(m.files) {
m.maxIdx = len(m.files) - 1
m.minIdx = m.maxIdx - m.Height()
}
case key.Matches(msg, m.KeyMap.PageUp):
m.selected -= m.Height()
if m.selected < 0 {
m.selected = 0
}
m.min -= m.Height()
m.max -= m.Height()
m.minIdx -= m.Height()
m.maxIdx -= m.Height()

if m.min < 0 {
m.min = 0
m.max = m.min + m.Height()
if m.minIdx < 0 {
m.minIdx = 0
m.maxIdx = m.minIdx + m.Height()
}
case key.Matches(msg, m.KeyMap.Back):
m.CurrentDirectory = filepath.Dir(m.CurrentDirectory)
if m.selectedStack.Length() > 0 {
m.selected, m.min, m.max = m.popView()
m.selected, m.minIdx, m.maxIdx = m.popView()
} else {
m.selected = 0
m.min = 0
m.max = m.Height() - 1
m.minIdx = 0
m.maxIdx = m.Height() - 1
}
return m, m.readDir(m.CurrentDirectory, m.ShowHidden)
case key.Matches(msg, m.KeyMap.Open):
Expand Down Expand Up @@ -349,10 +349,10 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
}

m.CurrentDirectory = filepath.Join(m.CurrentDirectory, f.Name())
m.pushView(m.selected, m.min, m.max)
m.pushView(m.selected, m.minIdx, m.maxIdx)
m.selected = 0
m.min = 0
m.max = m.Height() - 1
m.minIdx = 0
m.maxIdx = m.Height() - 1
return m, m.readDir(m.CurrentDirectory, m.ShowHidden)
}
}
Expand All @@ -367,7 +367,7 @@ func (m Model) View() string {
var s strings.Builder

for i, f := range m.files {
if i < m.min || i > m.max {
if i < m.minIdx || i > m.maxIdx {
continue
}

Expand Down
4 changes: 2 additions & 2 deletions filepicker/hidden_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (
func IsHidden(file string) (bool, error) {
pointer, err := syscall.UTF16PtrFromString(file)
if err != nil {
return false, err
return false, err //nolint:wrapcheck
}
attributes, err := syscall.GetFileAttributes(pointer)
if err != nil {
return false, err
return false, err //nolint:wrapcheck
}
return attributes&syscall.FILE_ATTRIBUTE_HIDDEN != 0, nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/charmbracelet/harmonica v0.2.0
github.com/charmbracelet/lipgloss/v2 v2.0.0-alpha.2.0.20250204145343-96725424379d
github.com/charmbracelet/x/ansi v0.8.0
github.com/charmbracelet/x/exp/golden v0.0.0-20241212170349-ad4b7ae0f25f
github.com/charmbracelet/x/exp/golden v0.0.0-20250207160936-21c02780d27a
github.com/dustin/go-humanize v1.0.1
github.com/lucasb-eyer/go-colorful v1.2.0
github.com/mattn/go-runewidth v0.0.16
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ github.com/charmbracelet/x/ansi v0.8.0 h1:9GTq3xq9caJW8ZrBTe0LIe2fvfLR/bYXKTx2ll
github.com/charmbracelet/x/ansi v0.8.0/go.mod h1:wdYl/ONOLHLIVmQaxbIYEC/cRKOQyjTkowiI4blgS9Q=
github.com/charmbracelet/x/cellbuf v0.0.9-0.20250203222631-bea22a7f0a07 h1:RFHEvURPMgGGd8epjjhi2UpXSKyFs39iRF4JTYCEdLg=
github.com/charmbracelet/x/cellbuf v0.0.9-0.20250203222631-bea22a7f0a07/go.mod h1:dKfNBxLovpvzzxAP6/GZfs5eb7vNxHlUDnwGhRmvIdY=
github.com/charmbracelet/x/exp/golden v0.0.0-20241212170349-ad4b7ae0f25f h1:UytXHv0UxnsDFmL/7Z9Q5SBYPwSuRLXHbwx+6LycZ2w=
github.com/charmbracelet/x/exp/golden v0.0.0-20241212170349-ad4b7ae0f25f/go.mod h1:wDlXFlCrmJ8J+swcL/MnGUuYnqgQdW9rhSD61oNMb6U=
github.com/charmbracelet/x/exp/golden v0.0.0-20250207160936-21c02780d27a h1:FsHEJ52OC4VuTzU8t+n5frMjLvpYWEznSr/u8tnkCYw=
github.com/charmbracelet/x/exp/golden v0.0.0-20250207160936-21c02780d27a/go.mod h1:wDlXFlCrmJ8J+swcL/MnGUuYnqgQdW9rhSD61oNMb6U=
github.com/charmbracelet/x/input v0.3.1 h1:TE4s3fTRj+OUpJ86dKphrN99+NgBnto//EkWncMJQIg=
github.com/charmbracelet/x/input v0.3.1/go.mod h1:4w9jS/NW62WrHSdmjbpzydvnbqkd+mtyK8WOWbHCdvs=
github.com/charmbracelet/x/term v0.2.1 h1:AQeHeLZ1OqSXhrAWpYUtZyX1T3zVxfpZuEQMIQaGIAQ=
Expand Down
14 changes: 7 additions & 7 deletions textarea/textarea.go
Original file line number Diff line number Diff line change
Expand Up @@ -975,13 +975,13 @@ func (m Model) LineInfo() LineInfo {
// repositionView repositions the view of the viewport based on the defined
// scrolling behavior.
func (m *Model) repositionView() {
min := m.viewport.YOffset
max := min + m.viewport.Height() - 1
minOffset := m.viewport.YOffset
maxOffset := minOffset + m.viewport.Height() - 1

if row := m.cursorLineNumber(); row < min {
m.viewport.LineUp(min - row)
} else if row > max {
m.viewport.LineDown(row - max)
if row := m.cursorLineNumber(); row < minOffset {
m.viewport.LineUp(minOffset - row)
} else if row > maxOffset {
m.viewport.LineDown(row - maxOffset)
}
}

Expand Down Expand Up @@ -1300,7 +1300,7 @@ func (m Model) View() string {
displayLine++

var ln string
if m.ShowLineNumbers { //nolint:nestif
if m.ShowLineNumbers {
if wl == 0 { // normal line
isCursorLine := m.row == l
s.WriteString(m.lineNumberView(l+1, isCursorLine))
Expand Down
Loading

0 comments on commit 468abfc

Please sign in to comment.