| 1 | |
| 2 | |
| 3 | package ui |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "io" |
| 8 | "os" |
| 9 | "regexp" |
| 10 | "strings" |
| 11 | |
| 12 | "golang.org/x/term" |
| 13 | ) |
| 14 | |
| 15 | |
| 16 | type UI struct { |
| 17 | Stderr io.Writer |
| 18 | |
| 19 | |
| 20 | OpenTTY func() (io.ReadCloser, error) |
| 21 | |
| 22 | StderrIsTerminal bool |
| 23 | |
| 24 | Width int |
| 25 | } |
| 26 | |
| 27 | |
| 28 | func New() *UI { |
| 29 | return &UI{Stderr: os.Stderr, StderrIsTerminal: term.IsTerminal(int(os.Stderr.Fd()))} |
| 30 | } |
| 31 | |
| 32 | func (u *UI) open() (io.ReadCloser, error) { |
| 33 | if u.OpenTTY != nil { |
| 34 | return u.OpenTTY() |
| 35 | } |
| 36 | return openTTY() |
| 37 | } |
| 38 | |
| 39 | |
| 40 | |
| 41 | func (u *UI) HaveTTY() bool { |
| 42 | f, err := u.open() |
| 43 | if err != nil { |
| 44 | return false |
| 45 | } |
| 46 | f.Close() |
| 47 | return true |
| 48 | } |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | func (u *UI) CanAsk() bool { |
| 54 | return u.HaveTTY() && u.StderrIsTerminal |
| 55 | } |
| 56 | |
| 57 | |
| 58 | |
| 59 | func (u *UI) line() string { |
| 60 | f, err := u.open() |
| 61 | if err != nil { |
| 62 | return "" |
| 63 | } |
| 64 | defer f.Close() |
| 65 | var b strings.Builder |
| 66 | var c [1]byte |
| 67 | for { |
| 68 | n, err := f.Read(c[:]) |
| 69 | if n == 1 { |
| 70 | if c[0] == '\n' { |
| 71 | break |
| 72 | } |
| 73 | b.WriteByte(c[0]) |
| 74 | } |
| 75 | if err != nil { |
| 76 | break |
| 77 | } |
| 78 | } |
| 79 | return strings.TrimSuffix(b.String(), "\r") |
| 80 | } |
| 81 | |
| 82 | |
| 83 | func (u *UI) Ask(prompt, def string) string { |
| 84 | fmt.Fprintf(u.Stderr, " %s [%s]: ", prompt, def) |
| 85 | if r := u.line(); r != "" { |
| 86 | return r |
| 87 | } |
| 88 | return def |
| 89 | } |
| 90 | |
| 91 | |
| 92 | func (u *UI) AskYN(prompt string, def bool) bool { |
| 93 | ind, d := "[y/N]", "N" |
| 94 | if def { |
| 95 | ind, d = "[Y/n]", "Y" |
| 96 | } |
| 97 | fmt.Fprintf(u.Stderr, " %s %s: ", prompt, ind) |
| 98 | r := u.line() |
| 99 | if r == "" { |
| 100 | r = d |
| 101 | } |
| 102 | return r[0] == 'y' || r[0] == 'Y' |
| 103 | } |
| 104 | |
| 105 | var positiveRe = regexp.MustCompile(`^[1-9][0-9]*$`) |
| 106 | |
| 107 | |
| 108 | |
| 109 | func (u *UI) AskInt(prompt string, def, max int) int { |
| 110 | for { |
| 111 | r := u.Ask(prompt, fmt.Sprint(def)) |
| 112 | var n int |
| 113 | if positiveRe.MatchString(r) { |
| 114 | if _, err := fmt.Sscan(r, &n); err == nil && (max == 0 || n <= max) { |
| 115 | return n |
| 116 | } |
| 117 | } |
| 118 | if max > 0 { |
| 119 | fmt.Fprintf(u.Stderr, " (a number from 1 to %d; got: %s)\n", max, r) |
| 120 | } else { |
| 121 | fmt.Fprintf(u.Stderr, " (must be a positive integer, e.g. 10; got: %s)\n", r) |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | |
| 127 | func (u *UI) AskChoice(prompt string, def int, options ...string) int { |
| 128 | for i, o := range options { |
| 129 | fmt.Fprintf(u.Stderr, " %d) %s\n", i+1, o) |
| 130 | } |
| 131 | return u.AskInt(prompt, def, len(options)) |
| 132 | } |
| 133 | |
| 134 | |
| 135 | |
| 136 | func Wrap(s string, width int) string { |
| 137 | var out []string |
| 138 | for _, l := range strings.Split(strings.TrimSuffix(s, "\n"), "\n") { |
| 139 | if strings.HasPrefix(l, " ") || len(l) <= width { |
| 140 | out = append(out, l) |
| 141 | continue |
| 142 | } |
| 143 | line := "" |
| 144 | for _, w := range strings.Fields(l) { |
| 145 | switch { |
| 146 | case line == "": |
| 147 | line = w |
| 148 | case len(line)+1+len(w) <= width: |
| 149 | line += " " + w |
| 150 | default: |
| 151 | out = append(out, line) |
| 152 | line = w |
| 153 | } |
| 154 | } |
| 155 | out = append(out, line) |
| 156 | } |
| 157 | return strings.Join(out, "\n") + "\n" |
| 158 | } |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | func (u *UI) Box(title, body string) { |
| 165 | width := u.Width |
| 166 | if width == 0 { |
| 167 | width = 72 |
| 168 | if f, err := u.open(); err == nil { |
| 169 | if fd, ok := f.(interface{ Fd() uintptr }); ok { |
| 170 | if w, _, err := term.GetSize(int(fd.Fd())); err == nil { |
| 171 | width = w |
| 172 | } |
| 173 | } |
| 174 | f.Close() |
| 175 | } |
| 176 | } |
| 177 | width = min(max(width, 30), 72) |
| 178 | rule := strings.Repeat("-", width) |
| 179 | n := max(width-len(title)-4, 1) |
| 180 | var b strings.Builder |
| 181 | fmt.Fprintf(&b, "\n+- %s %s\n|\n", title, rule[:n]) |
| 182 | for _, l := range strings.Split(strings.TrimSuffix(Wrap(body, width-2), "\n"), "\n") { |
| 183 | b.WriteString(strings.TrimRight("| "+l, " ") + "\n") |
| 184 | } |
| 185 | fmt.Fprintf(&b, "|\n+%s\n", rule[:width-1]) |
| 186 | fmt.Fprint(u.Stderr, b.String()) |
| 187 | } |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | func ResetTermModes(stdout *os.File) { |
| 195 | if term.IsTerminal(int(stdout.Fd())) { |
| 196 | fmt.Fprint(stdout, "\033[?1000l\033[?1002l\033[?1003l\033[?1006l\033[?1015l\033[?1004l\033[?2004l\033[<u\033[>4;0m\033[?25h") |
| 197 | } |
| 198 | } |