forked from strangelove-ventures/interchaintest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
90 lines (72 loc) · 2.41 KB
/
model.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
88
89
90
package tui
import (
"context"
"time"
"github.com/atotto/clipboard"
"github.com/odin-protocol/interchaintest/v7/internal/blockdb"
"github.com/rivo/tview"
)
//go:generate go run golang.org/x/tools/cmd/stringer -type=mainContent
// mainContent is the primary content for user interaction in the UI akin to html <main>.
type mainContent int
const (
testCasesMain mainContent = iota
cosmosMessagesMain
txDetailMain
errorModalMain
)
type mainStack []mainContent
func (stack mainStack) Push(s mainContent) []mainContent { return append(stack, s) }
func (stack mainStack) Current() mainContent { return stack[len(stack)-1] }
func (stack mainStack) Pop() []mainContent { return stack[:len(stack)-1] }
// QueryService fetches data from a database.
type QueryService interface {
CosmosMessages(ctx context.Context, chainPkey int64) ([]blockdb.CosmosMessageResult, error)
Transactions(ctx context.Context, chainPkey int64) ([]blockdb.TxResult, error)
}
// Model encapsulates state that updates a view.
type Model struct {
querySvc QueryService
databasePath string
schemaVersion string
schemaDate time.Time
testCases []blockdb.TestCaseResult
layout *tview.Flex
// stack keeps tracks of primary content pushed and popped
stack mainStack
// write to the system clipboard
clipboard func(text string) error
}
// NewModel returns a valid *Model.
func NewModel(
querySvc QueryService,
databasePath string,
schemaVersion string,
schemaDate time.Time,
testCases []blockdb.TestCaseResult,
) *Model {
m := &Model{
querySvc: querySvc,
databasePath: databasePath,
schemaVersion: schemaVersion,
schemaDate: schemaDate,
testCases: testCases,
stack: mainStack{testCasesMain},
clipboard: clipboard.WriteAll,
}
flex := tview.NewFlex().SetDirection(tview.FlexRow)
flex.SetBackgroundColor(backgroundColor).SetBorder(false)
// Setting fixed size keeps the header height stable, so it will show all help keys.
flex.AddItem(headerView(m), 6, 1, false)
// The primary view is a page view to act like a stack where we can push and pop views.
// Flex and grid views do not allow a "stack-like" behavior.
pages := tview.NewPages()
pages.AddAndSwitchToPage(m.stack[0].String(), testCasesView(m), true)
flex.AddItem(pages, 0, 10, true)
m.layout = flex
return m
}
// RootView is a root view for a tview.Application.
func (m *Model) RootView() *tview.Flex {
return m.layout
}