Skip to content

Commit 4cf2f7c

Browse files
committed
feat: emit errors to the render
1 parent f4bf128 commit 4cf2f7c

File tree

5 files changed

+76
-2
lines changed

5 files changed

+76
-2
lines changed

cmd/root.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,12 @@ var (
7575
)
7676
go func() {
7777
state.Watch(func(status *internal.StateModel, err error) {
78-
cobra.CheckErr(err)
79-
p.Send(state)
78+
if err == nil {
79+
p.Send(state)
80+
}
81+
if err != nil {
82+
p.Send(err)
83+
}
8084
}, context.Background(), client)
8185
}()
8286
_, err = p.Run()

internal/state.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func (s *StateModel) Watch(cb func(model *StateModel, err error), ctx context.Co
4040
}
4141
if status.StatusCode() != 200 {
4242
cb(nil, errors.New(status.Status()))
43+
return
4344
}
4445

4546
// Update Status

ui/error.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package ui
2+
3+
import (
4+
"github.com/algorandfoundation/hack-tui/ui/controls"
5+
tea "github.com/charmbracelet/bubbletea"
6+
"github.com/charmbracelet/lipgloss"
7+
"strings"
8+
)
9+
10+
type ErrorViewModel struct {
11+
Height int
12+
Width int
13+
controls controls.Model
14+
Message string
15+
}
16+
17+
func NewErrorViewModel(message string) ErrorViewModel {
18+
return ErrorViewModel{
19+
Height: 0,
20+
Width: 0,
21+
controls: controls.New(" Error "),
22+
}
23+
}
24+
25+
func (m ErrorViewModel) Init() tea.Cmd {
26+
return nil
27+
}
28+
29+
func (m ErrorViewModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
30+
return m.HandleMessage(msg)
31+
}
32+
33+
func (m ErrorViewModel) HandleMessage(msg tea.Msg) (ErrorViewModel, tea.Cmd) {
34+
var cmd tea.Cmd
35+
switch msg := msg.(type) {
36+
37+
case tea.WindowSizeMsg:
38+
m.Width = msg.Width
39+
m.Height = msg.Height - 2
40+
}
41+
m.controls, cmd = m.controls.HandleMessage(msg)
42+
return m, cmd
43+
}
44+
45+
func (m ErrorViewModel) View() string {
46+
pad := strings.Repeat("\n", max(0, m.Height/2-1))
47+
return lipgloss.JoinVertical(lipgloss.Center, pad, red.Render(m.Message), pad, m.controls.View())
48+
}

ui/style.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var (
2323
cyan = lipgloss.NewStyle().Foreground(lipgloss.Color("14"))
2424
yellow = lipgloss.NewStyle().Foreground(lipgloss.Color("11"))
2525
green = lipgloss.NewStyle().Foreground(lipgloss.Color("10"))
26+
red = lipgloss.NewStyle().Foreground(lipgloss.Color("9"))
2627
Magenta = lipgloss.NewStyle().
2728
Foreground(lipgloss.Color("5")).
2829
Render

ui/viewport.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const (
2020
KeysPage ViewportPage = "keys"
2121
GeneratePage ViewportPage = "generate"
2222
TransactionPage ViewportPage = "transaction"
23+
ErrorPage ViewportPage = "error"
2324
)
2425

2526
type ViewportViewModel struct {
@@ -40,6 +41,10 @@ type ViewportViewModel struct {
4041

4142
page ViewportPage
4243
client *api.ClientWithResponses
44+
45+
// Error Handler
46+
errorMsg *string
47+
errorPage ErrorViewModel
4348
}
4449

4550
type DeleteFinished string
@@ -72,6 +77,9 @@ func (m ViewportViewModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
7277
cmds = append(cmds, cmd)
7378

7479
switch msg := msg.(type) {
80+
case error:
81+
strMsg := msg.Error()
82+
m.errorMsg = &strMsg
7583
// When the state updates
7684
case internal.StateModel:
7785
m.Data = &msg
@@ -158,6 +166,7 @@ func (m ViewportViewModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
158166
m.transactionPage, cmd = m.transactionPage.HandleMessage(pageMsg)
159167
cmds = append(cmds, cmd)
160168
//}
169+
m.errorPage, cmd = m.errorPage.HandleMessage(pageMsg)
161170
cmds = append(cmds, cmd)
162171
// Avoid triggering commands again
163172
return m, tea.Batch(cmds...)
@@ -180,6 +189,13 @@ func (m ViewportViewModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
180189

181190
// View renders the viewport.Model
182191
func (m ViewportViewModel) View() string {
192+
errMsg := m.errorMsg
193+
194+
if errMsg != nil {
195+
m.errorPage.Message = *errMsg
196+
m.page = ErrorPage
197+
}
198+
183199
// Handle Page render
184200
var page tea.Model
185201
switch m.page {
@@ -191,6 +207,8 @@ func (m ViewportViewModel) View() string {
191207
page = m.keysPage
192208
case TransactionPage:
193209
page = m.transactionPage
210+
case ErrorPage:
211+
page = m.errorPage
194212
}
195213

196214
if page == nil {
@@ -235,6 +253,8 @@ func MakeViewportViewModel(state *internal.StateModel, client *api.ClientWithRes
235253
page: AccountsPage,
236254
// RPC client
237255
client: client,
256+
257+
errorPage: NewErrorViewModel(""),
238258
}
239259

240260
return &m, nil

0 commit comments

Comments
 (0)