From 6da7ec55250e06314203bbfc67c137481408c315 Mon Sep 17 00:00:00 2001 From: cpendery Date: Tue, 12 Sep 2023 23:38:12 -0400 Subject: [PATCH] refactor: theming & fix bad clamp on suggestion cursor positioning Signed-off-by: cpendery --- ui/suggestions/suggestions.go | 13 +++++++------ ui/theme/theme.go | 9 +++++++++ ui/ui.go | 3 ++- 3 files changed, 18 insertions(+), 7 deletions(-) create mode 100644 ui/theme/theme.go diff --git a/ui/suggestions/suggestions.go b/ui/suggestions/suggestions.go index f6b9c34..1b77076 100644 --- a/ui/suggestions/suggestions.go +++ b/ui/suggestions/suggestions.go @@ -9,6 +9,7 @@ import ( tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" "github.com/cpendery/clac/autocomplete" + "github.com/cpendery/clac/ui/theme" "github.com/cpendery/clac/ui/utils" "github.com/mattn/go-runewidth" "github.com/muesli/reflow/wordwrap" @@ -100,7 +101,7 @@ func (m Model) Update(msg tea.Msg, command string, userInputCursorLocation int) } m.userInputCursorLocation = userInputCursorLocation m.suggestions, m.argDescription, m.runesToRemove = autocomplete.LoadSuggestions(command) - if len(m.suggestions) == 0 { + if m.cursor > len(m.suggestions)-1 { m.cursor = 0 } return m @@ -110,7 +111,7 @@ func (m Model) renderSuggestion(suggestion autocomplete.Suggestion, position, cu content := suggestion.NamePrefix + " " + suggestion.Name truncatedContent := wordTrunc(content, width) if position == m.cursor%MaxSuggestions { - return lipgloss.NewStyle().Background(lipgloss.Color("#7D56F4")).Width(width).Render(truncatedContent) + return lipgloss.NewStyle().Background(theme.ActiveSuggestionBackground).Width(width).Render(truncatedContent) } return lipgloss.NewStyle().Render(truncatedContent) } @@ -142,7 +143,7 @@ func (m Model) renderSuggestions(width int) string { Bold(true). Width(width). BorderStyle(suggestionBorder). - BorderForeground(lipgloss.Color("63")) + BorderForeground(theme.BorderForeground) m.suggestions = pageSuggestions(m.suggestions, m.cursor) r := make([]string, len(m.suggestions)) @@ -155,7 +156,7 @@ func (m Model) renderSuggestions(width int) string { descriptionStyle := lipgloss.NewStyle(). Width(width). Border(ArgumentDescriptionBorder, false, true, true, true). - BorderForeground(lipgloss.Color("63")) + BorderForeground(theme.BorderForeground) return lipgloss.JoinVertical( lipgloss.Left, @@ -170,7 +171,7 @@ func (m Model) renderDescription(width int) string { Bold(true). Width(width). BorderStyle(lipgloss.NormalBorder()). - BorderForeground(lipgloss.Color("63")) + BorderForeground(theme.BorderForeground) return style.Render(wordWrap(m.suggestions[m.cursor].Description, width)) } @@ -183,7 +184,7 @@ func (m Model) renderArgumentDescription(width int) string { MarginLeft(utils.Clamp(m.userInputCursorLocation, 0, maxLeftPaddingDescription)). Width(argDescriptionWidth). BorderStyle(lipgloss.NormalBorder()). - BorderForeground(lipgloss.Color("63")) + BorderForeground(theme.BorderForeground) return style.Render(wordWrap(m.argDescription, argDescriptionWidth)) } diff --git a/ui/theme/theme.go b/ui/theme/theme.go new file mode 100644 index 0000000..560c5cb --- /dev/null +++ b/ui/theme/theme.go @@ -0,0 +1,9 @@ +package theme + +import "github.com/charmbracelet/lipgloss" + +const ( + BorderForeground = lipgloss.Color("8") + CursorForeground = lipgloss.Color("15") + ActiveSuggestionBackground = lipgloss.Color("#7D56F4") +) diff --git a/ui/ui.go b/ui/ui.go index e269566..f534fff 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -9,6 +9,7 @@ import ( tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" "github.com/cpendery/clac/ui/suggestions" + "github.com/cpendery/clac/ui/theme" "github.com/cpendery/clac/ui/utils" ) @@ -28,7 +29,7 @@ const ( func Start() model { ti := textinput.New() - ti.Cursor.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("63")) + ti.Cursor.Style = lipgloss.NewStyle().Foreground(theme.CursorForeground) ti.Focus() sug := suggestions.New()