-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstyle.go
132 lines (102 loc) · 3.6 KB
/
style.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
package style
// CurrentStyler is the current selected Styler. Default to a style.NewTerm()
var CurrentStyler Styler = NewTerm()
// Styler represents any type that knows how to format texts.
type Styler interface {
// Upper changes a string case to upper case
Upper(string) string
// Lower changes a string case to lower case
Lower(string) string
// TitleCase changes all letters that begin words to their title case.
TitleCase(string) string
// Black changes a string foreground color to black
Black(string) string
// Red changes a string foreground color to red
Red(string) string
// Green changes a string foreground color to green
Green(string) string
// Yellow changes a string foreground color to yellow
Yellow(string) string
// Blue changes a string foreground color to blue
Blue(string) string
// Magenta changes a string foreground color to magenta
Magenta(string) string
// Cyan changes a string foreground color to cyan
Cyan(string) string
// White changes a string foreground color to white
White(string) string
// Inverse changes a string by inverting its fore- and back-ground
// colors
Inverse(string) string
// Bold changes a string case to bold
Bold(string) string
// Italic changes a string case to italic
Italic(string) string
// Underline changes a string to be underlined
Underline(string) string
// Crossout changes a string to be strikethrough
Crossout(string) string
// Tab increases the tabulation level (indents then wraps the provided text)
Tab() func(string) string
// Header returns text as a chapter's header.
Header(int) func(string) string
// Metadata returns formatted metadata information.
// Usual metadata are "title", "authors", "date", "mansection".
Metadata(map[string]string) string
// Paragraph returns text as a new paragraph.
Paragraph(string) string
// BulletedList returns a new bulleted-list (each list item has a leading
// bullet).
BulletedList() func(...string) string
// OrderedList returns a new ordered-list (each list item has a leading
// auto-incrementing enumerator).
OrderedList() func(...string) string
// Define returns a term definition.
Define(string, string) string
// Table draws a table.
Table(...[]string) string
// Link returns links to internal or external resources
Link(string, string) string
// Img returns links to an image
Img(string, string) string
// Escape escapes the provided text.
// Chaining Escapes with Styler formatting functions can lead to unexpected results.
Escape(string) string
}
// Chain combines several styling functions into one. Styling functions are
// executed in the provided order.
//
// Notice that chaining styling functions can be tricky, and should be used
// wisely (for example, some styling functions can alter formatting tags
// inserted by the previous chaining function voiding the result).
func Chain(fn ...func(string) string) func(string) string {
return func(src string) string {
s := src
for _, f := range fn {
s = f(s)
}
return s
}
}
// FuncMap returns a ready to use text/template.FuncMap to extend available
// template mapping by exposing some st styling functions.
func FuncMap(st Styler) map[string]interface{} {
return map[string]interface{}{
"upper": st.Upper,
"lower": st.Lower,
"titlecase": st.TitleCase,
"black": st.Black,
"red": st.Red,
"green": st.Green,
"yellow": st.Yellow,
"blue": st.Blue,
"magenta": st.Magenta,
"cyan": st.Cyan,
"white": st.White,
"inverse": st.Inverse,
"bold": st.Bold,
"italic": st.Italic,
"underline": st.Underline,
"crossout": st.Crossout,
}
}