From 99f1348a451c8d567876e0a26a78cf74528435ef Mon Sep 17 00:00:00 2001 From: ROMAIN GUISSET Date: Wed, 24 May 2023 12:11:20 +0000 Subject: [PATCH] feat(filter): add cursor text line styling --- filter/command.go | 1 + filter/filter.go | 10 ++++++++-- filter/options.go | 1 + 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/filter/command.go b/filter/command.go index af62920..492c904 100644 --- a/filter/command.go +++ b/filter/command.go @@ -84,6 +84,7 @@ func (o Options) Run() error { matchStyle: o.MatchStyle.ToLipgloss(), headerStyle: o.HeaderStyle.ToLipgloss(), textStyle: o.TextStyle.ToLipgloss(), + cursorTextStyle: o.CursorTextStyle.ToLipgloss(), height: o.Height, selected: make(map[string]struct{}), limit: o.Limit, diff --git a/filter/filter.go b/filter/filter.go index 69d62f5..22f4fdf 100644 --- a/filter/filter.go +++ b/filter/filter.go @@ -39,6 +39,7 @@ type model struct { headerStyle lipgloss.Style matchStyle lipgloss.Style textStyle lipgloss.Style + cursorTextStyle lipgloss.Style indicatorStyle lipgloss.Style selectedPrefixStyle lipgloss.Style unselectedPrefixStyle lipgloss.Style @@ -54,6 +55,7 @@ func (m model) View() string { } var s strings.Builder + var lineTextStyle lipgloss.Style // 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 @@ -74,10 +76,14 @@ func (m model) View() string { // If this is the current selected index, we add a small indicator to // 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 { s.WriteString(m.indicatorStyle.Render(m.indicator)) + lineTextStyle = m.cursorTextStyle } else { s.WriteString(strings.Repeat(" ", lipgloss.Width(m.indicator))) + lineTextStyle = m.textStyle } // 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. if mi < len(match.MatchedIndexes) && ci == match.MatchedIndexes[mi] { // Flush text buffer. - s.WriteString(m.textStyle.Render(buf.String())) + s.WriteString(lineTextStyle.Render(buf.String())) buf.Reset() s.WriteString(m.matchStyle.Render(string(c))) @@ -112,7 +118,7 @@ func (m model) View() string { } } // 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 // characters highlighted and the rest filled in. diff --git a/filter/options.go b/filter/options.go index 1392d91..b578d29 100644 --- a/filter/options.go +++ b/filter/options.go @@ -16,6 +16,7 @@ type Options struct { 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_"` + 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_"` Placeholder string `help:"Placeholder value" default:"Filter..." env:"GUM_FILTER_PLACEHOLDER"` Prompt string `help:"Prompt to display" default:"> " env:"GUM_FILTER_PROMPT"`