From cd332eb2d507533f8c38e3bbe1157a0ef2a3e3ef Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 7 Mar 2023 17:15:21 +0530 Subject: [PATCH] DRYer --- tools/cmd/ask/choices.go | 15 +++++++-------- tools/cmd/ask/get_line.go | 9 ++++----- tools/cmd/ask/main.go | 13 +++++++++---- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/tools/cmd/ask/choices.go b/tools/cmd/ask/choices.go index 0f63de9f9..741ef7df9 100644 --- a/tools/cmd/ask/choices.go +++ b/tools/cmd/ask/choices.go @@ -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 } diff --git a/tools/cmd/ask/get_line.go b/tools/cmd/ask/get_line.go index 41d4e827b..54992cf73 100644 --- a/tools/cmd/ask/get_line.go +++ b/tools/cmd/ask/get_line.go @@ -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 } diff --git a/tools/cmd/ask/main.go b/tools/cmd/ask/main.go index 2861b79d4..087c9b474 100644 --- a/tools/cmd/ask/main.go +++ b/tools/cmd/ask/main.go @@ -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 }