Debian Codegraph @

plugin/tui.go

raw symbols SCIP
1 package plugin
2
3 import (
4 "errors"
5 "fmt"
6
7 "filippo.io/age/internal/term"
8 )
9
10 // NewTerminalUI returns a [ClientUI] that uses the terminal to request inputs,
11 // and the provided functions to display messages and errors.
12 //
13 // The terminal is reached directly through /dev/tty or CONIN$/CONOUT$,
14 // bypassing standard input and output, so this UI can be used even when
15 // standard input or output are redirected.
16 func NewTerminalUI(printf, warningf func(format string, v ...any)) *ClientUI {
17 return &ClientUI{
18 DisplayMessage: func(name, message string) error {
19 printf("%s plugin: %s", name, message)
20 return nil
21 },
22 RequestValue: func(name, message string, isSecret bool) (s string, err error) {
23 defer func() {
24 if err != nil {
25 warningf("could not read value for age-plugin-%s: %v", name, err)
26 }
27 }()
28 if isSecret {
29 secret, err := term.ReadSecret(message)
30 if err != nil {
31 return "", err
32 }
33 return string(secret), nil
34 } else {
35 public, err := term.ReadPublic(message)
36 if err != nil {
37 return "", err
38 }
39 return string(public), nil
40 }
41 },
42 Confirm: func(name, message, yes, no string) (choseYes bool, err error) {
43 defer func() {
44 if err != nil {
45 warningf("could not read value for age-plugin-%s: %v", name, err)
46 }
47 }()
48 if no == "" {
49 message += fmt.Sprintf(" (press enter for %q)", yes)
50 _, err := term.ReadSecret(message)
51 if err != nil {
52 return false, err
53 }
54 return true, nil
55 }
56 message += fmt.Sprintf(" (press [1] for %q or [2] for %q)", yes, no)
57 for {
58 selection, err := term.ReadCharacter(message)
59 if err != nil {
60 return false, err
61 }
62 switch selection {
63 case '1':
64 return true, nil
65 case '2':
66 return false, nil
67 case '\x03': // CTRL-C
68 return false, errors.New("user cancelled prompt")
69 default:
70 warningf("reading value for age-plugin-%s: invalid selection %q", name, selection)
71 }
72 }
73 },
74 WaitTimer: func(name string) {
75 printf("waiting on %s plugin...", name)
76 },
77 }
78 }