Skip to content

Commit

Permalink
feat: basic keep previous responses
Browse files Browse the repository at this point in the history
  • Loading branch information
traviolus committed Sep 11, 2024
1 parent 470ab6a commit d6dc1aa
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 13 deletions.
23 changes: 18 additions & 5 deletions models/weaveinit/run_l1_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package weaveinit

import (
"fmt"
"github.com/initia-labs/weave/styles"
"os"
"path/filepath"

Expand All @@ -12,7 +13,8 @@ import (

type RunL1NodeNetworkSelect struct {
utils.Selector[L1NodeNetworkOption]
state *RunL1NodeState
state *RunL1NodeState
question string
}

type L1NodeNetworkOption string
Expand All @@ -32,18 +34,25 @@ func NewRunL1NodeNetworkSelect(state *RunL1NodeState) *RunL1NodeNetworkSelect {
Local,
},
},
state: state,
state: state,
question: "Which network will your node participate in?",
}
}

func (m *RunL1NodeNetworkSelect) GetQuestion() string {
return m.question
}

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

func (m *RunL1NodeNetworkSelect) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
selected, cmd := m.Select(msg)
if selected != nil {
m.state.network = string(*selected)
selectedString := string(*selected)
m.state.network = selectedString
m.state.weave.PreviousResponse += styles.RenderPreviousResponse(styles.ArrowSeparator, m.GetQuestion(), []string{}, selectedString)
switch *selected {
case Mainnet, Testnet:
return NewExistingAppChecker(m.state), utils.DoTick()
Expand All @@ -57,7 +66,11 @@ func (m *RunL1NodeNetworkSelect) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

func (m *RunL1NodeNetworkSelect) View() string {
view := "? Which network will your node participate in?\n"
view := m.state.weave.PreviousResponse + styles.RenderPrompt(
"Which network will your node participate in?\n",
[]string{},
styles.Question,
)
for i, option := range m.Options {
if i == m.Cursor {
view += "(■) " + string(option) + "\n"
Expand Down Expand Up @@ -95,7 +108,7 @@ func (m *RunL1NodeVersionInput) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

func (m *RunL1NodeVersionInput) View() string {
return m.TextInput.View("Please specify the initiad version")
return m.state.weave.PreviousResponse + m.TextInput.View("Please specify the initiad version")
}

type RunL1NodeChainIdInput struct {
Expand Down
3 changes: 3 additions & 0 deletions models/weaveinit/state.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package weaveinit

import "github.com/initia-labs/weave/types"

type RunL1NodeState struct {
weave types.WeaveState
network string
initiadVersion string
chainId string
Expand Down
19 changes: 19 additions & 0 deletions styles/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,22 @@ func RenderPrompt(text string, highlights []string, status PromptStatus) string
// Return the prompt with the highlighted text
return prompt + text
}

type ResponseSeparator int64

const (
ArrowSeparator ResponseSeparator = iota
DotsSeparator
)

func RenderPreviousResponse(separator ResponseSeparator, question string, highlights []string, answer string) string {
var separatorString string
switch separator {
case ArrowSeparator:
separatorString = Text(" > ", Gray)
case DotsSeparator:
separatorString = Text(" ... ", Gray)
}

return RenderPrompt(question, highlights, Completed) + separatorString + answer + "\n"
}
5 changes: 5 additions & 0 deletions types/state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package types

type WeaveState struct {
PreviousResponse string
}
14 changes: 6 additions & 8 deletions utils/text_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,21 @@ type TextInput struct {
Text string
Cursor int // Cursor position within the text
Placeholder string
Entered bool
}

func NewTextInput() TextInput {
return TextInput{Text: "", Cursor: 0, Placeholder: "<todo: Jennie revisit placeholder>", Entered: false}
return TextInput{
Text: "",
Cursor: 0,
Placeholder: "<todo: Jennie revisit placeholder>",
}
}

func (ti TextInput) Update(msg tea.Msg) (TextInput, tea.Cmd, bool) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.Type {
case tea.KeyEnter:
ti.Entered = true
return ti, nil, true
case tea.KeyBackspace, tea.KeyCtrlH:
if ti.Cursor > 0 && len(ti.Text) > 0 {
Expand All @@ -50,11 +52,7 @@ func (ti TextInput) Update(msg tea.Msg) (TextInput, tea.Cmd, bool) {
}

func (ti TextInput) View(questionText string) string {
if !ti.Entered {
questionText = styles.Text("? ", styles.Cyan) + questionText + "\n> "
} else {
questionText = styles.Text("✓ ", styles.Green) + questionText + "\n> "
}
questionText = styles.Text("? ", styles.Cyan) + questionText + "\n> "

var beforeCursor, cursorChar, afterCursor string
if len(ti.Text) == 0 {
Expand Down

0 comments on commit d6dc1aa

Please sign in to comment.