Skip to content

Commit 5e91eb2

Browse files
committed
feat: view for selector
1 parent ec959fc commit 5e91eb2

File tree

5 files changed

+40
-63
lines changed

5 files changed

+40
-63
lines changed

models/homepage.go

+6-16
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package models
22

33
import (
4-
"fmt"
5-
"github.com/initia-labs/weave/styles"
6-
74
tea "github.com/charmbracelet/bubbletea"
5+
86
"github.com/initia-labs/weave/models/weaveinit"
7+
"github.com/initia-labs/weave/styles"
98
"github.com/initia-labs/weave/utils"
109
)
1110

@@ -67,20 +66,11 @@ func (m *Homepage) View() string {
6766
view := "Hi 👋🏻 Weave is a CLI for managing Initia deployments.\n"
6867

6968
if m.isFirstTimeSetup {
70-
view += fmt.Sprintf(
71-
styles.RenderPrompt("It looks like this is your first time using Weave. Let's get started!\nPlease set up a Gas Station account (The account that will hold the funds required by the OPinit-bots or relayer to send transactions):", []string{}, styles.Information),
72-
m.TextInput.View(),
73-
)
69+
view += styles.RenderPrompt("It looks like this is your first time using Weave. Let's get started!\n"+
70+
"Please set up a Gas Station account (The account that will hold the funds required by the "+
71+
"OPinit-bots or relayer to send transactions):", []string{}, styles.Information) + m.TextInput.View()
7472
} else {
75-
view += "What would you like to do today?\n"
76-
for i, option := range m.Options {
77-
if i == m.Cursor {
78-
view += "(■) " + string(option) + "\n"
79-
} else {
80-
view += "( ) " + string(option) + "\n"
81-
}
82-
}
83-
view += "\nPress Enter to select, or q to quit."
73+
view += styles.RenderPrompt("What would you like to do today?", []string{}, styles.Question) + m.Selector.View()
8474
}
8575

8676
return view

models/weaveinit/run_l1_node.go

+6-36
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package weaveinit
22

33
import (
44
"fmt"
5-
"github.com/initia-labs/weave/styles"
65
"os"
76
"path/filepath"
87

98
tea "github.com/charmbracelet/bubbletea"
109

10+
"github.com/initia-labs/weave/styles"
1111
"github.com/initia-labs/weave/utils"
1212
)
1313

@@ -66,19 +66,7 @@ func (m *RunL1NodeNetworkSelect) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
6666
}
6767

6868
func (m *RunL1NodeNetworkSelect) View() string {
69-
view := m.state.weave.PreviousResponse + styles.RenderPrompt(
70-
m.GetQuestion(),
71-
[]string{},
72-
styles.Question,
73-
) + "\n"
74-
for i, option := range m.Options {
75-
if i == m.Cursor {
76-
view += "(■) " + string(option) + "\n"
77-
} else {
78-
view += "( ) " + string(option) + "\n"
79-
}
80-
}
81-
return view + "\nPress Enter to select, or q to quit."
69+
return styles.RenderPrompt("Which network will your node participate in?", []string{}, styles.Question) + m.Selector.View()
8270
}
8371

8472
type RunL1NodeVersionInput struct {
@@ -287,19 +275,7 @@ func (m *ExistingAppReplaceSelect) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
287275
}
288276

289277
func (m *ExistingAppReplaceSelect) View() string {
290-
view := m.state.weave.PreviousResponse + styles.RenderPrompt(
291-
m.GetQuestion(),
292-
[]string{},
293-
styles.Question,
294-
) + "\n"
295-
for i, option := range m.Options {
296-
if i == m.Cursor {
297-
view += "(■) " + string(option) + "\n"
298-
} else {
299-
view += "( ) " + string(option) + "\n"
300-
}
301-
}
302-
return view + "\nPress Enter to select, or q to quit."
278+
return styles.RenderPrompt("Existing config/app.toml and config/config.toml detected. Would you like to use the current files or replace them", []string{"config/app.toml", "config/config.toml"}, styles.Question) + m.Selector.View()
303279
}
304280

305281
type MinGasPriceInput struct {
@@ -569,17 +545,11 @@ func (m *ExistingGenesisReplaceSelect) Update(msg tea.Msg) (tea.Model, tea.Cmd)
569545
func (m *ExistingGenesisReplaceSelect) View() string {
570546
view := m.state.weave.PreviousResponse + styles.RenderPrompt(
571547
m.GetQuestion(),
572-
[]string{},
548+
[]string{"config/genesis.json"},
573549
styles.Question,
574550
) + "\n"
575-
for i, option := range m.Options {
576-
if i == m.Cursor {
577-
view += "(■) " + string(option) + "\n"
578-
} else {
579-
view += "( ) " + string(option) + "\n"
580-
}
581-
}
582-
return view + "\nPress Enter to select, or q to quit."
551+
552+
return view + m.Selector.View()
583553
}
584554

585555
type GenesisEndpointInput struct {

models/weaveinit/weaveinit.go

+2-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package weaveinit
22

33
import (
44
tea "github.com/charmbracelet/bubbletea"
5+
"github.com/initia-labs/weave/styles"
56
"github.com/initia-labs/weave/utils"
67
)
78

@@ -49,13 +50,5 @@ func (m *WeaveInit) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
4950
}
5051

5152
func (m *WeaveInit) View() string {
52-
view := "? What action would you like to perform?\n"
53-
for i, option := range m.Options {
54-
if i == m.Cursor {
55-
view += "(■) " + string(option) + "\n"
56-
} else {
57-
view += "( ) " + string(option) + "\n"
58-
}
59-
}
60-
return view + "\nPress Enter to select, or q to quit."
53+
return styles.RenderPrompt("What action would you like to perform?", []string{}, styles.Question) + m.Selector.View()
6154
}

styles/text.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ var (
4141
QuestionMark string = Text("? ", Cyan)
4242
CorrectMark string = Text("✓ ", Green)
4343
InformationMark string = Text("i ", Cyan)
44+
SelectorCursor string = Text("> ", Cyan)
4445
)
4546

4647
// RenderPrompt highlights phrases in the text if they match any phrase in the highlights list
@@ -59,7 +60,12 @@ func RenderPrompt(text string, highlights []string, status PromptStatus) string
5960
for _, highlight := range highlights {
6061
if strings.Contains(text, highlight) {
6162
// Apply Cyan color to the matching highlight
62-
coloredHighlight := BoldText(highlight, Cyan)
63+
coloredHighlight := ""
64+
if status == Information {
65+
coloredHighlight = BoldText(highlight, White)
66+
} else {
67+
coloredHighlight = BoldText(highlight, Cyan)
68+
}
6369
text = strings.ReplaceAll(text, highlight, coloredHighlight)
6470
}
6571
}

utils/selector.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package utils
22

3-
import tea "github.com/charmbracelet/bubbletea"
3+
import (
4+
"fmt"
5+
6+
tea "github.com/charmbracelet/bubbletea"
7+
"github.com/initia-labs/weave/styles"
8+
)
49

510
type Selector[T any] struct {
611
Options []T
@@ -25,3 +30,16 @@ func (s *Selector[T]) Select(msg tea.Msg) (*T, tea.Cmd) {
2530
}
2631
return nil, nil
2732
}
33+
34+
func (s *Selector[T]) View() string {
35+
view := "\n"
36+
for i, option := range s.Options {
37+
if i == s.Cursor {
38+
view += styles.SelectorCursor + styles.BoldText(fmt.Sprintf("%v", option), styles.White) + "\n"
39+
} else {
40+
view += " " + styles.Text(fmt.Sprintf("%v", option), styles.White) + "\n"
41+
}
42+
}
43+
44+
return view + "Press Enter to select, or q to quit.\n"
45+
}

0 commit comments

Comments
 (0)