feat(filter): add cursor text line styling

This commit is contained in:
ROMAIN GUISSET 2023-05-24 12:11:20 +00:00 committed by Maas Lalani
parent 6aac40560f
commit 99f1348a45
3 changed files with 10 additions and 2 deletions

View file

@ -84,6 +84,7 @@ func (o Options) Run() error {
matchStyle: o.MatchStyle.ToLipgloss(), matchStyle: o.MatchStyle.ToLipgloss(),
headerStyle: o.HeaderStyle.ToLipgloss(), headerStyle: o.HeaderStyle.ToLipgloss(),
textStyle: o.TextStyle.ToLipgloss(), textStyle: o.TextStyle.ToLipgloss(),
cursorTextStyle: o.CursorTextStyle.ToLipgloss(),
height: o.Height, height: o.Height,
selected: make(map[string]struct{}), selected: make(map[string]struct{}),
limit: o.Limit, limit: o.Limit,

View file

@ -39,6 +39,7 @@ type model struct {
headerStyle lipgloss.Style headerStyle lipgloss.Style
matchStyle lipgloss.Style matchStyle lipgloss.Style
textStyle lipgloss.Style textStyle lipgloss.Style
cursorTextStyle lipgloss.Style
indicatorStyle lipgloss.Style indicatorStyle lipgloss.Style
selectedPrefixStyle lipgloss.Style selectedPrefixStyle lipgloss.Style
unselectedPrefixStyle lipgloss.Style unselectedPrefixStyle lipgloss.Style
@ -54,6 +55,7 @@ func (m model) View() string {
} }
var s strings.Builder var s strings.Builder
var lineTextStyle lipgloss.Style
// For reverse layout, if the number of matches is less than the viewport // For reverse layout, if the number of matches is less than the viewport
// height, we need to offset the matches so that the first match is at the // height, we need to offset the matches so that the first match is at the
@ -74,10 +76,14 @@ func (m model) View() string {
// If this is the current selected index, we add a small indicator to // If this is the current selected index, we add a small indicator to
// represent it. Otherwise, simply pad the string. // represent it. Otherwise, simply pad the string.
// The line's text style is set depending on whether or not the cursor
// points to this line.
if i == m.cursor { if i == m.cursor {
s.WriteString(m.indicatorStyle.Render(m.indicator)) s.WriteString(m.indicatorStyle.Render(m.indicator))
lineTextStyle = m.cursorTextStyle
} else { } else {
s.WriteString(strings.Repeat(" ", lipgloss.Width(m.indicator))) s.WriteString(strings.Repeat(" ", lipgloss.Width(m.indicator)))
lineTextStyle = m.textStyle
} }
// If there are multiple selections mark them, otherwise leave an empty space // If there are multiple selections mark them, otherwise leave an empty space
@ -99,7 +105,7 @@ func (m model) View() string {
// index. If so, color the character to indicate a match. // index. If so, color the character to indicate a match.
if mi < len(match.MatchedIndexes) && ci == match.MatchedIndexes[mi] { if mi < len(match.MatchedIndexes) && ci == match.MatchedIndexes[mi] {
// Flush text buffer. // Flush text buffer.
s.WriteString(m.textStyle.Render(buf.String())) s.WriteString(lineTextStyle.Render(buf.String()))
buf.Reset() buf.Reset()
s.WriteString(m.matchStyle.Render(string(c))) s.WriteString(m.matchStyle.Render(string(c)))
@ -112,7 +118,7 @@ func (m model) View() string {
} }
} }
// Flush text buffer. // Flush text buffer.
s.WriteString(m.textStyle.Render(buf.String())) s.WriteString(lineTextStyle.Render(buf.String()))
// We have finished displaying the match with all of it's matched // We have finished displaying the match with all of it's matched
// characters highlighted and the rest filled in. // characters highlighted and the rest filled in.

View file

@ -16,6 +16,7 @@ type Options struct {
HeaderStyle style.Styles `embed:"" prefix:"header." set:"defaultForeground=240" envprefix:"GUM_FILTER_HEADER_"` HeaderStyle style.Styles `embed:"" prefix:"header." set:"defaultForeground=240" envprefix:"GUM_FILTER_HEADER_"`
Header string `help:"Header value" default:"" env:"GUM_FILTER_HEADER"` Header string `help:"Header value" default:"" env:"GUM_FILTER_HEADER"`
TextStyle style.Styles `embed:"" prefix:"text." envprefix:"GUM_FILTER_TEXT_"` TextStyle style.Styles `embed:"" prefix:"text." envprefix:"GUM_FILTER_TEXT_"`
CursorTextStyle style.Styles `embed:"" prefix:"cursor-text." envprefix:"GUM_FILTER_CURSOR_TEXT_"`
MatchStyle style.Styles `embed:"" prefix:"match." set:"defaultForeground=212" envprefix:"GUM_FILTER_MATCH_"` MatchStyle style.Styles `embed:"" prefix:"match." set:"defaultForeground=212" envprefix:"GUM_FILTER_MATCH_"`
Placeholder string `help:"Placeholder value" default:"Filter..." env:"GUM_FILTER_PLACEHOLDER"` Placeholder string `help:"Placeholder value" default:"Filter..." env:"GUM_FILTER_PLACEHOLDER"`
Prompt string `help:"Prompt to display" default:"> " env:"GUM_FILTER_PROMPT"` Prompt string `help:"Prompt to display" default:"> " env:"GUM_FILTER_PROMPT"`