-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.go
More file actions
130 lines (115 loc) · 2.62 KB
/
cli.go
File metadata and controls
130 lines (115 loc) · 2.62 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"context"
"errors"
"io"
"log/slog"
"strconv"
"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/abennett/ttt/pkg/client"
"github.com/abennett/ttt/pkg/messages"
)
var ErrTooManyRedirects = errors.New("too many redirects")
var baseStyle = lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder()).
Align(lipgloss.Center)
var columns = []table.Column{
{Title: "User", Width: 10},
{Title: "Result", Width: 6},
{Title: "Done", Width: 6},
}
type ttt struct {
client *client.Client
table table.Model
}
func newTTT(c *client.Client) (*ttt, error) {
t := table.New(
table.WithColumns(columns),
table.WithHeight(0),
table.WithFocused(false),
)
s := table.DefaultStyles()
s.Header = s.Header.Foreground(lipgloss.Color("#01c5d1"))
s.Selected = s.Selected.Foreground(lipgloss.NoColor{}).Bold(false)
t.SetStyles(s)
return &ttt{
client: c,
table: t,
}, nil
}
func (t *ttt) Init() tea.Cmd {
err := t.client.Init()
if err != nil {
panic(err)
}
return func() tea.Msg {
return t.client.ReadUpdate()
}
}
func resultsToRows(rrs []messages.RollResult) []table.Row {
rows := make([]table.Row, len(rrs))
for idx, rr := range rrs {
if rr.IsDone {
rows[idx] = table.Row{rr.User, strconv.Itoa(rr.Result), "✅"}
} else {
rows[idx] = table.Row{rr.User, strconv.Itoa(rr.Result), ""}
}
}
return rows
}
func (t *ttt) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case []messages.RollResult:
slog.Debug("roll result")
t.table.SetHeight(len(msg) + 1)
t.table.SetRows(resultsToRows(msg))
for _, rr := range msg {
if !rr.IsDone {
return t, func() tea.Msg {
return t.client.ReadUpdate()
}
}
}
return t, tea.Quit
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
err := t.client.Close()
if err != nil {
slog.Error("failed to close client", "error", err)
}
return t, tea.Quit
// Attempt to update done index
case " ":
err := t.client.ToggleDone()
if err != nil {
panic(err)
}
}
case error:
slog.Error("exiting for error", "error", msg)
return t, tea.Quit
default:
slog.Debug("unsupported message", "msg", msg)
}
slog.Debug("no update")
return t, nil
}
func (t *ttt) View() string {
slog.Debug("rerendering view")
return baseStyle.Render(t.table.View()) + "\n"
}
func rollRemote(_ context.Context, args []string) error {
c, err := client.New(args[0], args[1], args[2], io.Discard)
if err != nil {
return err
}
ttt, err := newTTT(c)
if err != nil {
return err
}
_, err = tea.NewProgram(ttt).Run()
return err
}