main.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
  2. package ask
  3. import (
  4. "errors"
  5. "fmt"
  6. "kitty/tools/cli"
  7. "kitty/tools/cli/markup"
  8. "kitty/tools/tui"
  9. )
  10. var _ = fmt.Print
  11. type Response struct {
  12. Items []string `json:"items"`
  13. Response string `json:"response"`
  14. }
  15. func show_message(msg string) {
  16. if msg != "" {
  17. m := markup.New(true)
  18. fmt.Println(m.Bold(msg))
  19. }
  20. }
  21. func main(_ *cli.Command, o *Options, args []string) (rc int, err error) {
  22. output := tui.KittenOutputSerializer()
  23. result := &Response{Items: args}
  24. if len(o.Prompt) > 2 && o.Prompt[0] == o.Prompt[len(o.Prompt)-1] && (o.Prompt[0] == '"' || o.Prompt[0] == '\'') {
  25. o.Prompt = o.Prompt[1 : len(o.Prompt)-1]
  26. }
  27. switch o.Type {
  28. case "yesno", "choices":
  29. result.Response, err = GetChoices(o)
  30. if err != nil {
  31. return 1, err
  32. }
  33. case "password":
  34. show_message(o.Message)
  35. pw, err := tui.ReadPassword(o.Prompt, false)
  36. if err != nil {
  37. if errors.Is(err, tui.Canceled) {
  38. pw = ""
  39. } else {
  40. return 1, err
  41. }
  42. }
  43. result.Response = pw
  44. case "line":
  45. show_message(o.Message)
  46. result.Response, err = get_line(o)
  47. if err != nil {
  48. return 1, err
  49. }
  50. default:
  51. return 1, fmt.Errorf("Unknown type: %s", o.Type)
  52. }
  53. s, err := output(result)
  54. if err != nil {
  55. return 1, err
  56. }
  57. _, err = fmt.Println(s)
  58. if err != nil {
  59. return 1, err
  60. }
  61. return
  62. }
  63. func EntryPoint(parent *cli.Command) {
  64. create_cmd(parent, main)
  65. }