-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoutput.go
More file actions
195 lines (173 loc) · 3.97 KB
/
output.go
File metadata and controls
195 lines (173 loc) · 3.97 KB
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
187
188
189
190
191
192
193
194
195
package main
import (
"flag"
"fmt"
"os"
"strings"
"time"
)
// ANSI color codes
const (
cReset = "\033[0m"
cBold = "\033[1m"
cDim = "\033[2m"
cRed = "\033[31m"
cGreen = "\033[32m"
cYellow = "\033[33m"
cCyan = "\033[36m"
)
var useColor bool
func initColor() {
if os.Getenv("NO_COLOR") != "" {
useColor = false
return
}
if flagNoColor {
useColor = false
return
}
fi, err := os.Stdout.Stat()
if err != nil {
useColor = false
return
}
useColor = fi.Mode()&os.ModeCharDevice != 0
}
// c wraps text in a color code if colors are enabled.
func c(color, text string) string {
if !useColor {
return text
}
return color + text + cReset
}
const fieldWidth = 14
// printField prints a labeled field with aligned values.
func printField(label, value string) {
if useColor {
fmt.Printf(" %s%-*s%s %s\n", cDim, fieldWidth, label, cReset, value)
} else {
fmt.Printf(" %-*s %s\n", fieldWidth, label, value)
}
}
// printHeader prints a section header.
func printHeader(text string) {
if useColor {
fmt.Printf("\n%s%s%s\n", cBold, text, cReset)
} else {
fmt.Printf("\n%s\n", text)
}
}
// printOK prints a success message.
func printOK(msg string) {
if useColor {
fmt.Printf(" %s✓%s %s\n", cGreen, cReset, msg)
} else {
fmt.Printf(" OK: %s\n", msg)
}
}
// printFail prints a failure message.
func printFail(msg string) {
if useColor {
fmt.Printf(" %s✗%s %s\n", cRed, cReset, msg)
} else {
fmt.Printf(" FAIL: %s\n", msg)
}
}
// printWarn prints a warning message.
func printWarn(msg string) {
if useColor {
fmt.Printf(" %s!%s %s\n", cYellow, cReset, msg)
} else {
fmt.Printf(" WARN: %s\n", msg)
}
}
// fatal prints an error and exits.
func fatal(format string, args ...interface{}) {
msg := fmt.Sprintf(format, args...)
if useColor {
fmt.Fprintf(os.Stderr, "%serror:%s %s\n", cRed, cReset, msg)
} else {
fmt.Fprintf(os.Stderr, "error: %s\n", msg)
}
os.Exit(1)
}
// printTable prints rows with aligned columns.
func printTable(headers []string, rows [][]string) {
if len(rows) == 0 {
return
}
// Compute column widths
widths := make([]int, len(headers))
for i, h := range headers {
widths[i] = len(h)
}
for _, row := range rows {
for i, cell := range row {
if i < len(widths) && len(cell) > widths[i] {
widths[i] = len(cell)
}
}
}
// Print header
if useColor {
fmt.Print(" ")
for i, h := range headers {
fmt.Printf("%s%-*s%s", cDim, widths[i]+2, h, cReset)
}
fmt.Println()
} else {
fmt.Print(" ")
for i, h := range headers {
fmt.Printf("%-*s", widths[i]+2, h)
}
fmt.Println()
}
// Print separator
fmt.Print(" ")
for i := range headers {
fmt.Print(strings.Repeat("-", widths[i]))
fmt.Print(" ")
}
fmt.Println()
// Print rows
for _, row := range rows {
fmt.Print(" ")
for i, cell := range row {
if i < len(widths) {
fmt.Printf("%-*s", widths[i]+2, cell)
}
}
fmt.Println()
}
}
// addWatchFlags registers --watch/-w and --interval flags on a FlagSet.
func addWatchFlags(fs *flag.FlagSet) (*bool, *int) {
watch := fs.Bool("watch", false, "auto-refresh output")
fs.BoolVar(watch, "w", false, "alias for --watch")
interval := fs.Int("interval", 2, "refresh interval in seconds")
fs.IntVar(interval, "i", 2, "alias for --interval")
return watch, interval
}
// watchLoop clears the screen and calls displayFn repeatedly until interrupted.
// If watch is false, calls displayFn once and returns its error.
func watchLoop(watch bool, intervalSec int, displayFn func() error) error {
if !watch {
return displayFn()
}
sigCh := make(chan os.Signal, 1)
signalNotify(sigCh)
for {
// Clear screen and move cursor to top-left
fmt.Print("\033[2J\033[H")
if err := displayFn(); err != nil {
fmt.Fprintf(os.Stderr, "watch error: %v\n", err)
}
fmt.Printf("\n%s", c(cDim, fmt.Sprintf(" refreshing every %ds — Ctrl-C to exit", intervalSec)))
select {
case <-sigCh:
fmt.Println()
return nil
case <-time.After(time.Duration(intervalSec) * time.Second):
}
}
}