Skip to content

Commit

Permalink
feat: filter height and scrolling viewport
Browse files Browse the repository at this point in the history
  • Loading branch information
maaslalani committed Aug 2, 2022
1 parent fb4a9e6 commit c049d1a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
5 changes: 5 additions & 0 deletions filter/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/alecthomas/kong"
"github.com/charmbracelet/bubbles/textinput"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/gum/internal/exit"
"github.com/charmbracelet/gum/internal/files"
Expand All @@ -25,6 +26,8 @@ func (o Options) Run() error {
i.Placeholder = o.Placeholder
i.Width = o.Width

v := viewport.New(o.Width, o.Height)

var choices []string
if input, _ := stdin.Read(); input != "" {
choices = strings.Split(strings.TrimSpace(input), "\n")
Expand All @@ -37,9 +40,11 @@ func (o Options) Run() error {
indicator: o.Indicator,
matches: matchAll(choices),
textinput: i,
viewport: &v,
indicatorStyle: o.IndicatorStyle.ToLipgloss(),
matchStyle: o.MatchStyle.ToLipgloss(),
textStyle: o.TextStyle.ToLipgloss(),
height: o.Height,
}, tea.WithOutput(os.Stderr))

tm, err := p.StartReturningModel()
Expand Down
21 changes: 16 additions & 5 deletions filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"strings"

"github.com/charmbracelet/bubbles/textinput"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/mattn/go-runewidth"
Expand All @@ -23,6 +24,7 @@ import (

type model struct {
textinput textinput.Model
viewport *viewport.Model
choices []string
matches []fuzzy.Match
selected int
Expand Down Expand Up @@ -78,29 +80,38 @@ func (m model) View() string {
s.WriteRune('\n')
}

tv := m.textinput.View()
results := lipgloss.NewStyle().MaxHeight(m.height - lipgloss.Height(tv)).Render(s.String())
m.viewport.SetContent(s.String())

// View the input and the filtered choices
return tv + "\n" + results
return m.textinput.View() + "\n" + m.viewport.View()
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.height = msg.Height
if m.height == 0 || m.height > msg.Height {
m.viewport.Height = msg.Height - lipgloss.Height(m.textinput.View())
}
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "esc":
m.aborted = true
fallthrough
m.quitting = true
return m, tea.Quit
case "enter":
m.quitting = true
return m, tea.Quit
case "ctrl+n", "ctrl+j", "down":
m.selected = clamp(0, len(m.matches)-1, m.selected+1)
if m.selected >= m.viewport.YOffset+m.viewport.Height {
m.viewport.LineDown(1)
}
case "ctrl+p", "ctrl+k", "up":
m.selected = clamp(0, len(m.matches)-1, m.selected-1)
if m.selected < m.viewport.YOffset {
m.viewport.SetYOffset(m.selected)
}
default:
m.textinput, cmd = m.textinput.Update(msg)

Expand Down
1 change: 1 addition & 0 deletions filter/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ type Options struct {
Prompt string `help:"Prompt to display" default:"> "`
PromptStyle style.Styles `embed:"" prefix:"prompt." set:"defaultForeground=240" set:"name=prompt"`
Width int `help:"Input width" default:"20"`
Height int `help:"Input height" default:"0"`
}

0 comments on commit c049d1a

Please sign in to comment.