mirror of
https://github.com/charmbracelet/gum
synced 2024-05-08 01:06:37 +02:00
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 <maas@lalani.dev>
This commit is contained in:
parent
ec2b8d0fee
commit
97feb1b4d0
|
@ -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())
|
||||
}
|
||||
|
||||
if m.header != "" {
|
||||
header := m.headerStyle.Render(m.header)
|
||||
return lipgloss.JoinVertical(lipgloss.Left, header, s.String())
|
||||
}
|
||||
|
||||
return s.String()
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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_"`
|
||||
}
|
||||
|
|
|
@ -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{}),
|
||||
|
|
|
@ -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 m.textinput.View() + "\n" + m.viewport.View()
|
||||
|
||||
return 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)
|
||||
|
|
|
@ -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"`
|
||||
|
|
Loading…
Reference in a new issue