-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtea.go
87 lines (69 loc) · 1.6 KB
/
tea.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"fmt"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
)
type TUIChoice string
type GopherModel struct {
choices TUIChoice
cursor int
selected map[int]struct{}
tuiInput textinput.Model
}
func NewGopherModel(tuiInput textinput.Model) *GopherModel {
return &GopherModel{
tuiInput: tuiInput,
selected: make(map[int]struct{}),
}
}
func (a *App) Init() tea.Cmd {
return nil
}
func (a *App) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
// Check if it is a key press
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
return a, tea.Quit
case "up", "k":
if a.GopherModel.cursor > 0 {
a.GopherModel.cursor--
}
case "down", "j":
if a.GopherModel.cursor < len(a.GopherModel.choices)-1 {
a.GopherModel.cursor++
}
case "enter", " ":
_, ok := a.GopherModel.selected[a.GopherModel.cursor]
if ok {
delete(a.GopherModel.selected, a.GopherModel.cursor)
} else {
a.GopherModel.selected[a.GopherModel.cursor] = struct{}{}
}
}
}
return a, nil
}
func (a *App) View() string {
s := "What table are you working on? \n\n"
choices := a.Tables
choiceList := []string{}
for _, v := range choices {
choiceList = append(choiceList, v.GetTableName())
}
for i, v := range choiceList {
cursor := " " // No cursor used yet
if a.GopherModel.cursor == i {
cursor = ">"
}
checked := " " // not selected
if _, ok := a.GopherModel.selected[i]; ok {
checked = "y"
}
s += fmt.Sprintf("%s [%s] %s\n", cursor, checked, v)
}
s += "\nPress q to quit.\n"
return s
}