This commit is contained in:
Kovid Goyal 2023-03-07 17:15:21 +05:30
parent f157882856
commit cd332eb2d5
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 20 additions and 17 deletions

View File

@ -61,11 +61,11 @@ func extra_for(width, screen_width int) int {
return utils.Max(0, screen_width-width)/2 + 1
}
func choices(o *Options, items []string) (ans map[string]any, err error) {
response := ""
func choices(o *Options) (response string, err error) {
response = ""
lp, err := loop.New()
if err != nil {
return nil, err
return "", err
}
lp.MouseTrackingMode(loop.BUTTONS_ONLY_MOUSE_TRACKING)
@ -114,7 +114,7 @@ func choices(o *Options, items []string) (ans map[string]any, err error) {
if hidden_text_start_pos > -1 {
raw, err := io.ReadAll(os.Stdin)
if err != nil {
return nil, fmt.Errorf("Failed to read hidden text from STDIN: %w", err)
return "", fmt.Errorf("Failed to read hidden text from STDIN: %w", err)
}
hidden_text = strings.TrimRightFunc(utils.UnsafeBytesToString(raw), unicode.IsSpace)
hidden_text_end_pos = hidden_text_start_pos + len(replacement_text)
@ -422,14 +422,13 @@ func choices(o *Options, items []string) (ans map[string]any, err error) {
err = lp.Run()
if err != nil {
return nil, err
return "", err
}
ds := lp.DeathSignalName()
if ds != "" {
fmt.Println("Killed by signal: ", ds)
lp.KillIfSignalled()
return nil, fmt.Errorf("Filled by signal: %s", ds)
return "", fmt.Errorf("Filled by signal: %s", ds)
}
ans = map[string]any{"items": items, "response": response}
return
return response, nil
}

View File

@ -16,13 +16,12 @@ import (
var _ = fmt.Print
func get_line(o *Options, items []string) (result map[string]any, err error) {
func get_line(o *Options) (result string, err error) {
lp, err := loop.New(loop.NoAlternateScreen, loop.NoRestoreColors)
if err != nil {
return
}
cwd, _ := os.Getwd()
result = map[string]any{"items": items, "response": ""}
ropts := readline.RlInit{Prompt: o.Prompt}
if o.Name != "" {
base := filepath.Join(utils.CacheDir(), "ask")
@ -59,7 +58,7 @@ func get_line(o *Options, items []string) (result map[string]any, err error) {
if err == readline.ErrAcceptInput {
hi := readline.HistoryItem{Timestamp: time.Now(), Cmd: rl.AllText(), ExitCode: 0, Cwd: cwd}
rl.AddHistoryItem(hi)
result["response"] = rl.AllText()
result = rl.AllText()
lp.Quit(0)
return nil
}
@ -83,11 +82,11 @@ func get_line(o *Options, items []string) (result map[string]any, err error) {
err = lp.Run()
rl.Shutdown()
if err != nil {
return nil, err
return "", err
}
ds := lp.DeathSignalName()
if ds != "" {
return nil, fmt.Errorf("Killed by signal: %s", ds)
return "", fmt.Errorf("Killed by signal: %s", ds)
}
return
}

View File

@ -13,6 +13,11 @@ import (
var _ = fmt.Print
type Response struct {
Items []string `json:"items"`
Response string `json:"response"`
}
func show_message(msg string) {
if msg != "" {
m := markup.New(true)
@ -22,13 +27,13 @@ func show_message(msg string) {
func main(_ *cli.Command, o *Options, args []string) (rc int, err error) {
output := tui.KittenOutputSerializer()
var result any
result := &Response{Items: args}
if len(o.Prompt) > 2 && o.Prompt[0] == o.Prompt[len(o.Prompt)-1] && (o.Prompt[0] == '"' || o.Prompt[0] == '\'') {
o.Prompt = o.Prompt[1 : len(o.Prompt)-1]
}
switch o.Type {
case "yesno", "choices":
result, err = choices(o, args)
result.Response, err = choices(o)
if err != nil {
return 1, err
}
@ -42,10 +47,10 @@ func main(_ *cli.Command, o *Options, args []string) (rc int, err error) {
return 1, err
}
}
result = map[string]any{"items": args, "response": pw}
result.Response = pw
case "line":
show_message(o.Message)
result, err = get_line(o, args)
result.Response, err = get_line(o)
if err != nil {
return 1, err
}