256 color support

Expose termbox OutputMode so users can override the default.
addColorMap to extend textbuilder with custom colors
ColorRGB24 to convert from web to terminal colors
This commit is contained in:
Matt Ranney 2016-02-04 22:20:46 -08:00 committed by Caleb Bassi
parent b715a57165
commit 606e1de924
3 changed files with 41 additions and 3 deletions

View File

@ -45,6 +45,18 @@ var (
dotw = rw.StringWidth(dot) dotw = rw.StringWidth(dot)
) )
// termbox passthrough
type OutputMode int
// termbox passthrough
const (
OutputCurrent OutputMode = iota
OutputNormal
Output256
Output216
OutputGrayscale
)
/* ----------------------- End ----------------------------- */ /* ----------------------- End ----------------------------- */
func toTmAttr(x Attribute) tm.Attribute { func toTmAttr(x Attribute) tm.Attribute {
@ -220,3 +232,19 @@ func CellsToStr(cs []Cell) string {
} }
return str return str
} }
// Passthrough to termbox using termbox constants above
func SetOutputMode(mode OutputMode) {
switch mode {
case OutputCurrent:
tm.SetOutputMode(tm.OutputCurrent)
case OutputNormal:
tm.SetOutputMode(tm.OutputNormal)
case Output256:
tm.SetOutputMode(tm.Output256)
case Output216:
tm.SetOutputMode(tm.Output216)
case OutputGrayscale:
tm.SetOutputMode(tm.OutputGrayscale)
}
}

View File

@ -52,6 +52,11 @@ var attrMap = map[string]Attribute{
"reverse": AttrReverse, "reverse": AttrReverse,
} }
// Allow users to add/override the string to attribute mapping
func AddColorMap(str string, attr Attribute) {
colorMap[str] = attr
}
func rmSpc(s string) string { func rmSpc(s string) string {
reg := regexp.MustCompile(`\s+`) reg := regexp.MustCompile(`\s+`)
return reg.ReplaceAllString(s, "") return reg.ReplaceAllString(s, "")
@ -85,10 +90,10 @@ func (mtb MarkdownTxBuilder) readAttr(s string) (Attribute, Attribute) {
if len(subs) > 1 { if len(subs) > 1 {
if subs[0] == "fg" { if subs[0] == "fg" {
fgs = append(fgs, subs[1]) fgs = append(fgs, subs[1])
} } else if subs[0] == "bg" {
if subs[0] == "bg" {
bgs = append(bgs, subs[1]) bgs = append(bgs, subs[1])
} }
// else maybe error somehow?
} }
} }

View File

@ -138,3 +138,8 @@ func ColorRGB(r, g, b int) Attribute {
r, b, g = within(r), within(b), within(g) r, b, g = within(r), within(b), within(g)
return Attribute(0x0f + 36*r + 6*g + b) return Attribute(0x0f + 36*r + 6*g + b)
} }
// Convert from familiar 24 bit colors into 6 bit terminal colors
func ColorRGB24(r, g, b int) Attribute {
return ColorRGB(r/51, g/51, b/51)
}