Debian Codegraph @

internal/term/term.go

raw symbols SCIP
1 package term
2
3 import (
4 "fmt"
5 "os"
6 "runtime"
7 "strings"
8 "unicode"
9
10 "golang.org/x/term"
11 )
12
13 // enableVirtualTerminalProcessing tries to enable virtual terminal processing
14 // on Windows. If it fails, avoid using escape sequences to prevent weird
15 // characters being printed to the console.
16 var enableVirtualTerminalProcessing func(out *os.File) error
17
18 func printPrompt(out *os.File, prompt string) {
19 fmt.Fprintf(out, "%s ", strings.Map(func(r rune) rune {
20 if unicode.IsControl(r) {
21 return '\uFFFD'
22 }
23 return r
24 }, prompt))
25 }
26
27 // clearLine clears the current line on the terminal, or opens a new line if
28 // terminal escape codes don't work.
29 func clearLine(out *os.File) {
30 const (
31 CUI = "\033[" // Control Sequence Introducer
32 CPL = CUI + "F" // Cursor Previous Line
33 EL = CUI + "K" // Erase in Line
34 )
35
36 // First, open a new line, which is guaranteed to work everywhere. Then, try
37 // to erase the line above with escape codes, if possible.
38 //
39 // (We use CRLF instead of LF to work around an apparent bug in WSL2's
40 // handling of CONOUT$. Only when running a Windows binary from WSL2, the
41 // cursor would not go back to the start of the line with a simple LF.
42 // Honestly, it's impressive CONIN$ and CONOUT$ work at all inside WSL2.)
43 fmt.Fprintf(out, "\r\n")
44 if enableVirtualTerminalProcessing == nil || enableVirtualTerminalProcessing(out) == nil {
45 fmt.Fprintf(out, CPL+EL)
46 }
47 }
48
49 // WithTerminal runs f with the terminal input and output files, if available.
50 // WithTerminal does not open a non-terminal stdin, so the caller does not need
51 // to check if stdin is in use.
52 func WithTerminal(f func(in, out *os.File) error) error {
53 if runtime.GOOS == "windows" {
54 in, err := os.OpenFile("CONIN$", os.O_RDWR, 0)
55 if err != nil {
56 return err
57 }
58 defer in.Close()
59 out, err := os.OpenFile("CONOUT$", os.O_WRONLY, 0)
60 if err != nil {
61 return err
62 }
63 defer out.Close()
64 return f(in, out)
65 } else if tty, err := os.OpenFile("/dev/tty", os.O_RDWR, 0); err == nil {
66 defer tty.Close()
67 return f(tty, tty)
68 } else if term.IsTerminal(int(os.Stdin.Fd())) {
69 return f(os.Stdin, os.Stdin)
70 } else {
71 return fmt.Errorf("standard input is not a terminal, and /dev/tty is not available: %v", err)
72 }
73 }
74
75 // ReadSecret reads a value from the terminal with no echo. The prompt is ephemeral.
76 func ReadSecret(prompt string) (s []byte, err error) {
77 err = WithTerminal(func(in, out *os.File) error {
78 printPrompt(out, prompt)
79 defer clearLine(out)
80 s, err = term.ReadPassword(int(in.Fd()))
81 return err
82 })
83 return
84 }
85
86 // ReadPublic reads a value from the terminal. The prompt is ephemeral.
87 func ReadPublic(prompt string) (s []byte, err error) {
88 err = WithTerminal(func(in, out *os.File) error {
89 printPrompt(out, prompt)
90 defer clearLine(out)
91
92 oldState, err := term.MakeRaw(int(in.Fd()))
93 if err != nil {
94 return err
95 }
96 defer term.Restore(int(in.Fd()), oldState)
97
98 t := term.NewTerminal(in, "")
99 line, err := t.ReadLine()
100 s = []byte(line)
101 return err
102 })
103 return
104 }
105
106 // ReadCharacter reads a single character from the terminal with no echo. The
107 // prompt is ephemeral.
108 func ReadCharacter(prompt string) (c byte, err error) {
109 err = WithTerminal(func(in, out *os.File) error {
110 printPrompt(out, prompt)
111 defer clearLine(out)
112
113 oldState, err := term.MakeRaw(int(in.Fd()))
114 if err != nil {
115 return err
116 }
117 defer term.Restore(int(in.Fd()), oldState)
118
119 b := make([]byte, 1)
120 if _, err := in.Read(b); err != nil {
121 return err
122 }
123
124 c = b[0]
125 return nil
126 })
127 return
128 }
129
130 // IsTerminal returns whether the given file is a terminal.
131 func IsTerminal(f *os.File) bool {
132 return term.IsTerminal(int(f.Fd()))
133 }