Skip to content

Commit 961aee3

Browse files
author
Kilian Drechsler
committed
changed Style handling and implemented jump
- Changed Style fields in list Model struct - added jump to example movement
1 parent b1e9a91 commit 961aee3

File tree

2 files changed

+40
-14
lines changed

2 files changed

+40
-14
lines changed

list/example/main.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/charmbracelet/bubbles/list"
66
tea "github.com/charmbracelet/bubbletea"
77
"os"
8+
"strconv"
89
"strings"
910
)
1011

@@ -13,6 +14,7 @@ type model struct {
1314
list list.Model
1415
finished bool
1516
endResult chan<- string
17+
jump string
1618
}
1719

1820
func main() {
@@ -28,7 +30,8 @@ func main() {
2830
"When you print the items there will be a loss of information,",
2931
"since one can not say what was a line break within an item or what is a new item",
3032
"Use '+' or '-' to move the item under the curser up and down.",
31-
"The 'v' inverts the selected state of each item.",
33+
"The key 'v' inverts the selected state of each item.",
34+
"To toggle betwen only absolute itemnumbers and also relativ numbers, the 'r' key is your friend.",
3235
}
3336
endResult := make(chan string, 1)
3437

@@ -87,10 +90,32 @@ func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {
8790
m.endResult <- ""
8891
return m, tea.Quit
8992
}
90-
switch msg.String() {
93+
switch keyString := msg.String(); keyString {
9194
case "q":
9295
m.endResult <- ""
9396
return m, tea.Quit
97+
case "1", "2", "3", "4", "5", "6", "7", "8", "9", "0":
98+
m.jump += keyString
99+
return m, nil
100+
case "down", "j":
101+
j := 1
102+
if m.jump != "" {
103+
j, _ = strconv.Atoi(m.jump)
104+
m.jump = ""
105+
}
106+
m.list.Move(j)
107+
return m, nil
108+
case "up", "k":
109+
j := 1
110+
if m.jump != "" {
111+
j, _ = strconv.Atoi(m.jump)
112+
m.jump = ""
113+
}
114+
m.list.Move(-j)
115+
return m, nil
116+
case "r":
117+
m.list.NumberRelative = !m.list.NumberRelative
118+
return m, nil
94119
}
95120

96121
// Enter prints the selected lines to StdOut

list/list.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,9 @@ type Model struct {
3434
Number bool
3535
NumberRelative bool
3636

37-
LineForeGroundStyle termenv.Style
38-
LineBackGroundStyle termenv.Style
39-
SelectedForeGroundStyle termenv.Style
40-
SelectedBackGroundStyle termenv.Style
37+
LineStyle termenv.Style
38+
SelectedStyle termenv.Style
39+
CurrentStyle termenv.Style
4140
}
4241

4342
// Item are Items used in the list Model
@@ -155,18 +154,18 @@ out:
155154
}
156155

157156
// Selecting: handel highlighting and prefixing of selected lines
158-
selString := prepad // assume not selected
159-
style := termenv.String() // create empty style
157+
selString := prepad
158+
style := m.LineStyle
160159

161160
if item.selected {
162-
style = m.SelectedBackGroundStyle // fill style
163-
selString = prefix // change if selected
161+
style = m.SelectedStyle
162+
selString = prefix
164163
}
165164

166165
// Current: handel highlighting of current item/first-line
167166
curPad := sufpad
168167
if index == m.curIndex {
169-
style = style.Reverse()
168+
style = m.CurrentStyle
170169
curPad = suffix
171170
}
172171

@@ -352,7 +351,7 @@ func (m *Model) Move(amount int) error {
352351
}
353352
// visible Up movement
354353
if direction < 0 && target < upperBorder {
355-
visOff = target-curOff
354+
visOff = target - curOff
356355
}
357356
// dont go infront of list begin
358357
if visOff < 0 {
@@ -366,7 +365,8 @@ func (m *Model) Move(amount int) error {
366365
// NewModel returns a Model with some save/sane defaults
367366
func NewModel() Model {
368367
p := termenv.ColorProfile()
369-
style := termenv.Style{}.Background(p.Color("#ff0000"))
368+
selStyle := termenv.Style{}.Background(p.Color("#ff0000"))
369+
curStyle := termenv.Style{}.Reverse()
370370
return Model{
371371
CurserOffset: 5,
372372

@@ -382,7 +382,8 @@ func NewModel() Model {
382382
return k < l
383383
},
384384

385-
SelectedBackGroundStyle: style,
385+
SelectedStyle: selStyle,
386+
CurrentStyle: curStyle,
386387
}
387388
}
388389

0 commit comments

Comments
 (0)