-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrender.go
110 lines (91 loc) · 2.69 KB
/
render.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
package figlet4go
// RenderOptions are used to set color or maybe future
// options to the AsciiRenderer
type RenderOptions struct {
// Name of the used font
FontName string
// Colors of the font
FontColor []Color
// Parser
Parser Parser
}
// NewRenderOptions creates new RenderOptions
// Sets the default font name
func NewRenderOptions() *RenderOptions {
p, _ := GetParser("terminal")
return &RenderOptions{
FontName: defaultFont,
Parser: *p,
}
}
// AsciiRender is the wrapper to render a string
type AsciiRender struct {
// FontManager to store all the fonts
fontMgr *fontManager
}
// NewAsciiRender creates a new AsciiRender
func NewAsciiRender() *AsciiRender {
return &AsciiRender{
fontMgr: newFontManager(),
}
}
// LoadFont loads all *.flf font files recursively in a path
func (ar *AsciiRender) LoadFont(fontPath string) error {
return ar.fontMgr.loadFontList(fontPath)
}
// LoadBinDataFont loads provided font binary
func (ar *AsciiRender) LoadBindataFont(fontBinary []byte, fontName string) error {
return ar.fontMgr.loadBindataFont(fontBinary, fontName)
}
// Render renders a string with the default options
// Calls the RenderOpts method with a new RenderOptions object
func (ar *AsciiRender) Render(str string) (string, error) {
return ar.RenderOpts(str, NewRenderOptions())
}
// RenderOpts renders a string with special RenderOptions
// Can be called from the user (if options wished) or the above Render method
// Contains the whole rendering logic
func (ar *AsciiRender) RenderOpts(str string, opt *RenderOptions) (string, error) {
// Should the text be colored
colored := len(opt.FontColor) > 0
// Load the font
font := ar.fontMgr.getFont(opt.FontName)
// Slice holding the chars
chars := []*asciiChar{}
// Index of the current color
curColorIndex := 0
// Foreach char create the ascii char
for _, char := range str {
// AsciiChar
asciiChar, err := newAsciiChar(font, char)
if err != nil {
return "", err
}
// Set color if given
if colored {
// Start colors from beginning if length is reached
if curColorIndex == len(opt.FontColor) {
curColorIndex = 0
}
// Assign color and increment the index
asciiChar.Color = opt.FontColor[curColorIndex]
curColorIndex++
}
// Append the char to the chars slice
chars = append(chars, asciiChar)
}
// Result which will be returned
result := ""
result += opt.Parser.Prefix
// Foreach line of the font height
for curLine := 0; curLine < font.height; curLine++ {
// Add the current line of the char to the result
for i := range chars {
result += chars[i].GetLine(curLine, opt.Parser)
}
// A new line at the end
result += opt.Parser.NewLine
}
result += opt.Parser.Suffix
return result, nil
}