DRYer
This commit is contained in:
parent
f157882856
commit
cd332eb2d5
@ -61,11 +61,11 @@ func extra_for(width, screen_width int) int {
|
|||||||
return utils.Max(0, screen_width-width)/2 + 1
|
return utils.Max(0, screen_width-width)/2 + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
func choices(o *Options, items []string) (ans map[string]any, err error) {
|
func choices(o *Options) (response string, err error) {
|
||||||
response := ""
|
response = ""
|
||||||
lp, err := loop.New()
|
lp, err := loop.New()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return "", err
|
||||||
}
|
}
|
||||||
lp.MouseTrackingMode(loop.BUTTONS_ONLY_MOUSE_TRACKING)
|
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 {
|
if hidden_text_start_pos > -1 {
|
||||||
raw, err := io.ReadAll(os.Stdin)
|
raw, err := io.ReadAll(os.Stdin)
|
||||||
if err != nil {
|
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 = strings.TrimRightFunc(utils.UnsafeBytesToString(raw), unicode.IsSpace)
|
||||||
hidden_text_end_pos = hidden_text_start_pos + len(replacement_text)
|
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()
|
err = lp.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return "", err
|
||||||
}
|
}
|
||||||
ds := lp.DeathSignalName()
|
ds := lp.DeathSignalName()
|
||||||
if ds != "" {
|
if ds != "" {
|
||||||
fmt.Println("Killed by signal: ", ds)
|
fmt.Println("Killed by signal: ", ds)
|
||||||
lp.KillIfSignalled()
|
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 response, nil
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -16,13 +16,12 @@ import (
|
|||||||
|
|
||||||
var _ = fmt.Print
|
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)
|
lp, err := loop.New(loop.NoAlternateScreen, loop.NoRestoreColors)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
cwd, _ := os.Getwd()
|
cwd, _ := os.Getwd()
|
||||||
result = map[string]any{"items": items, "response": ""}
|
|
||||||
ropts := readline.RlInit{Prompt: o.Prompt}
|
ropts := readline.RlInit{Prompt: o.Prompt}
|
||||||
if o.Name != "" {
|
if o.Name != "" {
|
||||||
base := filepath.Join(utils.CacheDir(), "ask")
|
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 {
|
if err == readline.ErrAcceptInput {
|
||||||
hi := readline.HistoryItem{Timestamp: time.Now(), Cmd: rl.AllText(), ExitCode: 0, Cwd: cwd}
|
hi := readline.HistoryItem{Timestamp: time.Now(), Cmd: rl.AllText(), ExitCode: 0, Cwd: cwd}
|
||||||
rl.AddHistoryItem(hi)
|
rl.AddHistoryItem(hi)
|
||||||
result["response"] = rl.AllText()
|
result = rl.AllText()
|
||||||
lp.Quit(0)
|
lp.Quit(0)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -83,11 +82,11 @@ func get_line(o *Options, items []string) (result map[string]any, err error) {
|
|||||||
err = lp.Run()
|
err = lp.Run()
|
||||||
rl.Shutdown()
|
rl.Shutdown()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return "", err
|
||||||
}
|
}
|
||||||
ds := lp.DeathSignalName()
|
ds := lp.DeathSignalName()
|
||||||
if ds != "" {
|
if ds != "" {
|
||||||
return nil, fmt.Errorf("Killed by signal: %s", ds)
|
return "", fmt.Errorf("Killed by signal: %s", ds)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@ -13,6 +13,11 @@ import (
|
|||||||
|
|
||||||
var _ = fmt.Print
|
var _ = fmt.Print
|
||||||
|
|
||||||
|
type Response struct {
|
||||||
|
Items []string `json:"items"`
|
||||||
|
Response string `json:"response"`
|
||||||
|
}
|
||||||
|
|
||||||
func show_message(msg string) {
|
func show_message(msg string) {
|
||||||
if msg != "" {
|
if msg != "" {
|
||||||
m := markup.New(true)
|
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) {
|
func main(_ *cli.Command, o *Options, args []string) (rc int, err error) {
|
||||||
output := tui.KittenOutputSerializer()
|
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] == '\'') {
|
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]
|
o.Prompt = o.Prompt[1 : len(o.Prompt)-1]
|
||||||
}
|
}
|
||||||
switch o.Type {
|
switch o.Type {
|
||||||
case "yesno", "choices":
|
case "yesno", "choices":
|
||||||
result, err = choices(o, args)
|
result.Response, err = choices(o)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 1, err
|
return 1, err
|
||||||
}
|
}
|
||||||
@ -42,10 +47,10 @@ func main(_ *cli.Command, o *Options, args []string) (rc int, err error) {
|
|||||||
return 1, err
|
return 1, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result = map[string]any{"items": args, "response": pw}
|
result.Response = pw
|
||||||
case "line":
|
case "line":
|
||||||
show_message(o.Message)
|
show_message(o.Message)
|
||||||
result, err = get_line(o, args)
|
result.Response, err = get_line(o)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 1, err
|
return 1, err
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user