-
Notifications
You must be signed in to change notification settings - Fork 316
/
basic.go
186 lines (163 loc) · 4.45 KB
/
basic.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package terminal
import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"os"
"strings"
"text/tabwriter"
"github.com/bgentry/speakeasy"
"github.com/fatih/color"
"github.com/mattn/go-isatty"
)
// basicUI is a standard implementation of the UI interface for the terminal.
type basicUI struct {
ctx context.Context
bufOut io.Writer
}
// NewBasicUI creates a new instance of the basicUI struct for the terminal
// with color output.
func NewBasicUI(ctx context.Context) *basicUI {
return NewUI(ctx, color.Output)
}
// NewUI creates a new instance of the basicUI struct that outputs to
// the bufOut buffer.
func NewUI(ctx context.Context, bufOut io.Writer) *basicUI {
return &basicUI{ctx: ctx, bufOut: bufOut}
}
// Input implements UI.
func (ui *basicUI) Input(input *Input) (string, error) {
var buf bytes.Buffer
// Write the prompt, add a space.
ui.Output(input.Prompt, WithStyle(input.Style), WithWriter(&buf))
fmt.Fprint(ui.bufOut, strings.TrimRight(buf.String(), "\r\n"))
fmt.Fprint(ui.bufOut, " ")
// Ask for input in a go-routine so that we can ignore it.
errCh := make(chan error, 1)
lineCh := make(chan string, 1)
go func() {
var line string
var err error
if input.Secret && isatty.IsTerminal(os.Stdin.Fd()) {
line, err = speakeasy.Ask("")
} else {
r := bufio.NewReader(os.Stdin)
line, err = r.ReadString('\n')
}
if err != nil {
errCh <- err
return
}
lineCh <- strings.TrimRight(line, "\r\n")
}()
select {
case err := <-errCh:
return "", err
case line := <-lineCh:
return line, nil
case <-ui.ctx.Done():
// Print newline so that any further output starts properly
fmt.Fprintln(ui.bufOut)
return "", ui.ctx.Err()
}
}
// Interactive implements UI.
func (ui *basicUI) Interactive() bool {
return isatty.IsTerminal(os.Stdin.Fd())
}
// Output prints the given message using the formatting options passed in.
func (ui *basicUI) Output(msg string, raw ...interface{}) {
msg, style, w := ui.parse(msg, raw...)
switch style {
case HeaderStyle:
msg = colorHeader.Sprintf("\n==> %s", msg)
case ErrorStyle:
msg = colorError.Sprintf(" ! %s", msg)
case ErrorBoldStyle:
msg = colorErrorBold.Sprintf(" ! %s", msg)
case WarningStyle:
msg = colorWarning.Sprintf(" * %s", msg)
case WarningBoldStyle:
msg = colorWarningBold.Sprintf(" * %s", msg)
case SuccessStyle:
msg = colorSuccess.Sprintf(" ✓ %s", msg)
case SuccessBoldStyle:
msg = colorSuccessBold.Sprintf(" ✓ %s", msg)
case LibraryStyle:
msg = colorLibrary.Sprintf(" --> %s", msg)
case DiffUnchangedStyle:
msg = colorDiffUnchanged.Sprintf(" %s", msg)
case DiffAddedStyle:
msg = colorDiffAdded.Sprintf(" %s", msg)
case DiffRemovedStyle:
msg = colorDiffRemoved.Sprintf(" %s", msg)
case InfoStyle:
lines := strings.Split(msg, "\n")
for i, line := range lines {
lines[i] = colorInfo.Sprintf(" %s", line)
}
msg = strings.Join(lines, "\n")
}
fmt.Fprintln(w, msg)
}
// NamedValues implements UI.
func (ui *basicUI) NamedValues(rows []NamedValue, opts ...Option) {
cfg := &config{
Writer: ui.bufOut,
Style: "",
}
for _, opt := range opts {
opt(cfg)
}
var buf bytes.Buffer
tr := tabwriter.NewWriter(&buf, 1, 8, 0, ' ', tabwriter.AlignRight)
for _, row := range rows {
switch v := row.Value.(type) {
case int, uint, int8, uint8, int16, uint16, int32, uint32, int64, uint64:
fmt.Fprintf(tr, " %s: \t%d\n", row.Name, row.Value)
case float32, float64:
fmt.Fprintf(tr, " %s: \t%f\n", row.Name, row.Value)
case bool:
fmt.Fprintf(tr, " %s: \t%v\n", row.Name, row.Value)
case string:
if v == "" {
continue
}
fmt.Fprintf(tr, " %s: \t%s\n", row.Name, row.Value)
default:
fmt.Fprintf(tr, " %s: \t%s\n", row.Name, row.Value)
}
}
_ = tr.Flush()
_, _ = colorInfo.Fprintln(cfg.Writer, buf.String())
}
// OutputWriters implements UI.
func (ui *basicUI) OutputWriters() (io.Writer, io.Writer, error) {
return os.Stdout, os.Stderr, nil
}
// parse decomposes the msg and arguments into the message, style, and writer.
func (ui *basicUI) parse(msg string, raw ...interface{}) (string, string, io.Writer) {
// Build our args and options
var args []interface{}
var opts []Option
for _, r := range raw {
if opt, ok := r.(Option); ok {
opts = append(opts, opt)
} else {
args = append(args, r)
}
}
// Build our message
msg = fmt.Sprintf(msg, args...)
// Build our config and set our options
cfg := &config{
Writer: ui.bufOut,
Style: "",
}
for _, opt := range opts {
opt(cfg)
}
return msg, cfg.Style, cfg.Writer
}