@@ -117,14 +117,22 @@ type LineInfo struct {
117
117
CharOffset int
118
118
}
119
119
120
- // Style that will be applied to the text area.
120
+ // Styles are the styles for the textarea, separated into focused and blurred
121
+ // states. The appropriate styles will be chosen based on the focus state of
122
+ // the textarea.
123
+ type Styles struct {
124
+ Focused StyleState
125
+ Blurred StyleState
126
+ }
127
+
128
+ // StyleState that will be applied to the text area.
121
129
//
122
- // Style can be applied to focused and unfocused states to change the styles
130
+ // StyleState can be applied to focused and unfocused states to change the styles
123
131
// depending on the focus state.
124
132
//
125
133
// For an introduction to styling with Lip Gloss see:
126
134
// https://github.com/charmbracelet/lipgloss
127
- type Style struct {
135
+ type StyleState struct {
128
136
Base lipgloss.Style
129
137
CursorLine lipgloss.Style
130
138
CursorLineNumber lipgloss.Style
@@ -135,34 +143,34 @@ type Style struct {
135
143
Text lipgloss.Style
136
144
}
137
145
138
- func (s Style ) computedCursorLine () lipgloss.Style {
146
+ func (s StyleState ) computedCursorLine () lipgloss.Style {
139
147
return s .CursorLine .Inherit (s .Base ).Inline (true )
140
148
}
141
149
142
- func (s Style ) computedCursorLineNumber () lipgloss.Style {
150
+ func (s StyleState ) computedCursorLineNumber () lipgloss.Style {
143
151
return s .CursorLineNumber .
144
152
Inherit (s .CursorLine ).
145
153
Inherit (s .Base ).
146
154
Inline (true )
147
155
}
148
156
149
- func (s Style ) computedEndOfBuffer () lipgloss.Style {
157
+ func (s StyleState ) computedEndOfBuffer () lipgloss.Style {
150
158
return s .EndOfBuffer .Inherit (s .Base ).Inline (true )
151
159
}
152
160
153
- func (s Style ) computedLineNumber () lipgloss.Style {
161
+ func (s StyleState ) computedLineNumber () lipgloss.Style {
154
162
return s .LineNumber .Inherit (s .Base ).Inline (true )
155
163
}
156
164
157
- func (s Style ) computedPlaceholder () lipgloss.Style {
165
+ func (s StyleState ) computedPlaceholder () lipgloss.Style {
158
166
return s .Placeholder .Inherit (s .Base ).Inline (true )
159
167
}
160
168
161
- func (s Style ) computedPrompt () lipgloss.Style {
169
+ func (s StyleState ) computedPrompt () lipgloss.Style {
162
170
return s .Prompt .Inherit (s .Base ).Inline (true )
163
171
}
164
172
165
- func (s Style ) computedText () lipgloss.Style {
173
+ func (s StyleState ) computedText () lipgloss.Style {
166
174
return s .Text .Inherit (s .Base ).Inline (true )
167
175
}
168
176
@@ -210,13 +218,13 @@ type Model struct {
210
218
211
219
// Styling. FocusedStyle and BlurredStyle are used to style the textarea in
212
220
// focused and blurred states.
213
- FocusedStyle Style
214
- BlurredStyle Style
215
- // style is the current styling to use.
221
+ Styles Styles
222
+
223
+ // activeStyle is the current styling to use.
216
224
// It is used to abstract the differences in focus state when styling the
217
- // model, since we can simply assign the set of styles to this variable
225
+ // model, since we can simply assign the set of activeStyle to this variable
218
226
// when switching focus states.
219
- style * Style
227
+ activeStyle * StyleState
220
228
221
229
// Cursor is the text area cursor.
222
230
Cursor cursor.Model
@@ -280,16 +288,15 @@ func New() Model {
280
288
vp .KeyMap = viewport.KeyMap {}
281
289
cur := cursor .New ()
282
290
283
- focusedStyle , blurredStyle := DefaultStyles (true )
291
+ styles := DefaultStyles (true )
284
292
285
293
m := Model {
286
294
CharLimit : defaultCharLimit ,
287
295
MaxHeight : defaultMaxHeight ,
288
296
MaxWidth : defaultMaxWidth ,
289
297
Prompt : lipgloss .ThickBorder ().Left + " " ,
290
- style : & blurredStyle ,
291
- FocusedStyle : focusedStyle ,
292
- BlurredStyle : blurredStyle ,
298
+ Styles : styles ,
299
+ activeStyle : & styles .Blurred ,
293
300
cache : memoization.NewMemoCache [line , [][]rune ](defaultMaxHeight ),
294
301
EndOfBufferCharacter : ' ' ,
295
302
ShowLineNumbers : true ,
@@ -312,10 +319,11 @@ func New() Model {
312
319
313
320
// DefaultStyles returns the default styles for focused and blurred states for
314
321
// the textarea.
315
- func DefaultStyles (isDark bool ) ( Style , Style ) {
322
+ func DefaultStyles (isDark bool ) Styles {
316
323
lightDark := lipgloss .LightDark (isDark )
317
324
318
- focused := Style {
325
+ var s Styles
326
+ s .Focused = StyleState {
319
327
Base : lipgloss .NewStyle (),
320
328
CursorLine : lipgloss .NewStyle ().Background (lightDark ("255" , "0" )),
321
329
CursorLineNumber : lipgloss .NewStyle ().Foreground (lightDark ("240" , "240" )),
@@ -325,7 +333,7 @@ func DefaultStyles(isDark bool) (Style, Style) {
325
333
Prompt : lipgloss .NewStyle ().Foreground (lipgloss .Color ("7" )),
326
334
Text : lipgloss .NewStyle (),
327
335
}
328
- blurred := Style {
336
+ s . Blurred = StyleState {
329
337
Base : lipgloss .NewStyle (),
330
338
CursorLine : lipgloss .NewStyle ().Foreground (lightDark ("245" , "7" )),
331
339
CursorLineNumber : lipgloss .NewStyle ().Foreground (lightDark ("249" , "7" )),
@@ -335,8 +343,7 @@ func DefaultStyles(isDark bool) (Style, Style) {
335
343
Prompt : lipgloss .NewStyle ().Foreground (lipgloss .Color ("7" )),
336
344
Text : lipgloss .NewStyle ().Foreground (lightDark ("245" , "7" )),
337
345
}
338
-
339
- return focused , blurred
346
+ return s
340
347
}
341
348
342
349
// SetValue sets the value of the text input.
@@ -578,15 +585,15 @@ func (m Model) Focused() bool {
578
585
// receive keyboard input and the cursor will be hidden.
579
586
func (m * Model ) Focus () tea.Cmd {
580
587
m .focus = true
581
- m .style = & m .FocusedStyle
588
+ m .activeStyle = & m .Styles . Focused
582
589
return m .Cursor .Focus ()
583
590
}
584
591
585
592
// Blur removes the focus state on the model. When the model is blurred it can
586
593
// not receive keyboard input and the cursor will be hidden.
587
594
func (m * Model ) Blur () {
588
595
m .focus = false
589
- m .style = & m .BlurredStyle
596
+ m .activeStyle = & m .Styles . Blurred
590
597
m .Cursor .Blur ()
591
598
}
592
599
@@ -899,7 +906,7 @@ func (m *Model) SetWidth(w int) {
899
906
}
900
907
901
908
// Add base style borders and padding to reserved outer width.
902
- reservedOuter := m .style .Base .GetHorizontalFrameSize ()
909
+ reservedOuter := m .activeStyle .Base .GetHorizontalFrameSize ()
903
910
904
911
// Add prompt width to reserved inner width.
905
912
reservedInner := m .promptWidth
@@ -1098,7 +1105,7 @@ func (m Model) View() string {
1098
1105
if m .Value () == "" && m .row == 0 && m .col == 0 && m .Placeholder != "" {
1099
1106
return m .placeholderView ()
1100
1107
}
1101
- m .Cursor .TextStyle = m .style .computedCursorLine ()
1108
+ m .Cursor .TextStyle = m .activeStyle .computedCursorLine ()
1102
1109
1103
1110
var (
1104
1111
s strings.Builder
@@ -1113,33 +1120,33 @@ func (m Model) View() string {
1113
1120
wrappedLines := m .memoizedWrap (line , m .width )
1114
1121
1115
1122
if m .row == l {
1116
- style = m .style .computedCursorLine ()
1123
+ style = m .activeStyle .computedCursorLine ()
1117
1124
} else {
1118
- style = m .style .computedText ()
1125
+ style = m .activeStyle .computedText ()
1119
1126
}
1120
1127
1121
1128
for wl , wrappedLine := range wrappedLines {
1122
1129
prompt := m .getPromptString (displayLine )
1123
- prompt = m .style .computedPrompt ().Render (prompt )
1130
+ prompt = m .activeStyle .computedPrompt ().Render (prompt )
1124
1131
s .WriteString (style .Render (prompt ))
1125
1132
displayLine ++
1126
1133
1127
1134
var ln string
1128
1135
if m .ShowLineNumbers { //nolint:nestif
1129
1136
if wl == 0 {
1130
1137
if m .row == l {
1131
- ln = style .Render (m .style .computedCursorLineNumber ().Render (m .formatLineNumber (l + 1 )))
1138
+ ln = style .Render (m .activeStyle .computedCursorLineNumber ().Render (m .formatLineNumber (l + 1 )))
1132
1139
s .WriteString (ln )
1133
1140
} else {
1134
- ln = style .Render (m .style .computedLineNumber ().Render (m .formatLineNumber (l + 1 )))
1141
+ ln = style .Render (m .activeStyle .computedLineNumber ().Render (m .formatLineNumber (l + 1 )))
1135
1142
s .WriteString (ln )
1136
1143
}
1137
1144
} else {
1138
1145
if m .row == l {
1139
- ln = style .Render (m .style .computedCursorLineNumber ().Render (m .formatLineNumber (" " )))
1146
+ ln = style .Render (m .activeStyle .computedCursorLineNumber ().Render (m .formatLineNumber (" " )))
1140
1147
s .WriteString (ln )
1141
1148
} else {
1142
- ln = style .Render (m .style .computedLineNumber ().Render (m .formatLineNumber (" " )))
1149
+ ln = style .Render (m .activeStyle .computedLineNumber ().Render (m .formatLineNumber (" " )))
1143
1150
s .WriteString (ln )
1144
1151
}
1145
1152
}
@@ -1187,20 +1194,20 @@ func (m Model) View() string {
1187
1194
// To do this we can simply pad out a few extra new lines in the view.
1188
1195
for i := 0 ; i < m .height ; i ++ {
1189
1196
prompt := m .getPromptString (displayLine )
1190
- prompt = m .style .computedPrompt ().Render (prompt )
1197
+ prompt = m .activeStyle .computedPrompt ().Render (prompt )
1191
1198
s .WriteString (prompt )
1192
1199
displayLine ++
1193
1200
1194
1201
// Write end of buffer content
1195
1202
leftGutter := string (m .EndOfBufferCharacter )
1196
1203
rightGapWidth := m .Width () - lipgloss .Width (leftGutter ) + widestLineNumber
1197
1204
rightGap := strings .Repeat (" " , max (0 , rightGapWidth ))
1198
- s .WriteString (m .style .computedEndOfBuffer ().Render (leftGutter + rightGap ))
1205
+ s .WriteString (m .activeStyle .computedEndOfBuffer ().Render (leftGutter + rightGap ))
1199
1206
s .WriteRune ('\n' )
1200
1207
}
1201
1208
1202
1209
m .viewport .SetContent (s .String ())
1203
- return m .style .Base .Render (m .viewport .View ())
1210
+ return m .activeStyle .Base .Render (m .viewport .View ())
1204
1211
}
1205
1212
1206
1213
// formatLineNumber formats the line number for display dynamically based on
@@ -1230,7 +1237,7 @@ func (m Model) placeholderView() string {
1230
1237
var (
1231
1238
s strings.Builder
1232
1239
p = m .Placeholder
1233
- style = m .style .computedPlaceholder ()
1240
+ style = m .activeStyle .computedPlaceholder ()
1234
1241
)
1235
1242
1236
1243
// word wrap lines
@@ -1241,16 +1248,16 @@ func (m Model) placeholderView() string {
1241
1248
plines := strings .Split (strings .TrimSpace (pwrap ), "\n " )
1242
1249
1243
1250
for i := 0 ; i < m .height ; i ++ {
1244
- lineStyle := m .style .computedPlaceholder ()
1245
- lineNumberStyle := m .style .computedLineNumber ()
1251
+ lineStyle := m .activeStyle .computedPlaceholder ()
1252
+ lineNumberStyle := m .activeStyle .computedLineNumber ()
1246
1253
if len (plines ) > i {
1247
- lineStyle = m .style .computedCursorLine ()
1248
- lineNumberStyle = m .style .computedCursorLineNumber ()
1254
+ lineStyle = m .activeStyle .computedCursorLine ()
1255
+ lineNumberStyle = m .activeStyle .computedCursorLineNumber ()
1249
1256
}
1250
1257
1251
1258
// render prompt
1252
1259
prompt := m .getPromptString (i )
1253
- prompt = m .style .computedPrompt ().Render (prompt )
1260
+ prompt = m .activeStyle .computedPrompt ().Render (prompt )
1254
1261
s .WriteString (lineStyle .Render (prompt ))
1255
1262
1256
1263
// when show line numbers enabled:
@@ -1274,7 +1281,7 @@ func (m Model) placeholderView() string {
1274
1281
// first line
1275
1282
case i == 0 :
1276
1283
// first character of first line as cursor with character
1277
- m .Cursor .TextStyle = m .style .computedPlaceholder ()
1284
+ m .Cursor .TextStyle = m .activeStyle .computedPlaceholder ()
1278
1285
m .Cursor .SetChar (string (plines [0 ][0 ]))
1279
1286
s .WriteString (lineStyle .Render (m .Cursor .View ()))
1280
1287
@@ -1288,7 +1295,7 @@ func (m Model) placeholderView() string {
1288
1295
}
1289
1296
default :
1290
1297
// end of line buffer character
1291
- eob := m .style .computedEndOfBuffer ().Render (string (m .EndOfBufferCharacter ))
1298
+ eob := m .activeStyle .computedEndOfBuffer ().Render (string (m .EndOfBufferCharacter ))
1292
1299
s .WriteString (eob )
1293
1300
}
1294
1301
@@ -1297,7 +1304,7 @@ func (m Model) placeholderView() string {
1297
1304
}
1298
1305
1299
1306
m .viewport .SetContent (s .String ())
1300
- return m .style .Base .Render (m .viewport .View ())
1307
+ return m .activeStyle .Base .Render (m .viewport .View ())
1301
1308
}
1302
1309
1303
1310
// Blink returns the blink command for the cursor.
0 commit comments