This repository has been archived by the owner on Feb 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coloredtext.go
83 lines (66 loc) · 1.79 KB
/
coloredtext.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
package color
import (
"errors"
"fmt"
)
var (
ErrWrongColorNumber = errors.New("Wrong number of colors")
)
// ColoredText is a section of text that is colored with mIRC color codes.
type ColoredText struct {
Foreground Color `json:"foreground"`
Background Color `json:"background"`
Content string `json:"content"`
}
// IRCString returns a ColoredText as it should be shown on IRC.
func (c ColoredText) IRCString() string {
if c.Background != None {
return fmt.Sprintf("%c%d,%d%s%c", ColorCodeDelim, c.Foreground, c.Background, c.Content, ColorCodeDelim)
} else if c.Foreground != None {
return fmt.Sprintf("%c%d%s%c", ColorCodeDelim, c.Foreground, c.Content, ColorCodeDelim)
}
return c.Content
}
// String returns a ColoredText as a string.
func (c ColoredText) String() string {
ret := "<ColoredText "
if c.Foreground != None {
ret = ret + fmt.Sprintf("Foreground: %s ", c.Foreground.String())
}
if c.Background != None {
ret = ret + fmt.Sprintf("Foreground: %s ", c.Foreground.String())
}
ret = ret + fmt.Sprintf(`Content: "%s"`, c.Content)
return ret
}
// NewColoredText makes a ColoredText with given colors and a content or returns
// a blank ColoredText and an error.
func NewColoredText(colors []Color, content string) (c ColoredText, err error) {
switch len(colors) {
case 0:
c.Background = None
c.Foreground = None
c.Content = content
case 1:
c.Background = None
if colors[0].Validate() {
c.Foreground = colors[0]
} else {
return ColoredText{}, ErrBadColor
}
case 2:
if colors[0].Validate() {
c.Foreground = colors[0]
} else {
return ColoredText{}, ErrBadColor
}
if colors[1].Validate() {
c.Background = colors[1]
} else {
return ColoredText{}, ErrBadColor
}
default:
return ColoredText{}, ErrWrongColorNumber
}
return
}