From 97feb1b4d031eb3f2ac5ccb20fa1bfd368db0655 Mon Sep 17 00:00:00 2001 From: Mikael Fangel <34864484+MikaelFangel@users.noreply.github.com> Date: Tue, 14 Mar 2023 20:58:48 +0100 Subject: [PATCH] feat: adds headers for choose and filter (#307) * added header to choose * corrected mistake in envvar * added header for filter * simplified return logic for filter * Update choose/options.go * render the header before calculating the height --------- Co-authored-by: Maas Lalani --- choose/choose.go | 13 +++++++++---- choose/command.go | 2 ++ choose/options.go | 2 ++ filter/command.go | 2 ++ filter/filter.go | 19 +++++++++++++++++-- filter/options.go | 2 ++ 6 files changed, 34 insertions(+), 6 deletions(-) diff --git a/choose/choose.go b/choose/choose.go index 2b1b113..4bd3937 100644 --- a/choose/choose.go +++ b/choose/choose.go @@ -25,6 +25,7 @@ type model struct { selectedPrefix string unselectedPrefix string cursorPrefix string + header string items []item quitting bool index int @@ -36,6 +37,7 @@ type model struct { // styles cursorStyle lipgloss.Style + headerStyle lipgloss.Style itemStyle lipgloss.Style selectedItemStyle lipgloss.Style } @@ -171,12 +173,15 @@ func (m model) View() string { } } - if m.paginator.TotalPages <= 1 { - return s.String() + if m.paginator.TotalPages > 1 { + s.WriteString(strings.Repeat("\n", m.height-m.paginator.ItemsOnPage(len(m.items))+1)) + s.WriteString(" " + m.paginator.View()) } - s.WriteString(strings.Repeat("\n", m.height-m.paginator.ItemsOnPage(len(m.items))+1)) - s.WriteString(" " + m.paginator.View()) + if m.header != "" { + header := m.headerStyle.Render(m.header) + return lipgloss.JoinVertical(lipgloss.Left, header, s.String()) + } return s.String() } diff --git a/choose/command.go b/choose/command.go index 5277ab2..a6444b3 100644 --- a/choose/command.go +++ b/choose/command.go @@ -98,6 +98,7 @@ func (o Options) Run() error { currentOrder: currentOrder, height: o.Height, cursor: o.Cursor, + header: o.Header, selectedPrefix: o.SelectedPrefix, unselectedPrefix: o.UnselectedPrefix, cursorPrefix: o.CursorPrefix, @@ -105,6 +106,7 @@ func (o Options) Run() error { limit: o.Limit, paginator: pager, cursorStyle: o.CursorStyle.ToLipgloss(), + headerStyle: o.HeaderStyle.ToLipgloss(), itemStyle: o.ItemStyle.ToLipgloss(), selectedItemStyle: o.SelectedItemStyle.ToLipgloss(), numSelected: currentSelected, diff --git a/choose/options.go b/choose/options.go index f6ee123..064718c 100644 --- a/choose/options.go +++ b/choose/options.go @@ -11,11 +11,13 @@ type Options struct { Ordered bool `help:"Maintain the order of the selected options" env:"GUM_CHOOSE_ORDERED"` Height int `help:"Height of the list" default:"10" env:"GUM_CHOOSE_HEIGHT"` Cursor string `help:"Prefix to show on item that corresponds to the cursor position" default:"> " env:"GUM_CHOOSE_CURSOR"` + Header string `help:"Header value" default:"" env:"GUM_CHOOSE_HEADER"` CursorPrefix string `help:"Prefix to show on the cursor item (hidden if limit is 1)" default:"○ " env:"GUM_CHOOSE_CURSOR_PREFIX"` SelectedPrefix string `help:"Prefix to show on selected items (hidden if limit is 1)" default:"◉ " env:"GUM_CHOOSE_SELECTED_PREFIX"` UnselectedPrefix string `help:"Prefix to show on unselected items (hidden if limit is 1)" default:"○ " env:"GUM_CHOOSE_UNSELECTED_PREFIX"` Selected []string `help:"Options that should start as selected" default:"" env:"GUM_CHOOSE_SELECTED"` CursorStyle style.Styles `embed:"" prefix:"cursor." set:"defaultForeground=212" envprefix:"GUM_CHOOSE_CURSOR_"` + HeaderStyle style.Styles `embed:"" prefix:"header." set:"defaultForeground=240" envprefix:"GUM_CHOOSE_HEADER_"` ItemStyle style.Styles `embed:"" prefix:"item." hidden:"" envprefix:"GUM_CHOOSE_ITEM_"` SelectedItemStyle style.Styles `embed:"" prefix:"selected." set:"defaultForeground=212" envprefix:"GUM_CHOOSE_SELECTED_"` } diff --git a/filter/command.go b/filter/command.go index 2a37d71..8a73e41 100644 --- a/filter/command.go +++ b/filter/command.go @@ -71,6 +71,7 @@ func (o Options) Run() error { choices: choices, indicator: o.Indicator, matches: matches, + header: o.Header, textinput: i, viewport: &v, indicatorStyle: o.IndicatorStyle.ToLipgloss(), @@ -79,6 +80,7 @@ func (o Options) Run() error { unselectedPrefixStyle: o.UnselectedPrefixStyle.ToLipgloss(), unselectedPrefix: o.UnselectedPrefix, matchStyle: o.MatchStyle.ToLipgloss(), + headerStyle: o.HeaderStyle.ToLipgloss(), textStyle: o.TextStyle.ToLipgloss(), height: o.Height, selected: make(map[string]struct{}), diff --git a/filter/filter.go b/filter/filter.go index 9e5a4c1..fb6f0e0 100644 --- a/filter/filter.go +++ b/filter/filter.go @@ -27,6 +27,7 @@ type model struct { choices []string matches []fuzzy.Match cursor int + header string selected map[string]struct{} limit int numSelected int @@ -36,6 +37,7 @@ type model struct { height int aborted bool quitting bool + headerStyle lipgloss.Style matchStyle lipgloss.Style textStyle lipgloss.Style indicatorStyle lipgloss.Style @@ -121,10 +123,18 @@ func (m model) View() string { m.viewport.SetContent(s.String()) // View the input and the filtered choices + header := m.headerStyle.Render(m.header) if m.reverse { - return m.viewport.View() + "\n" + m.textinput.View() + view := m.viewport.View() + "\n" + m.textinput.View() + if m.header != "" { + return lipgloss.JoinVertical(lipgloss.Left, view, header) + } + + return view } - return m.textinput.View() + "\n" + m.viewport.View() + + view := m.textinput.View() + "\n" + m.viewport.View() + return lipgloss.JoinVertical(lipgloss.Left, header, view) } func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { @@ -134,6 +144,11 @@ 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 + if m.header != "" { + m.viewport.Height = m.viewport.Height - lipgloss.Height(m.headerStyle.Render(m.header)) + } m.viewport.Width = msg.Width if m.reverse { m.viewport.YOffset = clamp(0, len(m.matches), len(m.matches)-m.viewport.Height) diff --git a/filter/options.go b/filter/options.go index 07e3551..32f72eb 100644 --- a/filter/options.go +++ b/filter/options.go @@ -13,6 +13,8 @@ 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_"` + Header string `help:"Header value" default:"" env:"GUM_FILTER_HEADER"` TextStyle style.Styles `embed:"" prefix:"text." envprefix:"GUM_FILTER_TEXT_"` MatchStyle style.Styles `embed:"" prefix:"match." set:"defaultForeground=212" envprefix:"GUM_FILTER_MATCH_"` Placeholder string `help:"Placeholder value" default:"Filter..." env:"GUM_FILTER_PLACEHOLDER"`