Skip to content

Commit

Permalink
chore(textarea,cursor): simplify cursor API
Browse files Browse the repository at this point in the history
  • Loading branch information
meowgorithm committed Jan 29, 2025
1 parent da97b2b commit 9dd3f8b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
8 changes: 4 additions & 4 deletions cursor/cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
20 changes: 8 additions & 12 deletions textarea/textarea.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package textarea
import (
"crypto/sha256"
"fmt"
"image/color"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -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:
//
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()))

Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions textinput/textinput.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit 9dd3f8b

Please sign in to comment.