@@ -26,10 +26,13 @@ type Model struct {
26
26
27
27
Wrap bool
28
28
29
- SelectedPrefix string
30
- Seperator string
31
- SeperatorWrap string
32
- CurrentMarker string
29
+ SelectedPrefix string
30
+ UnSelectedPrefix string
31
+ Seperator string
32
+ SeperatorWrap string
33
+ CurrentMarker string
34
+
35
+ WrapPrefix bool
33
36
34
37
Number bool
35
38
NumberRelative bool
@@ -98,20 +101,34 @@ func (m *Model) Lines() []string {
98
101
99
102
// pad all prefixes to the same width for easy exchange
100
103
prefix := m .SelectedPrefix
101
- preWidth := ansi .PrintableRuneWidth (prefix )
102
- prepad := strings .Repeat (" " , preWidth )
104
+ prepad := m .UnSelectedPrefix
105
+ preWid := ansi .PrintableRuneWidth (prefix )
106
+ tmpWid := ansi .PrintableRuneWidth (prepad )
107
+
108
+ preWidth := preWid
109
+ if tmpWid > preWidth {
110
+ preWidth = tmpWid
111
+ }
112
+ prefix = strings .Repeat (" " , preWidth - preWid ) + prefix
113
+
114
+ wrapPrePad := prepad
115
+ if ! m .WrapPrefix {
116
+ wrapPrePad = strings .Repeat (" " , preWidth )
117
+ }
118
+
119
+ prepad = strings .Repeat (" " , preWidth - tmpWid ) + prepad
103
120
104
121
// pad all seperators to the same width for easy exchange
105
122
sepItem := strings .Repeat (" " , sepWidth - widthItem ) + m .Seperator
106
123
sepWrap := strings .Repeat (" " , sepWidth - widthWrap ) + m .SeperatorWrap
107
124
108
125
// pad right of prefix, with lenght of current pointer
109
- suffix := m .CurrentMarker
110
- sufWidth := ansi .PrintableRuneWidth (suffix )
111
- sufpad := strings .Repeat (" " , sufWidth )
126
+ mark := m .CurrentMarker
127
+ markWidth := ansi .PrintableRuneWidth (mark )
128
+ unmark := strings .Repeat (" " , markWidth )
112
129
113
130
// Get the hole prefix width
114
- holePrefixWidth := numWidth + preWidth + sepWidth + sufWidth
131
+ holePrefixWidth := numWidth + preWidth + sepWidth + markWidth
115
132
116
133
// Get actual content width
117
134
contentWidth := width - holePrefixWidth
@@ -121,9 +138,13 @@ func (m *Model) Lines() []string {
121
138
panic ("Can't display with zero width for content" )
122
139
}
123
140
124
- // renew wrap of all items TODO check if to slow
125
- for i := range m .listItems {
126
- m .listItems [i ] = m .listItems [i ].genVisLines (contentWidth )
141
+ // If set
142
+ wrap := m .Wrap
143
+ if wrap {
144
+ // renew wrap of all items
145
+ for i := range m .listItems {
146
+ m .listItems [i ] = m .listItems [i ].genVisLines (contentWidth )
147
+ }
127
148
}
128
149
129
150
var visLines int
137
158
}
138
159
139
160
item := m .listItems [index ]
140
- if item .wrapedLenght <= 0 {
161
+ if wrap && item .wrapedLenght <= 0 {
141
162
panic ("cant display item with no visible content" )
142
163
}
143
164
@@ -163,15 +184,19 @@ out:
163
184
}
164
185
165
186
// Current: handel highlighting of current item/first-line
166
- curPad := sufpad
187
+ curPad := unmark
167
188
if index == m .curIndex {
168
189
style = m .CurrentStyle
169
- curPad = suffix
190
+ curPad = mark
170
191
}
171
192
172
193
// join all prefixes
173
- linePrefix := strings .Join ([]string {firstPad , selString , sepItem , curPad }, "" )
174
- wrapPrefix := strings .Join ([]string {wrapPad , selString , sepWrap , sufpad }, "" ) // dont prefix wrap lines with CurrentMarker (suffix)
194
+ var wrapPrefix , linePrefix string
195
+
196
+ linePrefix = strings .Join ([]string {firstPad , selString , sepItem , curPad }, "" )
197
+ if wrap {
198
+ wrapPrefix = strings .Join ([]string {wrapPad , wrapPrePad , sepWrap , unmark }, "" ) // dont prefix wrap lines with CurrentMarker (suffix)
199
+ }
175
200
176
201
// join pad and first line content
177
202
// NOTE linebreak is not added here because it would mess with the highlighting
187
212
}
188
213
189
214
// Dont write wraped lines if not set
190
- if ! m . Wrap || item .wrapedLenght <= 1 {
215
+ if ! wrap || item .wrapedLenght <= 1 {
191
216
continue
192
217
}
193
218
@@ -226,6 +251,9 @@ func lineNumber(relativ bool, curser, current int) int {
226
251
227
252
// Update changes the Model of the List according to the messages recieved
228
253
func Update (msg tea.Msg , m Model ) (Model , tea.Cmd ) {
254
+ if ! m .focus {
255
+ return m , nil
256
+ }
229
257
var cmd tea.Cmd
230
258
231
259
switch msg := msg .(type ) {
@@ -366,17 +394,28 @@ func (m *Model) Move(amount int) error {
366
394
func NewModel () Model {
367
395
p := termenv .ColorProfile ()
368
396
selStyle := termenv.Style {}.Background (p .Color ("#ff0000" ))
397
+ // just reverse colors to keep there information
369
398
curStyle := termenv.Style {}.Reverse ()
370
399
return Model {
400
+ // Accept keypresses
401
+ focus : true ,
402
+
403
+ // Try to keep $CurserOffset lines between Cursor and screen Border
371
404
CurserOffset : 5 ,
372
405
406
+ // Wrap lines to have no loss of information
373
407
Wrap : true ,
374
408
375
- Seperator : "╭" ,
376
- SeperatorWrap : "│" ,
409
+ // Make clear where a item begins and where it ends
410
+ Seperator : "╭" ,
411
+ SeperatorWrap : "│" ,
412
+
413
+ // Mark it so that even without color support all is explicit
377
414
CurrentMarker : ">" ,
378
415
SelectedPrefix : "*" ,
379
- Number : true ,
416
+
417
+ // enable Linenumber
418
+ Number : true ,
380
419
381
420
less : func (k , l string ) bool {
382
421
return k < l
@@ -544,3 +583,18 @@ func (m *Model) CheckWithinBorder(index int) bool {
544
583
//func (m *Model) AddDataItem(content string, data interface{}) {
545
584
// m.listItems = append(m.listItems, item{content: content, userValue: data})
546
585
//}
586
+
587
+ // Focus sets the list Model focus so it accepts keyinput and responds to them
588
+ func (m * Model ) Focus () {
589
+ m .focus = true
590
+ }
591
+
592
+ // UnFocus removes the focus so that the list Model does NOT responed to key presses
593
+ func (m * Model ) UnFocus () {
594
+ m .focus = false
595
+ }
596
+
597
+ // Focused returns if the list Model is focused and acccepts keypresses
598
+ func (m * Model ) Focused () bool {
599
+ return m .focus
600
+ }
0 commit comments