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

reverting huh: small fixes and improvements #743

Merged
merged 7 commits into from
Dec 6, 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
4 changes: 3 additions & 1 deletion choose/choose.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ func defaultKeymap() keymap {
ToggleAll: key.NewBinding(
key.WithKeys("a", "A", "ctrl+a"),
key.WithHelp("ctrl+a", "select all"),
key.WithDisabled(),
),
Toggle: key.NewBinding(
key.WithKeys(" ", "tab", "x", "ctrl+@"),
key.WithHelp("x", "toggle"),
key.WithDisabled(),
),
Abort: key.NewBinding(
key.WithKeys("ctrl+c", "esc"),
Expand Down Expand Up @@ -91,7 +93,7 @@ func (k keymap) ShortHelp() []key.Binding {
k.Toggle,
key.NewBinding(
key.WithKeys("up", "down", "right", "left"),
key.WithHelp("↑↓←→", "navigation"),
key.WithHelp("↑↓←→", "navigate"),
),
k.Submit,
k.ToggleAll,
Expand Down
11 changes: 10 additions & 1 deletion choose/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ func (o Options) Run() error {
pager.InactiveDot = verySubduedStyle.Render("•")
pager.KeyMap = paginator.KeyMap{}
pager.Page = startingIndex / o.Height

km := defaultKeymap()
if o.NoLimit || o.Limit > 1 {
km.Toggle.SetEnabled(true)
}
if o.NoLimit {
km.ToggleAll.SetEnabled(true)
}

// Disable Keybindings since we will control it ourselves.
tm, err := tea.NewProgram(model{
index: startingIndex,
Expand All @@ -115,7 +124,7 @@ func (o Options) Run() error {
timeout: o.Timeout,
showHelp: o.ShowHelp,
help: help.New(),
keymap: defaultKeymap(),
keymap: km,
}, tea.WithOutput(os.Stderr)).Run()
if err != nil {
return fmt.Errorf("failed to start tea program: %w", err)
Expand Down
9 changes: 8 additions & 1 deletion filter/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,16 @@ func (o Options) Run() error {
matches = matchAll(o.Options)
}

km := defaultKeymap()

if o.NoLimit {
o.Limit = len(o.Options)
}
if o.NoLimit || o.Limit > 1 {
km.Toggle.SetEnabled(true)
km.ToggleAndPrevious.SetEnabled(true)
km.ToggleAndNext.SetEnabled(true)
}

p := tea.NewProgram(model{
choices: o.Options,
Expand Down Expand Up @@ -98,7 +105,7 @@ func (o Options) Run() error {
sort: o.Sort && o.FuzzySort,
strict: o.Strict,
showHelp: o.ShowHelp,
keymap: defaultKeymap(),
keymap: km,
help: help.New(),
}, options...)

Expand Down
21 changes: 14 additions & 7 deletions filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,17 @@ func defaultKeymap() keymap {
ToggleAndNext: key.NewBinding(
key.WithKeys("tab"),
key.WithHelp("tab", "toggle"),
key.WithDisabled(),
),
ToggleAndPrevious: key.NewBinding(
key.WithKeys("shift+tab"),
key.WithHelp("shift+tab", "toggle"),
key.WithDisabled(),
),
Toggle: key.NewBinding(
key.WithKeys("ctrl+@"),
key.WithHelp("ctrl+@", "toggle"),
key.WithDisabled(),
),
Abort: key.NewBinding(
key.WithKeys("ctrl+c", "esc"),
Expand Down Expand Up @@ -79,7 +82,7 @@ func (k keymap) ShortHelp() []key.Binding {
k.ToggleAndNext,
key.NewBinding(
key.WithKeys("up", "down"),
key.WithHelp("↑↓", "navigation"),
key.WithHelp("↑↓", "navigate"),
),
k.Submit,
}
Expand Down Expand Up @@ -205,15 +208,15 @@ func (m model) View() string {

help := ""
if m.showHelp {
help = m.help.View(m.keymap)
help = m.helpView()
}

// View the input and the filtered choices
header := m.headerStyle.Render(m.header)
if m.reverse {
view := m.viewport.View() + "\n" + m.textinput.View()
if m.showHelp {
view += "\n" + help
view += help
}
if m.header != "" {
return lipgloss.JoinVertical(lipgloss.Left, view, header)
Expand All @@ -224,14 +227,18 @@ func (m model) View() string {

view := m.textinput.View() + "\n" + m.viewport.View()
if m.showHelp {
view += "\n" + help
view += help
}
if m.header != "" {
return lipgloss.JoinVertical(lipgloss.Left, header, view)
}
return view
}

func (m model) helpView() string {
return "\n\n" + m.help.View(m.keymap)
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
Expand All @@ -248,13 +255,13 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if m.height == 0 || m.height > msg.Height {
m.viewport.Height = msg.Height - lipgloss.Height(m.textinput.View())
}

// Make place in the view port if header is set
// Include the header in the height calculation.
if m.header != "" {
m.viewport.Height = m.viewport.Height - lipgloss.Height(m.headerStyle.Render(m.header))
}
// Include the help in the total height calculation.
if m.showHelp {
m.viewport.Height = m.viewport.Height - lipgloss.Height(m.help.View(m.keymap))
m.viewport.Height = m.viewport.Height - lipgloss.Height(m.helpView())
}
m.viewport.Width = msg.Width
if m.reverse {
Expand Down
2 changes: 1 addition & 1 deletion filter/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Options struct {
SelectedPrefixStyle style.Styles `embed:"" prefix:"selected-indicator." set:"defaultForeground=212" envprefix:"GUM_FILTER_SELECTED_PREFIX_"`
UnselectedPrefix string `help:"Character to indicate unselected items (hidden if limit is 1)" default:" ○ " env:"GUM_FILTER_UNSELECTED_PREFIX"`
UnselectedPrefixStyle style.Styles `embed:"" prefix:"unselected-prefix." set:"defaultForeground=240" envprefix:"GUM_FILTER_UNSELECTED_PREFIX_"`
HeaderStyle style.Styles `embed:"" prefix:"header." set:"defaultForeground=240" envprefix:"GUM_FILTER_HEADER_"`
HeaderStyle style.Styles `embed:"" prefix:"header." set:"defaultForeground=99" envprefix:"GUM_FILTER_HEADER_"`
Header string `help:"Header value" default:"" env:"GUM_FILTER_HEADER"`
TextStyle style.Styles `embed:"" prefix:"text." envprefix:"GUM_FILTER_TEXT_"`
CursorTextStyle style.Styles `embed:"" prefix:"cursor-text." envprefix:"GUM_FILTER_CURSOR_TEXT_"`
Expand Down
Loading