-
-
Notifications
You must be signed in to change notification settings - Fork 398
/
Copy pathtext.go
138 lines (117 loc) · 2.91 KB
/
text.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
/*
* This file is part of arduino-cli.
*
* Copyright 2018 ARDUINO SA (http://www.arduino.cc/)
*
* This software is released under the GNU General Public License version 3,
* which covers the main part of arduino-cli.
* The terms of this license can be found at:
* https://www.gnu.org/licenses/gpl-3.0.en.html
*
* You can be released from the requirements of the above licenses by purchasing
* a commercial license. Buying such a license is mandatory if you want to modify or
* otherwise use the software for commercial activities involving the Arduino
* software without disclosing the source code of your own applications. To purchase
* a commercial license, send an email to [email protected].
*/
package output
import (
"fmt"
"unicode/utf8"
"github.com/fatih/color"
)
var red = color.New(color.FgRed).SprintfFunc()
var blue = color.New(color.FgBlue).SprintfFunc()
var green = color.New(color.FgGreen).SprintfFunc()
var yellow = color.New(color.FgYellow).SprintfFunc()
var white = color.New(color.FgWhite).SprintfFunc()
var hiWhite = color.New(color.FgHiWhite).SprintfFunc()
// Red FIXMEDOC
func Red(in string) *Text {
return &Text{raw: red(in), clean: in}
}
// Blue FIXMEDOC
func Blue(in string) *Text {
return &Text{raw: blue(in), clean: in}
}
// Green FIXMEDOC
func Green(in string) *Text {
return &Text{raw: green(in), clean: in}
}
// Yellow FIXMEDOC
func Yellow(in string) *Text {
return &Text{raw: yellow(in), clean: in}
}
// White FIXMEDOC
func White(in string) *Text {
return &Text{raw: white(in), clean: in}
}
// HiWhite FIXMEDOC
func HiWhite(in string) *Text {
return &Text{raw: hiWhite(in), clean: in}
}
// TextBox FIXMEDOC
type TextBox interface {
Len() int
Pad(availableWidth int) string
}
// Text FIXMEDOC
type Text struct {
clean string
raw string
justify int
}
// Len FIXMEDOC
func (t *Text) Len() int {
return utf8.RuneCountInString(t.clean)
}
// func (t *Text) String() string {
// return t.raw
// }
// JustifyLeft FIXMEDOC
func (t *Text) JustifyLeft() {
t.justify = 0
}
// JustifyCenter FIXMEDOC
func (t *Text) JustifyCenter() {
t.justify = 1
}
// JustifyRight FIXMEDOC
func (t *Text) JustifyRight() {
t.justify = 2
}
// Pad FIXMEDOC
func (t *Text) Pad(totalLen int) string {
delta := totalLen - t.Len()
switch t.justify {
case 0:
return t.raw + spaces(delta)
case 1:
return spaces(delta/2) + t.raw + spaces(delta-delta/2)
case 2:
return spaces(delta) + t.raw
}
panic(fmt.Sprintf("internal error: invalid justify %d", t.justify))
}
func spaces(n int) string {
res := ""
for n > 0 {
res += " "
n--
}
return res
}
func sprintf(format string, args ...interface{}) TextBox {
cleanArgs := make([]interface{}, len(args))
for i, arg := range args {
if text, ok := arg.(*Text); ok {
cleanArgs[i], args[i] = text.clean, text.raw
} else {
cleanArgs[i] = args[i]
}
}
return &Text{
clean: fmt.Sprintf(format, cleanArgs...),
raw: fmt.Sprintf(format, args...),
}
}