Skip to content

Commit 617f0cc

Browse files
author
Kilian Drechsler
committed
renaming local variables, add configurability, changing test samples
- added field for costumising the unselected marker - added WrapPrefix to turn of the selected marker by wraped lines - renamed some varibales to (hopefully) better names - turned of wraping of lines when wrap is not enabled - added Focus to be able to ignore keypresses Warning: You can still change the state by surpassing the Update and use the Methodes directly! this raises the question if focus should also affect the Methodes and if false return a error.? - removed newlines from the ends of the (golden) test samples, since it would subtract a line from the View's height
1 parent 961aee3 commit 617f0cc

File tree

3 files changed

+84
-29
lines changed

3 files changed

+84
-29
lines changed

list/example/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ func main() {
6363
func initialize(lineList []string, endResult chan<- string) func() (tea.Model, tea.Cmd) {
6464
l := list.NewModel()
6565
l.AddItems(lineList)
66+
// l.WrapPrefix = false // uncomment for fancy check (selected) box :-)
67+
// l.SelectedPrefix = " [x]"
68+
// l.UnSelectedPrefix = "[ ]"
6669

6770
return func() (tea.Model, tea.Cmd) { return model{list: l, endResult: endResult}, nil }
6871
}

list/list.go

Lines changed: 76 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@ type Model struct {
2626

2727
Wrap bool
2828

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
3336

3437
Number bool
3538
NumberRelative bool
@@ -98,20 +101,34 @@ func (m *Model) Lines() []string {
98101

99102
// pad all prefixes to the same width for easy exchange
100103
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
103120

104121
// pad all seperators to the same width for easy exchange
105122
sepItem := strings.Repeat(" ", sepWidth-widthItem) + m.Seperator
106123
sepWrap := strings.Repeat(" ", sepWidth-widthWrap) + m.SeperatorWrap
107124

108125
// 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)
112129

113130
// Get the hole prefix width
114-
holePrefixWidth := numWidth + preWidth + sepWidth + sufWidth
131+
holePrefixWidth := numWidth + preWidth + sepWidth + markWidth
115132

116133
// Get actual content width
117134
contentWidth := width - holePrefixWidth
@@ -121,9 +138,13 @@ func (m *Model) Lines() []string {
121138
panic("Can't display with zero width for content")
122139
}
123140

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+
}
127148
}
128149

129150
var visLines int
@@ -137,7 +158,7 @@ out:
137158
}
138159

139160
item := m.listItems[index]
140-
if item.wrapedLenght <= 0 {
161+
if wrap && item.wrapedLenght <= 0 {
141162
panic("cant display item with no visible content")
142163
}
143164

@@ -163,15 +184,19 @@ out:
163184
}
164185

165186
// Current: handel highlighting of current item/first-line
166-
curPad := sufpad
187+
curPad := unmark
167188
if index == m.curIndex {
168189
style = m.CurrentStyle
169-
curPad = suffix
190+
curPad = mark
170191
}
171192

172193
// 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+
}
175200

176201
// join pad and first line content
177202
// NOTE linebreak is not added here because it would mess with the highlighting
@@ -187,7 +212,7 @@ out:
187212
}
188213

189214
// Dont write wraped lines if not set
190-
if !m.Wrap || item.wrapedLenght <= 1 {
215+
if !wrap || item.wrapedLenght <= 1 {
191216
continue
192217
}
193218

@@ -226,6 +251,9 @@ func lineNumber(relativ bool, curser, current int) int {
226251

227252
// Update changes the Model of the List according to the messages recieved
228253
func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
254+
if !m.focus {
255+
return m, nil
256+
}
229257
var cmd tea.Cmd
230258

231259
switch msg := msg.(type) {
@@ -366,17 +394,28 @@ func (m *Model) Move(amount int) error {
366394
func NewModel() Model {
367395
p := termenv.ColorProfile()
368396
selStyle := termenv.Style{}.Background(p.Color("#ff0000"))
397+
// just reverse colors to keep there information
369398
curStyle := termenv.Style{}.Reverse()
370399
return Model{
400+
// Accept keypresses
401+
focus: true,
402+
403+
// Try to keep $CurserOffset lines between Cursor and screen Border
371404
CurserOffset: 5,
372405

406+
// Wrap lines to have no loss of information
373407
Wrap: true,
374408

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
377414
CurrentMarker: ">",
378415
SelectedPrefix: "*",
379-
Number: true,
416+
417+
// enable Linenumber
418+
Number: true,
380419

381420
less: func(k, l string) bool {
382421
return k < l
@@ -544,3 +583,18 @@ func (m *Model) CheckWithinBorder(index int) bool {
544583
//func (m *Model) AddDataItem(content string, data interface{}) {
545584
// m.listItems = append(m.listItems, item{content: content, userValue: data})
546585
//}
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+
}

list/list_test.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func genTestModels() []test {
104104
[]string{
105105
"",
106106
},
107-
"\x1b[7m0 ╭>\x1b[0m\n",
107+
"\x1b[7m0 ╭>\x1b[0m",
108108
},
109109
// if exceding the boards and softwrap (at word bounderys are possible
110110
// wrap there. Dont increment the item number because its still the same item.
@@ -114,7 +114,7 @@ func genTestModels() []test {
114114
[]string{
115115
"robert frost",
116116
},
117-
"\x1b[7m0 ╭>robert\x1b[0m\n\x1b[7m │ frost\x1b[0m\n",
117+
"\x1b[7m0 ╭>robert\x1b[0m\n\x1b[7m │ frost\x1b[0m",
118118
},
119119
{
120120
10,
@@ -129,8 +129,7 @@ func genTestModels() []test {
129129
6 ╭
130130
7 ╭
131131
8 ╭
132-
9 ╭
133-
`,
132+
9 ╭ `,
134133
},
135134
}
136135
}
@@ -177,7 +176,7 @@ func genDynamicModels() []testModel {
177176
moveDown.Down()
178177
return []testModel{
179178
{model: moveBottom,
180-
shouldBe: "0 ╭ \n1 ╭ \n2 ╭ \n\x1b[7m3 ╭>\x1b[0m\n",
179+
shouldBe: "0 ╭ \n1 ╭ \n2 ╭ \n\x1b[7m3 ╭>\x1b[0m",
181180
afterMethode: "Bottom",
182181
},
183182
{model: moveDown,
@@ -230,8 +229,7 @@ func genDynamicModels() []testModel {
230229
`47 ╭
231230
48 ╭
232231
49 ╭
233-
50 ╭
234-
`,
232+
50 ╭ `,
235233
afterMethode: "Down",
236234
},
237235
}

0 commit comments

Comments
 (0)