Skip to content

Commit 78ee0fb

Browse files
authored
Merge pull request #1 from initia-labs/feat/skeleton-refactor
feat: alternative of existing skeleton
2 parents 2c9c04c + 081580a commit 78ee0fb

File tree

9 files changed

+158
-16
lines changed

9 files changed

+158
-16
lines changed

cmd/root.go

+2-10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/spf13/cobra"
88
"github.com/spf13/viper"
99

10+
"github.com/initia-labs/weave/models/demo"
1011
"github.com/initia-labs/weave/states"
1112
)
1213

@@ -27,16 +28,7 @@ func Execute() error {
2728
return nil
2829
},
2930
RunE: func(cmd *cobra.Command, args []string) error {
30-
31-
// Initialize state transitions and ensure they're set up correctly
32-
states.GetHomePage() // Initialize homepage state
33-
34-
states.SetHomePageTransitions() // Set homepage transitions lazily after initialization
35-
states.SetInitiaInitTransitions() // Initialize transitions for InitiaInit
36-
states.SetRunL1NodeTransitions() // Initialize transitions for RunL1Node
37-
states.SetRunL1NodeTextInputTransitions() // Initialize transitions for RunL1NodeTextInput
38-
39-
_, err := tea.NewProgram(states.GetHomePage()).Run()
31+
_, err := tea.NewProgram(demo.New()).Run()
4032
if err != nil {
4133
return err
4234
}

go.mod

-2
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@ module github.com/initia-labs/weave
33
go 1.22.6
44

55
require (
6-
github.com/charmbracelet/bubbles v0.19.0
76
github.com/charmbracelet/bubbletea v1.1.0
87
github.com/spf13/cobra v1.8.1
98
github.com/spf13/viper v1.19.0
109
github.com/stretchr/testify v1.9.0
1110
)
1211

1312
require (
14-
github.com/atotto/clipboard v0.1.4 // indirect
1513
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
1614
github.com/charmbracelet/lipgloss v0.13.0 // indirect
1715
github.com/charmbracelet/x/ansi v0.2.3 // indirect

go.sum

-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
2-
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
31
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
42
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
5-
github.com/charmbracelet/bubbles v0.19.0 h1:gKZkKXPP6GlDk6EcfujDK19PCQqRjaJZQ7QRERx1UF0=
6-
github.com/charmbracelet/bubbles v0.19.0/go.mod h1:WILteEqZ+krG5c3ntGEMeG99nCupcuIk7V0/zOP0tOA=
73
github.com/charmbracelet/bubbletea v1.1.0 h1:FjAl9eAL3HBCHenhz/ZPjkKdScmaS5SK69JAK2YJK9c=
84
github.com/charmbracelet/bubbletea v1.1.0/go.mod h1:9Ogk0HrdbHolIKHdjfFpyXJmiCzGwy+FesYkZr7hYU4=
95
github.com/charmbracelet/lipgloss v0.13.0 h1:4X3PPeoWEDCMvzDvGmTajSyYPcZM4+y8sCA/SsA3cjw=

models/demo/denom.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package demo
2+
3+
import (
4+
"fmt"
5+
6+
tea "github.com/charmbracelet/bubbletea"
7+
8+
"github.com/initia-labs/weave/utils"
9+
)
10+
11+
type Denom struct {
12+
input utils.TextInput
13+
state *State
14+
}
15+
16+
func NewDenom(state *State) *Denom {
17+
return &Denom{
18+
input: "umin",
19+
state: state,
20+
}
21+
}
22+
23+
func (m *Denom) Init() tea.Cmd {
24+
return nil
25+
}
26+
27+
func (m *Denom) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
28+
input, done := m.input.Update(msg)
29+
if done {
30+
m.state.denom = string(input)
31+
fmt.Println("[info] state ", m.state)
32+
return m, tea.Quit
33+
}
34+
m.input = input
35+
return m, nil
36+
}
37+
38+
func (m *Denom) View() string {
39+
return fmt.Sprintf("Enter the denom: %s\n", m.input)
40+
}

models/demo/model.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package demo
2+
3+
import tea "github.com/charmbracelet/bubbletea"
4+
5+
func New() tea.Model {
6+
return NewVMSelect(&State{})
7+
}

models/demo/state.go

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package demo
2+
3+
type State struct {
4+
vm string
5+
denom string
6+
}

models/demo/vm_select.go

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package demo
2+
3+
import (
4+
tea "github.com/charmbracelet/bubbletea"
5+
"github.com/initia-labs/weave/utils"
6+
)
7+
8+
type VMSelect struct {
9+
utils.Selector[string]
10+
state *State
11+
}
12+
13+
func NewVMSelect(state *State) *VMSelect {
14+
return &VMSelect{
15+
Selector: utils.Selector[string]{
16+
Options: []string{
17+
"move",
18+
"wasm",
19+
"evm",
20+
},
21+
Cursor: 0,
22+
},
23+
state: state,
24+
}
25+
}
26+
27+
func (m *VMSelect) Init() tea.Cmd {
28+
return nil
29+
}
30+
31+
func (m *VMSelect) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
32+
selected, cmd := m.Select(msg)
33+
if selected != nil {
34+
m.state.vm = *selected
35+
return NewDenom(m.state), nil
36+
}
37+
38+
return m, cmd
39+
}
40+
41+
func (m *VMSelect) View() string {
42+
view := "Which vm would you like to build on?\n"
43+
for i, option := range m.Options {
44+
if i == m.Cursor {
45+
view += "(•) " + option + "\n"
46+
} else {
47+
view += "( ) " + option + "\n"
48+
}
49+
}
50+
return view + "\nPress Enter to select, or q to quit."
51+
}

utils/selector.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package utils
2+
3+
import tea "github.com/charmbracelet/bubbletea"
4+
5+
type Selector[T any] struct {
6+
Options []T
7+
Cursor int
8+
}
9+
10+
func (s *Selector[T]) Select(msg tea.Msg) (*T, tea.Cmd) {
11+
switch msg := msg.(type) {
12+
case tea.KeyMsg:
13+
switch msg.String() {
14+
case "down", "j":
15+
s.Cursor = (s.Cursor + 1) % len(s.Options)
16+
return nil, nil
17+
case "up", "k":
18+
s.Cursor = (s.Cursor - 1 + len(s.Options)) % len(s.Options)
19+
return nil, nil
20+
case "q", "ctrl+c":
21+
return nil, tea.Quit
22+
case "enter":
23+
return &s.Options[s.Cursor], nil
24+
}
25+
}
26+
return nil, nil
27+
}

utils/text_input.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package utils
2+
3+
import tea "github.com/charmbracelet/bubbletea"
4+
5+
type TextInput string
6+
7+
func (ti TextInput) Update(msg tea.Msg) (TextInput, bool) {
8+
switch msg := msg.(type) {
9+
case tea.KeyMsg:
10+
switch msg.Type {
11+
case tea.KeyEnter:
12+
return ti, true
13+
default:
14+
return HandleKeyInput(ti, msg), false
15+
}
16+
}
17+
return ti, false
18+
}
19+
20+
func HandleKeyInput(currentText TextInput, msg tea.KeyMsg) TextInput {
21+
if msg.Type == tea.KeyRunes {
22+
return currentText + TextInput(string(msg.Runes))
23+
}
24+
return currentText
25+
}

0 commit comments

Comments
 (0)