From 1fd9c2666a335d96e6146bbcb6c72ac793a8eede Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Tue, 28 Jan 2025 22:06:47 -0500 Subject: [PATCH] chore(textarea,cursor): simplify cursor API --- cursor/cursor.go | 8 ++++---- textarea/textarea.go | 20 ++++++++------------ textinput/textinput.go | 4 ++-- 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/cursor/cursor.go b/cursor/cursor.go index 33d2946..824b450 100644 --- a/cursor/cursor.go +++ b/cursor/cursor.go @@ -56,15 +56,15 @@ type Model struct { // Style styles the cursor block. Style lipgloss.Style - // BlinkedStyle is the style used for the cursor when it is blinking + // TextStyle is the style used for the cursor when it is blinking // (hidden), i.e. displaying normal text. - BlinkedStyle lipgloss.Style + TextStyle lipgloss.Style // BlinkSpeed is the speed at which the cursor blinks. This has no effect // unless [CursorMode] is not set to [CursorBlink]. BlinkSpeed time.Duration - // Blink is the cursor blink state. + // Blink is the state of the cursor blink. When true, the cursor is hidden. Blink bool // char is the character under the cursor @@ -224,7 +224,7 @@ func (m *Model) SetChar(char string) { // View displays the cursor. func (m Model) View() string { if m.Blink { - return m.BlinkedStyle.Inline(true).Render(m.char) + return m.TextStyle.Inline(true).Render(m.char) } return m.Style.Inline(true).Reverse(true).Render(m.char) } diff --git a/textarea/textarea.go b/textarea/textarea.go index ea1a034..f57faaf 100644 --- a/textarea/textarea.go +++ b/textarea/textarea.go @@ -3,6 +3,7 @@ package textarea import ( "crypto/sha256" "fmt" + "image/color" "strconv" "strings" "time" @@ -135,12 +136,7 @@ type CursorStyle struct { // // For real cursors, the foreground color set here will be used as the // cursor color. - Style lipgloss.Style - - // BlinkedStyle is the style used for the cursor when it is blinking - // (hidden), i.e. displaying normal text. This is only used for virtual - // cursors. - BlinkedStyle lipgloss.Style + Color color.Color // Shape is the cursor shape. The following shapes are available: // @@ -395,7 +391,8 @@ func DefaultStyles(isDark bool) Styles { Text: lipgloss.NewStyle().Foreground(lightDark(lipgloss.Color("245"), lipgloss.Color("7"))), } s.Cursor = CursorStyle{ - Style: lipgloss.NewStyle().Foreground(lipgloss.Color("7")), + Color: lipgloss.Color("7"), + Shape: tea.CursorBlock, Blink: true, } return s @@ -419,8 +416,7 @@ func (m *Model) updateVirtualCursorStyle() { return } - m.virtualCursor.Style = m.Styles.Cursor.Style - m.virtualCursor.BlinkedStyle = m.Styles.Cursor.BlinkedStyle + m.virtualCursor.Style = lipgloss.NewStyle().Foreground(m.Styles.Cursor.Color) // By default, the blink speed of the cursor is set to a default // internally. @@ -1194,7 +1190,7 @@ func (m Model) View() string { if m.Value() == "" && m.row == 0 && m.col == 0 && m.Placeholder != "" { return m.placeholderView() } - m.virtualCursor.BlinkedStyle = m.activeStyle.computedCursorLine() + m.virtualCursor.TextStyle = m.activeStyle.computedCursorLine() var ( s strings.Builder @@ -1370,7 +1366,7 @@ func (m Model) placeholderView() string { // first line case i == 0: // first character of first line as cursor with character - m.virtualCursor.BlinkedStyle = m.activeStyle.computedPlaceholder() + m.virtualCursor.TextStyle = m.activeStyle.computedPlaceholder() m.virtualCursor.SetChar(string(plines[0][0])) s.WriteString(lineStyle.Render(m.virtualCursor.View())) @@ -1423,7 +1419,7 @@ func (m Model) Cursor() *tea.Cursor { c := tea.NewCursor(x, y) c.Blink = m.Styles.Cursor.Blink - c.Color = m.Styles.Cursor.Style.GetForeground() + c.Color = m.Styles.Cursor.Color c.Shape = m.Styles.Cursor.Shape return c } diff --git a/textinput/textinput.go b/textinput/textinput.go index b98d00b..92c468a 100644 --- a/textinput/textinput.go +++ b/textinput/textinput.go @@ -671,7 +671,7 @@ func (m Model) View() string { if m.canAcceptSuggestion() { suggestion := m.matchedSuggestions[m.currentSuggestionIndex] if len(value) < len(suggestion) { - m.Cursor.BlinkedStyle = m.CompletionStyle + m.Cursor.TextStyle = m.CompletionStyle m.Cursor.SetChar(m.echoTransform(string(suggestion[pos]))) v += m.Cursor.View() v += m.completionView(1) @@ -709,7 +709,7 @@ func (m Model) placeholderView() string { p := make([]rune, m.Width()+1) copy(p, []rune(m.Placeholder)) - m.Cursor.BlinkedStyle = m.PlaceholderStyle + m.Cursor.TextStyle = m.PlaceholderStyle m.Cursor.SetChar(string(p[:1])) v += m.Cursor.View()