-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.go
More file actions
112 lines (96 loc) · 2.56 KB
/
helpers.go
File metadata and controls
112 lines (96 loc) · 2.56 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
package pretty
import (
"strings"
"unicode"
"github.com/muesli/termenv"
)
// FgColor returns a formatter that sets the foreground color.
// Example:
//
// FgColor(termenv.RGBColor("#ff0000"))
// FgColor(termenv.ANSI256Color(196))
// FgColor(termenv.ANSIColor(31))
func FgColor(c termenv.Color) Formatter {
seq := c.Sequence(false)
return CSI(seq)
}
// BgColor returns a formatter that sets the background color.
// Example:
//
// BgColor(termenv.RGBColor("#ff0000"))
// BgColor(termenv.ANSI256Color(196))
// BgColor(termenv.ANSIColor(31))
func BgColor(c termenv.Color) Formatter {
seq := c.Sequence(true)
return CSI(seq)
}
// CSI wraps the text in the given CSI (Control Sequence Introducer) sequence.
// Example:
//
// CSI(termenv.BoldSeq)
// CSI(termenv.UnderlineSeq)
// CSI(termenv.ItalicSeq)
func CSI(seq string) Formatter {
if seq == "" {
return Nop
}
return Wrap(termenv.CSI+seq+"m", termenv.CSI+termenv.ResetSeq+"m")
}
// Bold returns a formatter that makes the text bold.
func Bold() Formatter {
return CSI(termenv.BoldSeq)
}
// Italic returns a formatter that makes the text italic.
func Italic() Formatter {
return CSI(termenv.ItalicSeq)
}
// Underline returns a formatter that underlines the text.
func Underline() Formatter {
return CSI(termenv.UnderlineSeq)
}
// Wrap wraps the text in the given prefix and suffix.
// It is useful for wrapping text in ANSI sequences.
func Wrap(prefix, suffix string) Formatter {
return formatterFunc(func(t *Text) {
t.Prepend(prefix)
t.Append(suffix)
})
}
// XPad pads the text on the left and right.
func XPad(left, right int) Formatter {
return formatterFunc(func(t *Text) {
t.Prepend(strings.Repeat(" ", left))
t.Append(strings.Repeat(" ", right))
})
}
// LineWrap wraps the text at the given width.
// It breaks lines at word boundaries when possible. It will never break up
// a word so that URLs and other long strings present correctly.
func LineWrap(width int) Formatter {
return formatterFunc(func(t *Text) {
var col int
for at := t.Head(); at != nil; at = at.Next {
nlAt := strings.IndexByte(at.S, '\n')
if nlAt < 0 {
nlAt = len(at.S)
}
col += nlAt
overflow := (width - col) * -1
if overflow <= 0 {
continue
}
spaceAt := strings.LastIndexFunc(at.S[:nlAt-overflow+1], unicode.IsSpace)
if spaceAt < 0 {
// Never break up a word.
continue
}
next := at.Split(spaceAt)
at.S = strings.TrimRight(at.S, " \t")
next.S = strings.TrimLeft(next.S, " \t")
next.Insert("\n")
col = 0
}
})
}
// Nop is a no-op formatter.
var Nop Formatter = formatterFunc(func(t *Text) {})