1
1
package main
2
2
3
3
import (
4
+ "bytes"
4
5
"fmt"
5
6
"github.com/charmbracelet/bubbles/list"
6
7
tea "github.com/charmbracelet/bubbletea"
7
8
"os"
8
9
"strconv"
9
- "strings"
10
10
)
11
11
12
12
type model struct {
@@ -17,11 +17,17 @@ type model struct {
17
17
jump string
18
18
}
19
19
20
+ type stringItem string
21
+
22
+ func (s stringItem ) String () string {
23
+ return string (s )
24
+ }
25
+
20
26
func main () {
21
- items := []string {
27
+ itemList := []string {
22
28
"Welcome to the bubbles-list example!" ,
23
29
"Use 'q' or 'ctrl-c' to quit!" ,
24
- "You can move the highlighted index up and down with the (arrow) keys 'k' and 'j'." ,
30
+ "You can move the highlighted index up and down with the (arrow keys 'k' and 'j'." ,
25
31
"Move to the beginning with 'g' and to the end with 'G'." ,
26
32
"Sort the entrys with 's', but be carefull you can't unsort it again." ,
27
33
"The list can handel linebreaks,\n and has wordwrap enabled if the line gets to long." ,
@@ -33,9 +39,11 @@ func main() {
33
39
"The key 'v' inverts the selected state of each item." ,
34
40
"To toggle betwen only absolute itemnumbers and also relativ numbers, the 'r' key is your friend." ,
35
41
}
42
+ stringerList := list .MakeStringerList (itemList )
43
+
36
44
endResult := make (chan string , 1 )
37
45
38
- p := tea .NewProgram (initialize (items , endResult ), update , view )
46
+ p := tea .NewProgram (initialize (stringerList , endResult ), update , view )
39
47
40
48
// Use the full size of the terminal in its "alternate screen buffer"
41
49
fullScreen := false // change to true if you want fullscreen
@@ -60,7 +68,7 @@ func main() {
60
68
61
69
// initialize sets up the model and returns it to the bubbletea runtime
62
70
// as a function result, so it can later be handed over to the update and view functions.
63
- func initialize (lineList []string , endResult chan <- string ) func () (tea.Model , tea.Cmd ) {
71
+ func initialize (lineList []fmt. Stringer , endResult chan <- string ) func () (tea.Model , tea.Cmd ) {
64
72
l := list .NewModel ()
65
73
l .AddItems (lineList )
66
74
// l.WrapPrefix = false // uncomment for fancy check (selected) box :-)
@@ -123,8 +131,12 @@ func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {
123
131
124
132
// Enter prints the selected lines to StdOut
125
133
if msg .Type == tea .KeyEnter {
126
- result := strings .Join (m .list .GetSelected (), "\n " )
127
- m .endResult <- result
134
+ var result bytes.Buffer
135
+ for _ , item := range m .list .GetSelected () {
136
+ result .WriteString (item .String ())
137
+ result .WriteString ("\n " )
138
+ }
139
+ m .endResult <- result .String ()
128
140
return m , tea .Quit
129
141
}
130
142
0 commit comments