mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-18 06:25:13 +02:00
Finish porting of ask kitten to Go
This commit is contained in:
93
tools/cmd/ask/get_line.go
Normal file
93
tools/cmd/ask/get_line.go
Normal file
@@ -0,0 +1,93 @@
|
||||
// License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
|
||||
|
||||
package ask
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"kitty/tools/tui/loop"
|
||||
"kitty/tools/tui/readline"
|
||||
"kitty/tools/utils"
|
||||
)
|
||||
|
||||
var _ = fmt.Print
|
||||
|
||||
func get_line(o *Options, items []string) (result map[string]any, 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")
|
||||
ropts.HistoryPath = filepath.Join(base, o.Name+".history.json")
|
||||
os.MkdirAll(base, 0o755)
|
||||
}
|
||||
rl := readline.New(lp, ropts)
|
||||
if o.Default != "" {
|
||||
rl.SetText(o.Default)
|
||||
}
|
||||
lp.OnInitialize = func() (string, error) {
|
||||
rl.Start()
|
||||
return "", nil
|
||||
}
|
||||
lp.OnFinalize = func() string { rl.End(); return "" }
|
||||
|
||||
lp.OnResumeFromStop = func() error {
|
||||
rl.Start()
|
||||
return nil
|
||||
}
|
||||
|
||||
lp.OnResize = rl.OnResize
|
||||
|
||||
lp.OnKeyEvent = func(event *loop.KeyEvent) error {
|
||||
if event.MatchesPressOrRepeat("ctrl+c") {
|
||||
return fmt.Errorf("Canceled by user")
|
||||
}
|
||||
err := rl.OnKeyEvent(event)
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
lp.Quit(0)
|
||||
return nil
|
||||
}
|
||||
if err == readline.ErrAcceptInput {
|
||||
hi := readline.HistoryItem{Timestamp: time.Now(), Cmd: rl.AllText(), ExitCode: 0, Cwd: cwd}
|
||||
rl.AddHistoryItem(hi)
|
||||
result["response"] = rl.AllText()
|
||||
lp.Quit(0)
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
if event.Handled {
|
||||
rl.Redraw()
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
lp.OnText = func(text string, from_key_event, in_bracketed_paste bool) error {
|
||||
err := rl.OnText(text, from_key_event, in_bracketed_paste)
|
||||
if err == nil {
|
||||
rl.Redraw()
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
err = lp.Run()
|
||||
rl.Shutdown()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ds := lp.DeathSignalName()
|
||||
if ds != "" {
|
||||
return nil, fmt.Errorf("Killed by signal: %s", ds)
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -34,7 +34,7 @@ func main(_ *cli.Command, o *Options, args []string) (rc int, err error) {
|
||||
}
|
||||
case "password":
|
||||
show_message(o.Message)
|
||||
pw, err := tui.ReadPassword(o.Prompt, true)
|
||||
pw, err := tui.ReadPassword(o.Prompt, false)
|
||||
if err != nil {
|
||||
if errors.Is(err, tui.Canceled) {
|
||||
pw = ""
|
||||
@@ -43,6 +43,12 @@ func main(_ *cli.Command, o *Options, args []string) (rc int, err error) {
|
||||
}
|
||||
}
|
||||
result = map[string]any{"items": args, "response": pw}
|
||||
case "line":
|
||||
show_message(o.Message)
|
||||
result, err = get_line(o, args)
|
||||
if err != nil {
|
||||
return 1, err
|
||||
}
|
||||
default:
|
||||
return 1, fmt.Errorf("Unknown type: %s", o.Type)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user