chore(textarea): make placeholder rendering more readable

This commit is contained in:
Christian Rocha
2025-01-31 16:21:17 -05:00
parent a6b8a4559c
commit 78262b85c3
+11 -9
View File
@@ -1341,18 +1341,16 @@ func (m Model) lineNumberView(n int, isCursorLine bool) (str string) {
return textStyle.Render(lineNumberStyle.Render(str)) return textStyle.Render(lineNumberStyle.Render(str))
} }
// placeholderView returns the prompt and placeholder view, if any. // placeholderView returns the prompt and placeholder, if any.
func (m Model) placeholderView() string { func (m Model) placeholderView() string {
var ( var (
s strings.Builder s strings.Builder
p = m.Placeholder p = m.Placeholder
styles = m.activeStyle() styles = m.activeStyle()
// placeholderStyle = m.activeStyle().computedPlaceholder()
) )
// word wrap lines // word wrap lines
pwordwrap := ansi.Wordwrap(p, m.width, "") pwordwrap := ansi.Wordwrap(p, m.width, "")
// wrap lines (handles lines that could not be word wrapped) // hard wrap lines (handles lines that could not be word wrapped)
pwrap := ansi.Hardwrap(pwordwrap, m.width, true) pwrap := ansi.Hardwrap(pwordwrap, m.width, true)
// split string by new lines // split string by new lines
plines := strings.Split(strings.TrimSpace(pwrap), "\n") plines := strings.Split(strings.TrimSpace(pwrap), "\n")
@@ -1360,10 +1358,9 @@ func (m Model) placeholderView() string {
for i := 0; i < m.height; i++ { for i := 0; i < m.height; i++ {
isLineNumber := len(plines) > i isLineNumber := len(plines) > i
// XXX: This will go. lineStyle := styles.computedPlaceholder()
lineStyle := m.activeStyle().computedPlaceholder()
if len(plines) > i { if len(plines) > i {
lineStyle = m.activeStyle().computedCursorLine() lineStyle = styles.computedCursorLine()
} }
// render prompt // render prompt
@@ -1397,12 +1394,17 @@ func (m Model) placeholderView() string {
s.WriteString(lineStyle.Render(m.virtualCursor.View())) s.WriteString(lineStyle.Render(m.virtualCursor.View()))
// the rest of the first line // the rest of the first line
s.WriteString(lineStyle.Render(plines[0][1:] + strings.Repeat(" ", max(0, m.width-uniseg.StringWidth(plines[0]))))) placeholderTail := plines[0][1:]
gap := strings.Repeat(" ", max(0, m.width-uniseg.StringWidth(plines[0])))
renderedPlaceholder := styles.computedPlaceholder().Render(placeholderTail + gap)
s.WriteString(lineStyle.Render(renderedPlaceholder))
// remaining lines // remaining lines
case len(plines) > i: case len(plines) > i:
// current line placeholder text // current line placeholder text
if len(plines) > i { if len(plines) > i {
s.WriteString(lineStyle.Render(plines[i] + strings.Repeat(" ", max(0, m.width-uniseg.StringWidth(plines[i]))))) placeholderLine := plines[i]
gap := strings.Repeat(" ", max(0, m.width-uniseg.StringWidth(plines[i])))
s.WriteString(lineStyle.Render(placeholderLine + gap))
} }
default: default:
// end of line buffer character // end of line buffer character