Skip to content

Commit 820aa35

Browse files
committed
impv: previous response to stack
1 parent f6f6bfb commit 820aa35

File tree

7 files changed

+178
-48
lines changed

7 files changed

+178
-48
lines changed

cmd/init.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func InitiaInitCommand() *cobra.Command {
4444
}
4545
}
4646

47-
_, err := tea.NewProgram(weaveinit.NewRunL1NodeNetworkSelect(&weaveinit.RunL1NodeState{})).Run()
47+
_, err := tea.NewProgram(weaveinit.NewRunL1NodeNetworkSelect(weaveinit.NewRunL1NodeState())).Run()
4848
if err != nil {
4949
return err
5050
}

models/weaveinit/run_l1_node.go

+64-46
Large diffs are not rendered by default.

models/weaveinit/state.go

+6
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,9 @@ type RunL1NodeState struct {
2424
snapshotEndpoint string
2525
stateSyncEndpoint string
2626
}
27+
28+
func NewRunL1NodeState() *RunL1NodeState {
29+
return &RunL1NodeState{
30+
weave: types.NewWeaveState(),
31+
}
32+
}

styles/text.go

+23
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,29 @@ var (
9898
SelectorCursor string = Text("> ", Cyan)
9999
)
100100

101+
func RenderPromptWithError(text string, highlights []string, err error, status PromptStatus) string {
102+
prompt := ""
103+
switch status {
104+
case Question:
105+
prompt += QuestionMark
106+
case Completed:
107+
prompt += CorrectMark
108+
case Information:
109+
prompt += InformationMark
110+
}
111+
112+
for _, highlight := range highlights {
113+
if strings.Contains(text, highlight) {
114+
text = strings.ReplaceAll(text, highlight, BoldText(highlight, Cyan))
115+
}
116+
}
117+
118+
text = DefaultTextWithoutOverridingStyledText(text)
119+
120+
return prompt + RenderError(err) + text
121+
122+
}
123+
101124
// RenderPrompt highlights phrases in the text if they match any phrase in the highlights list
102125
func RenderPrompt(text string, highlights []string, status PromptStatus) string {
103126
prompt := ""

types/state.go

+28-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,32 @@
11
package types
22

33
type WeaveState struct {
4-
PreviousResponse string
4+
PreviousResponse []string
5+
}
6+
7+
func NewWeaveState() WeaveState {
8+
return WeaveState{
9+
PreviousResponse: make([]string, 0),
10+
}
11+
}
12+
13+
func (w *WeaveState) Render() string {
14+
render := ""
15+
for _, r := range w.PreviousResponse {
16+
render += r
17+
}
18+
return render
19+
}
20+
21+
func (w *WeaveState) PopPreviousResponse() {
22+
l := len(w.PreviousResponse)
23+
if l == 0 {
24+
return
25+
}
26+
27+
w.PreviousResponse = w.PreviousResponse[:l-1]
28+
}
29+
30+
func (w *WeaveState) PushPreviousResponse(s string) {
31+
w.PreviousResponse = append(w.PreviousResponse, s)
532
}

utils/downloader.go

+29
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type Downloader struct {
2121
url string
2222
dest string
2323
done bool
24+
err error
2425
}
2526

2627
func NewDownloader(text, url, dest string) *Downloader {
@@ -30,54 +31,78 @@ func NewDownloader(text, url, dest string) *Downloader {
3031
text: text,
3132
url: url,
3233
dest: dest,
34+
err: nil,
3335
}
3436
}
3537

38+
func (m *Downloader) GetError() error {
39+
return m.err
40+
}
41+
3642
func (m *Downloader) startDownload() tea.Cmd {
3743
return func() tea.Msg {
3844
const bufferSize = 65536
3945

46+
// Send a GET request to the URL
4047
resp, err := http.Get(m.url)
4148
if err != nil {
4249
m.done = true
50+
m.err = fmt.Errorf("failed to connect to URL: %w", err)
4351
return nil
4452
}
4553
defer resp.Body.Close()
4654

55+
// Check if the response status is not 200 OK
56+
if resp.StatusCode != http.StatusOK {
57+
m.done = true
58+
m.err = fmt.Errorf("failed to download: received status code %d", resp.StatusCode)
59+
return nil
60+
}
61+
62+
// Get the total size of the file
4763
m.total = resp.ContentLength
4864
if m.total <= 0 {
65+
// If Content-Length is not provided, we set a default value for safety
4966
m.total = 1
5067
}
5168

69+
// Create the destination file
5270
file, err := os.Create(m.dest)
5371
if err != nil {
5472
m.done = true
73+
m.err = fmt.Errorf("failed to create destination file: %w", err)
5574
return nil
5675
}
5776
defer file.Close()
5877

78+
// Prepare to download the file in chunks
5979
buffer := make([]byte, bufferSize)
6080
var totalDownloaded int64
6181
for {
6282
n, err := resp.Body.Read(buffer)
6383
if err != nil && err != io.EOF {
6484
m.done = true
85+
m.err = fmt.Errorf("error during file download: %w", err)
6586
return nil
6687
}
6788
if n == 0 {
6889
break
6990
}
7091

92+
// Write the downloaded chunk to the file
7193
if _, err := file.Write(buffer[:n]); err != nil {
7294
m.done = true
95+
m.err = fmt.Errorf("failed to write to file: %w", err)
7396
return nil
7497
}
7598

99+
// Update the progress
76100
totalDownloaded += int64(n)
77101
m.current = totalDownloaded
78102
}
79103

80104
m.done = true
105+
m.err = nil
81106
return nil
82107
}
83108
}
@@ -103,6 +128,10 @@ func (m *Downloader) Update(msg tea.Msg) (*Downloader, tea.Cmd) {
103128
}
104129

105130
func (m *Downloader) View() string {
131+
if m.err != nil {
132+
return ""
133+
}
134+
106135
if m.done {
107136
return fmt.Sprintf("%sDownload Complete!\nTotal Size: %d bytes\n", styles.CorrectMark, m.total)
108137
}

utils/text_input.go

+27
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,30 @@ func (ti TextInput) View() string {
9494
// Compose the full view string
9595
return fmt.Sprintf("\n%s %s%s%s\n\n%s%s", styles.Text(">", styles.Cyan), beforeCursor, cursorChar, afterCursor, feedback, bottomText)
9696
}
97+
98+
func (ti TextInput) ViewErr(err error) string {
99+
var beforeCursor, cursorChar, afterCursor string
100+
bottomText := styles.Text("Press Enter to submit or Ctrl+C to quit.", styles.Gray)
101+
if len(ti.Text) == 0 {
102+
return "\n" + styles.Text("> ", styles.Cyan) + styles.Text(ti.Placeholder, styles.Gray) + styles.Cursor(" ") + "\n\n" + styles.RenderError(err) + bottomText
103+
} else if ti.Cursor < len(ti.Text) {
104+
// Cursor is within the text
105+
beforeCursor = styles.Text(ti.Text[:ti.Cursor], styles.Ivory)
106+
cursorChar = styles.Cursor(ti.Text[ti.Cursor : ti.Cursor+1])
107+
afterCursor = styles.Text(ti.Text[ti.Cursor+1:], styles.Ivory)
108+
} else {
109+
// Cursor is at the end of the text
110+
beforeCursor = styles.Text(ti.Text, styles.Ivory)
111+
cursorChar = styles.Cursor(" ")
112+
}
113+
114+
feedback := ""
115+
if ti.IsEntered {
116+
if err := ti.ValidationFn(ti.Text); err != nil {
117+
feedback = styles.RenderError(err)
118+
}
119+
}
120+
121+
// Compose the full view string
122+
return fmt.Sprintf("\n%s %s%s%s\n\n%s%s%s", styles.Text(">", styles.Cyan), beforeCursor, cursorChar, afterCursor, feedback, styles.RenderError(err), bottomText)
123+
}

0 commit comments

Comments
 (0)