diff --git a/filepicker/filepicker.go b/filepicker/filepicker.go index 7966fc9..4b6f5ac 100644 --- a/filepicker/filepicker.go +++ b/filepicker/filepicker.go @@ -35,7 +35,7 @@ func New() Model { DirAllowed: false, FileAllowed: true, AutoHeight: true, - Height: 0, + height: 0, max: 0, min: 0, selectedStack: newStack(), @@ -152,7 +152,7 @@ type Model struct { maxStack stack minStack stack - Height int + height int AutoHeight bool Cursor string @@ -222,6 +222,16 @@ func (m Model) readDir(path string, showHidden bool) tea.Cmd { } } +// SetHeight sets the height of the file picker. +func (m *Model) SetHeight(h int) { + m.height = h +} + +// Height returns the height of the file picker. +func (m Model) Height() int { + return m.height +} + // Init initializes the file picker model. func (m Model) Init() (Model, tea.Cmd) { return m, m.readDir(m.CurrentDirectory, m.ShowHidden) @@ -235,21 +245,21 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) { break } m.files = msg.entries - m.max = max(m.max, m.Height-1) + m.max = max(m.max, m.Height()-1) case tea.WindowSizeMsg: if m.AutoHeight { - m.Height = msg.Height - marginBottom + m.SetHeight(msg.Height - marginBottom) } - m.max = m.Height - 1 + m.max = 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.max = m.Height() - 1 case key.Matches(msg, m.KeyMap.GoToLast): m.selected = len(m.files) - 1 - m.min = len(m.files) - m.Height + m.min = len(m.files) - m.Height() m.max = len(m.files) - 1 case key.Matches(msg, m.KeyMap.Down): m.selected++ @@ -270,28 +280,28 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) { m.max-- } case key.Matches(msg, m.KeyMap.PageDown): - m.selected += m.Height + 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.min += m.Height() + m.max += m.Height() if m.max >= len(m.files) { m.max = len(m.files) - 1 - m.min = m.max - m.Height + m.min = m.max - m.Height() } case key.Matches(msg, m.KeyMap.PageUp): - m.selected -= m.Height + m.selected -= m.Height() if m.selected < 0 { m.selected = 0 } - m.min -= m.Height - m.max -= m.Height + m.min -= m.Height() + m.max -= m.Height() if m.min < 0 { m.min = 0 - m.max = m.min + m.Height + m.max = m.min + m.Height() } case key.Matches(msg, m.KeyMap.Back): m.CurrentDirectory = filepath.Dir(m.CurrentDirectory) @@ -300,7 +310,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) { } else { m.selected = 0 m.min = 0 - m.max = m.Height - 1 + m.max = m.Height() - 1 } return m, m.readDir(m.CurrentDirectory, m.ShowHidden) case key.Matches(msg, m.KeyMap.Open): @@ -342,7 +352,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) { m.pushView(m.selected, m.min, m.max) m.selected = 0 m.min = 0 - m.max = m.Height - 1 + m.max = m.Height() - 1 return m, m.readDir(m.CurrentDirectory, m.ShowHidden) } } @@ -352,7 +362,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) { // View returns the view of the file picker. func (m Model) View() string { if len(m.files) == 0 { - return m.Styles.EmptyDirectory.Height(m.Height).MaxHeight(m.Height).String() + return m.Styles.EmptyDirectory.Height(m.Height()).MaxHeight(m.Height()).String() } var s strings.Builder @@ -418,7 +428,7 @@ func (m Model) View() string { s.WriteRune('\n') } - for i := lipgloss.Height(s.String()); i <= m.Height; i++ { + for i := lipgloss.Height(s.String()); i <= m.Height(); i++ { s.WriteRune('\n') }