Skip to content

Commit 7e3043a

Browse files
author
Kilian Drechsler
committed
changed to interface fmt.Stringer within item struct
- changed List implementation from operating on string values, within of stuct item, to work with interface: fmt.Stringer To allow *inherent* relation safety betwen user structs and displayed string! - added StringItem Struct to allow still for ease of use. - changed test and example accordingly - renamed some variables
1 parent 617f0cc commit 7e3043a

File tree

3 files changed

+116
-81
lines changed

3 files changed

+116
-81
lines changed

list/example/main.go

+19-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package main
22

33
import (
4+
"bytes"
45
"fmt"
56
"github.com/charmbracelet/bubbles/list"
67
tea "github.com/charmbracelet/bubbletea"
78
"os"
89
"strconv"
9-
"strings"
1010
)
1111

1212
type model struct {
@@ -17,11 +17,17 @@ type model struct {
1717
jump string
1818
}
1919

20+
type stringItem string
21+
22+
func (s stringItem) String() string {
23+
return string(s)
24+
}
25+
2026
func main() {
21-
items := []string{
27+
itemList := []string{
2228
"Welcome to the bubbles-list example!",
2329
"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'.",
2531
"Move to the beginning with 'g' and to the end with 'G'.",
2632
"Sort the entrys with 's', but be carefull you can't unsort it again.",
2733
"The list can handel linebreaks,\nand has wordwrap enabled if the line gets to long.",
@@ -33,9 +39,11 @@ func main() {
3339
"The key 'v' inverts the selected state of each item.",
3440
"To toggle betwen only absolute itemnumbers and also relativ numbers, the 'r' key is your friend.",
3541
}
42+
stringerList := list.MakeStringerList(itemList)
43+
3644
endResult := make(chan string, 1)
3745

38-
p := tea.NewProgram(initialize(items, endResult), update, view)
46+
p := tea.NewProgram(initialize(stringerList, endResult), update, view)
3947

4048
// Use the full size of the terminal in its "alternate screen buffer"
4149
fullScreen := false // change to true if you want fullscreen
@@ -60,7 +68,7 @@ func main() {
6068

6169
// initialize sets up the model and returns it to the bubbletea runtime
6270
// 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) {
6472
l := list.NewModel()
6573
l.AddItems(lineList)
6674
// 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) {
123131

124132
// Enter prints the selected lines to StdOut
125133
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()
128140
return m, tea.Quit
129141
}
130142

0 commit comments

Comments
 (0)