This repository has been archived by the owner on Jul 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
status.go
158 lines (126 loc) · 2.91 KB
/
status.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
package terminal
import (
"context"
"fmt"
"os"
"strings"
"sync"
"time"
"github.com/fatih/color"
"github.com/hashicorp/waypoint-plugin-sdk/internal/pkg/spinner"
"github.com/morikuni/aec"
)
const (
StatusOK = "ok"
StatusError = "error"
StatusWarn = "warn"
StatusTimeout = "timeout"
StatusAbort = "abort"
)
var emojiStatus = map[string]string{
StatusOK: "\u2713",
StatusError: "❌",
StatusWarn: "⚠️",
StatusTimeout: "⌛",
}
var textStatus = map[string]string{
StatusOK: " +",
StatusError: " !",
StatusWarn: " *",
StatusTimeout: "<>",
}
var colorStatus = map[string][]aec.ANSI{
StatusOK: {aec.GreenF},
StatusError: {aec.RedF},
StatusWarn: {aec.YellowF},
}
// Status is used to provide an updating status to the user. The status
// usually has some animated element along with it such as a spinner.
type Status interface {
// Update writes a new status. This should be a single line.
Update(msg string)
// Indicate that a step has finished, confering an ok, error, or warn upon
// it's finishing state. If the status is not StatusOK, StatusError, or StatusWarn
// then the status text is written directly to the output, allowing for custom
// statuses.
Step(status, msg string)
// Close should be called when the live updating is complete. The
// status will be cleared from the line.
Close() error
}
// spinnerStatus implements Status and uses a spinner to show updates.
type spinnerStatus struct {
mu sync.Mutex
spinner *spinner.Spinner
running bool
}
var statusIcons map[string]string
const envForceEmoji = "WAYPOINT_FORCE_EMOJI"
func init() {
if os.Getenv(envForceEmoji) != "" || strings.Contains(os.Getenv("LANG"), "UTF-8") {
statusIcons = emojiStatus
} else {
statusIcons = textStatus
}
}
func newSpinnerStatus(ctx context.Context) *spinnerStatus {
return &spinnerStatus{
spinner: spinner.New(
ctx,
spinner.CharSets[11],
time.Second/6,
spinner.WithColor("bold"),
),
}
}
func (s *spinnerStatus) Update(msg string) {
s.mu.Lock()
defer s.mu.Unlock()
s.spinner.Suffix = " " + msg
if !s.running {
s.spinner.Start()
s.running = true
}
}
func (s *spinnerStatus) Step(status, msg string) {
s.mu.Lock()
defer s.mu.Unlock()
s.spinner.Stop()
s.running = false
pad := ""
statusIcon := emojiStatus[status]
if statusIcon == "" {
statusIcon = status
} else if status == StatusWarn {
pad = " "
}
fmt.Fprintf(color.Output, "%s%s %s\n", statusIcon, pad, msg)
}
func (s *spinnerStatus) Close() error {
s.mu.Lock()
defer s.mu.Unlock()
if s.running {
s.running = false
s.spinner.Suffix = ""
}
s.spinner.Stop()
return nil
}
func (s *spinnerStatus) Pause() bool {
s.mu.Lock()
defer s.mu.Unlock()
wasRunning := s.running
if s.running {
s.running = false
s.spinner.Stop()
}
return wasRunning
}
func (s *spinnerStatus) Start() {
s.mu.Lock()
defer s.mu.Unlock()
if !s.running {
s.running = true
s.spinner.Start()
}
}