Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/huh-rev' into interrupt
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 committed Dec 6, 2024
2 parents 46a9fbd + 09a33c2 commit f072411
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 16 deletions.
4 changes: 3 additions & 1 deletion choose/choose.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,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 @@ -89,7 +91,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
13 changes: 12 additions & 1 deletion choose/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,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)
}

m := model{
index: startingIndex,
currentOrder: currentOrder,
Expand All @@ -120,8 +129,10 @@ func (o Options) Run() error {
numSelected: currentSelected,
showHelp: o.ShowHelp,
help: help.New(),
keymap: defaultKeymap(),
keymap: km,
}

// Disable Keybindings since we will control it ourselves.
tm, err := tea.NewProgram(
m,
tea.WithOutput(os.Stderr),
Expand Down
14 changes: 9 additions & 5 deletions file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ func (m model) Init() tea.Cmd {

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
if m.showHelp {
m.filepicker.Height -= lipgloss.Height(m.helpView())
}
case tea.KeyMsg:
switch {
case key.Matches(msg, keyAbort):
Expand Down Expand Up @@ -98,9 +102,9 @@ func (m model) View() string {
if !m.showHelp {
return m.filepicker.View()
}
return lipgloss.JoinVertical(
lipgloss.Top,
m.filepicker.View(),
m.help.View(m.keymap),
)
return m.filepicker.View() + m.helpView()
}

func (m model) helpView() string {
return "\n" + m.help.View(m.keymap)
}
9 changes: 8 additions & 1 deletion filter/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,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)
}

m := model{
choices: o.Options,
Expand All @@ -105,7 +112,7 @@ func (o Options) Run() error {
sort: o.Sort && o.FuzzySort,
strict: o.Strict,
showHelp: o.ShowHelp,
keymap: defaultKeymap(),
keymap: km,
help: help.New(),
}
p := tea.NewProgram(m, options...)
Expand Down
21 changes: 14 additions & 7 deletions filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,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 @@ -76,7 +79,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 @@ -199,15 +202,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 @@ -218,28 +221,32 @@ 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) {
case tea.WindowSizeMsg:
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

0 comments on commit f072411

Please sign in to comment.