Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: alternative of existing skeleton #1

Merged
merged 2 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/initia-labs/weave/models/demo"
"github.com/initia-labs/weave/states"
)

Expand All @@ -27,16 +28,7 @@ func Execute() error {
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {

// Initialize state transitions and ensure they're set up correctly
states.GetHomePage() // Initialize homepage state

states.SetHomePageTransitions() // Set homepage transitions lazily after initialization
states.SetInitiaInitTransitions() // Initialize transitions for InitiaInit
states.SetRunL1NodeTransitions() // Initialize transitions for RunL1Node
states.SetRunL1NodeTextInputTransitions() // Initialize transitions for RunL1NodeTextInput

_, err := tea.NewProgram(states.GetHomePage()).Run()
_, err := tea.NewProgram(demo.New()).Run()
if err != nil {
return err
}
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ module github.com/initia-labs/weave
go 1.22.6

require (
github.com/charmbracelet/bubbles v0.19.0
github.com/charmbracelet/bubbletea v1.1.0
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
github.com/stretchr/testify v1.9.0
)

require (
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/lipgloss v0.13.0 // indirect
github.com/charmbracelet/x/ansi v0.2.3 // indirect
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/charmbracelet/bubbles v0.19.0 h1:gKZkKXPP6GlDk6EcfujDK19PCQqRjaJZQ7QRERx1UF0=
github.com/charmbracelet/bubbles v0.19.0/go.mod h1:WILteEqZ+krG5c3ntGEMeG99nCupcuIk7V0/zOP0tOA=
github.com/charmbracelet/bubbletea v1.1.0 h1:FjAl9eAL3HBCHenhz/ZPjkKdScmaS5SK69JAK2YJK9c=
github.com/charmbracelet/bubbletea v1.1.0/go.mod h1:9Ogk0HrdbHolIKHdjfFpyXJmiCzGwy+FesYkZr7hYU4=
github.com/charmbracelet/lipgloss v0.13.0 h1:4X3PPeoWEDCMvzDvGmTajSyYPcZM4+y8sCA/SsA3cjw=
Expand Down
40 changes: 40 additions & 0 deletions models/demo/denom.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package demo

import (
"fmt"

tea "github.com/charmbracelet/bubbletea"

"github.com/initia-labs/weave/utils"
)

type Denom struct {
input utils.TextInput
state *State
}

func NewDenom(state *State) *Denom {
return &Denom{
input: "umin",
state: state,
}
}

func (m *Denom) Init() tea.Cmd {
return nil
}

func (m *Denom) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
input, done := m.input.Update(msg)
if done {
m.state.denom = string(input)
fmt.Println("[info] state ", m.state)
return m, tea.Quit
}
m.input = input
return m, nil
}

func (m *Denom) View() string {
return fmt.Sprintf("Enter the denom: %s\n", m.input)
}
7 changes: 7 additions & 0 deletions models/demo/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package demo

import tea "github.com/charmbracelet/bubbletea"

func New() tea.Model {
return NewVMSelect(&State{})
}
6 changes: 6 additions & 0 deletions models/demo/state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package demo

type State struct {
vm string
denom string
}
51 changes: 51 additions & 0 deletions models/demo/vm_select.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package demo

import (
tea "github.com/charmbracelet/bubbletea"
"github.com/initia-labs/weave/utils"
)

type VMSelect struct {
utils.Selector[string]
state *State
}

func NewVMSelect(state *State) *VMSelect {
return &VMSelect{
Selector: utils.Selector[string]{
Options: []string{
"move",
"wasm",
"evm",
},
Cursor: 0,
},
state: state,
}
}

func (m *VMSelect) Init() tea.Cmd {
return nil
}

func (m *VMSelect) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
selected, cmd := m.Select(msg)
if selected != nil {
m.state.vm = *selected
return NewDenom(m.state), nil
}

return m, cmd
}

func (m *VMSelect) View() string {
view := "Which vm would you like to build on?\n"
for i, option := range m.Options {
if i == m.Cursor {
view += "(•) " + option + "\n"
} else {
view += "( ) " + option + "\n"
}
}
return view + "\nPress Enter to select, or q to quit."
}
27 changes: 27 additions & 0 deletions utils/selector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package utils

import tea "github.com/charmbracelet/bubbletea"

type Selector[T any] struct {
Options []T
Cursor int
}

func (s *Selector[T]) Select(msg tea.Msg) (*T, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "down", "j":
s.Cursor = (s.Cursor + 1) % len(s.Options)
return nil, nil
case "up", "k":
s.Cursor = (s.Cursor - 1 + len(s.Options)) % len(s.Options)
return nil, nil
case "q", "ctrl+c":
return nil, tea.Quit
case "enter":
return &s.Options[s.Cursor], nil
}
}
return nil, nil
}
25 changes: 25 additions & 0 deletions utils/text_input.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package utils

import tea "github.com/charmbracelet/bubbletea"

type TextInput string

func (ti TextInput) Update(msg tea.Msg) (TextInput, bool) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.Type {
case tea.KeyEnter:
return ti, true
default:
return HandleKeyInput(ti, msg), false
}
}
return ti, false
}

func HandleKeyInput(currentText TextInput, msg tea.KeyMsg) TextInput {
if msg.Type == tea.KeyRunes {
return currentText + TextInput(string(msg.Runes))
}
return currentText
}
Loading