-
Notifications
You must be signed in to change notification settings - Fork 9
/
char.go
54 lines (44 loc) · 1.08 KB
/
char.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
package figlet4go
import (
"errors"
"strings"
)
// Represents a single ascii character
type asciiChar struct {
// Slice with the lines of the Char
Lines []string
// Color of the char
Color Color
}
// Creates a new ascii character
func newAsciiChar(font *font, char rune) (*asciiChar, error) {
// If not ascii, throw an error
if char < 0 || char > 127 {
return nil, errors.New("Not Ascii")
}
// Get the font's representation of the char
lines := font.getCharSlice(char)
return &asciiChar{Lines: lines}, nil
}
// Return a line of the char as string with color if set
func (char *asciiChar) GetLine(index int, p Parser) string {
prefix := ""
suffix := ""
line := handleReplaces(char.Lines[index], p)
if char.Color != nil {
prefix = char.Color.getPrefix(p)
suffix = char.Color.getSuffix(p)
}
return prefix + line + suffix
}
// Replace all parser specific things
func handleReplaces(str string, p Parser) string {
if p.Replaces == nil {
return str
}
// Replace for each entry
for old, new := range p.Replaces {
str = strings.Replace(str, old, new, -1)
}
return str
}