-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.go
73 lines (55 loc) · 1.74 KB
/
ui.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"github.com/pterm/pterm"
"github.com/vonhraban/shai/internal/openai_client"
"os"
)
func askPrompt(defaultValue string) string {
textInput := pterm.DefaultInteractiveTextInput.WithMultiLine(false)
// Show the text input and get the result
input, _ := textInput.WithDefaultValue(defaultValue).Show()
return input
}
func promptForInputInteractive(client openai_client.Client, input string) string {
// Create a multi printer. This allows multiple spinners to print simultaneously.
multi := pterm.DefaultMultiPrinter
writer := multi.NewWriter()
spinner, _ := pterm.DefaultSpinner.WithWriter(writer).Start("Loading")
multi.Start()
res, err := client.PromptCompletions(input)
if err != nil {
panic(err) // TODO! Return err and handle gracefully
}
spinner.Success("Completed")
pterm.DefaultHeader.WithWriter(writer).WithMargin(15).WithBackgroundStyle(pterm.NewStyle(pterm.BgCyan)).WithTextStyle(pterm.NewStyle(pterm.FgBlack)).Println(res)
// Stop the multi printer. This will stop printing all the spinners.
multi.Stop()
// Print a blank line for better readability.
printNewLine()
options := []string{"Run", "Change Query", "Cancel"}
selectedOption, _ := pterm.DefaultInteractiveSelect.
WithDefaultText("Run the command?").
WithDefaultOption("Yes").
WithFilter(false).
WithOptions(options).
Show()
switch selectedOption {
case "Run":
return res
case "Change Query":
input = askPrompt(input)
return promptForInputInteractive(client, input)
}
// Everything else - terminate
return ""
}
func exitWithMessage(message string) {
pterm.Warning.Println("Cancelled")
os.Exit(0)
}
func printMessagef(format string, a ...interface{}) {
pterm.Info.Printfln(format)
}
func printNewLine() {
pterm.Println()
}