This repository was archived by the owner on Apr 19, 2021. It is now read-only.
forked from buildkite/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterminal.go
145 lines (117 loc) · 2.76 KB
/
terminal.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package cli
import (
"bufio"
"errors"
"fmt"
"os"
"strings"
"time"
"github.com/ahmetb/go-cursor"
"github.com/briandowns/spinner"
"github.com/fatih/color"
sshterminal "golang.org/x/crypto/ssh/terminal"
)
var (
headerColorFunc = color.New(color.Bold, color.FgGreen).SprintfFunc()
)
type Terminal struct {
}
func (t *Terminal) Header(h string) {
fmt.Printf(headerColorFunc("──── " + h + "\n\n"))
}
func (t *Terminal) WaitForKeyPress(prompt string) {
fmt.Fprintf(os.Stderr, "%s", prompt)
var input string
fmt.Scanln(&input)
}
func (t *Terminal) ReadPassword(prompt string) (string, error) {
fmt.Fprintf(os.Stderr, "%s: ", prompt)
b, err := sshterminal.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
return "", err
}
return string(b), nil
}
func (t *Terminal) ReadString(prompt string) (string, error) {
fmt.Fprintf(os.Stderr, "%s: ", prompt)
reader := bufio.NewReader(os.Stdin)
text, err := reader.ReadString('\n')
if err != nil {
return "", err
}
return strings.TrimSpace(text), nil
}
func (t *Terminal) Println(s ...interface{}) {
fmt.Println(s...)
}
func (t *Terminal) Printf(s string, v ...interface{}) {
fmt.Printf(s, v...)
}
func (t *Terminal) Failure(msg string) {
t.Printf(color.RedString("%s ❌\n"), msg)
}
type Spinner interface {
Start()
Stop()
}
func (t *Terminal) Spinner() Spinner {
return spinner.New(spinner.CharSets[14], 100*time.Millisecond)
}
type Tryer interface {
Start(msg string)
Println(msg string)
Success(msg string)
Failure(msg string)
}
func (t *Terminal) Try() Tryer {
return &tryPrompt{terminal: t, spinner: t.Spinner()}
}
type tryPrompt struct {
terminal *Terminal
message string
spinner Spinner
}
func (t *tryPrompt) Start(msg string) {
t.message = msg
t.terminal.Printf(color.WhiteString("%s: ", t.message))
t.spinner.Start()
}
func (t *tryPrompt) Println(msg string) {
t.spinner.Stop()
fmt.Printf("\r")
fmt.Printf(cursor.ClearEntireLine())
t.terminal.Println(msg)
t.terminal.Printf(color.WhiteString("%s: ", t.message))
t.spinner.Start()
}
func (t *tryPrompt) Success(msg string) {
t.spinner.Stop()
cursor.MoveLeft(2)
cursor.ClearLineRight()
t.terminal.Printf(color.GreenString("%s ✅\n"), msg)
}
func (t *tryPrompt) Failure(msg string) {
t.spinner.Stop()
cursor.MoveLeft(2)
cursor.ClearLineRight()
t.terminal.Printf(color.RedString("%s ❌\n"), msg)
}
type ExitCoder interface {
ExitCode() int
}
type ExitError struct {
err error
exitCode int
}
func NewExitError(err error, exitCode int) ExitError {
return ExitError{err: err, exitCode: exitCode}
}
func NewExitErrorString(msg string, exitCode int) ExitError {
return NewExitError(errors.New(msg), exitCode)
}
func (ex ExitError) ExitCode() int {
return ex.exitCode
}
func (ex ExitError) Error() string {
return ex.err.Error()
}