2017-01-14 14:07:43 +08:00
|
|
|
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
|
2015-03-21 04:21:50 +08:00
|
|
|
// Use of this source code is governed by a MIT license that can
|
|
|
|
// be found in the LICENSE file.
|
|
|
|
|
2015-03-12 04:15:59 +08:00
|
|
|
package termui
|
|
|
|
|
2015-09-21 15:11:58 +08:00
|
|
|
import "strings"
|
|
|
|
|
|
|
|
/*
|
2015-03-20 17:35:09 +08:00
|
|
|
// A ColorScheme represents the current look-and-feel of the dashboard.
|
|
|
|
type ColorScheme struct {
|
2015-03-12 04:15:59 +08:00
|
|
|
BodyBg Attribute
|
|
|
|
BlockBg Attribute
|
|
|
|
HasBorder bool
|
|
|
|
BorderFg Attribute
|
|
|
|
BorderBg Attribute
|
|
|
|
BorderLabelTextFg Attribute
|
|
|
|
BorderLabelTextBg Attribute
|
|
|
|
ParTextFg Attribute
|
|
|
|
ParTextBg Attribute
|
|
|
|
SparklineLine Attribute
|
|
|
|
SparklineTitle Attribute
|
|
|
|
GaugeBar Attribute
|
|
|
|
GaugePercent Attribute
|
|
|
|
LineChartLine Attribute
|
|
|
|
LineChartAxes Attribute
|
|
|
|
ListItemFg Attribute
|
|
|
|
ListItemBg Attribute
|
|
|
|
BarChartBar Attribute
|
|
|
|
BarChartText Attribute
|
|
|
|
BarChartNum Attribute
|
2015-04-18 06:03:22 +08:00
|
|
|
MBarChartBar Attribute
|
|
|
|
MBarChartText Attribute
|
|
|
|
MBarChartNum Attribute
|
2015-05-27 03:37:04 +08:00
|
|
|
TabActiveBg Attribute
|
2015-03-12 04:15:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// default color scheme depends on the user's terminal setting.
|
2015-03-20 17:35:09 +08:00
|
|
|
var themeDefault = ColorScheme{HasBorder: true}
|
2015-03-12 04:15:59 +08:00
|
|
|
|
2015-03-20 17:35:09 +08:00
|
|
|
var themeHelloWorld = ColorScheme{
|
2015-03-12 04:15:59 +08:00
|
|
|
BodyBg: ColorBlack,
|
|
|
|
BlockBg: ColorBlack,
|
|
|
|
HasBorder: true,
|
|
|
|
BorderFg: ColorWhite,
|
|
|
|
BorderBg: ColorBlack,
|
|
|
|
BorderLabelTextBg: ColorBlack,
|
|
|
|
BorderLabelTextFg: ColorGreen,
|
|
|
|
ParTextBg: ColorBlack,
|
|
|
|
ParTextFg: ColorWhite,
|
|
|
|
SparklineLine: ColorMagenta,
|
|
|
|
SparklineTitle: ColorWhite,
|
|
|
|
GaugeBar: ColorRed,
|
|
|
|
GaugePercent: ColorWhite,
|
|
|
|
LineChartLine: ColorYellow | AttrBold,
|
|
|
|
LineChartAxes: ColorWhite,
|
|
|
|
ListItemBg: ColorBlack,
|
|
|
|
ListItemFg: ColorYellow,
|
|
|
|
BarChartBar: ColorRed,
|
|
|
|
BarChartNum: ColorWhite,
|
|
|
|
BarChartText: ColorCyan,
|
2015-04-18 06:03:22 +08:00
|
|
|
MBarChartBar: ColorRed,
|
|
|
|
MBarChartNum: ColorWhite,
|
|
|
|
MBarChartText: ColorCyan,
|
2015-05-27 03:37:04 +08:00
|
|
|
TabActiveBg: ColorMagenta,
|
2015-03-12 04:15:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var theme = themeDefault // global dep
|
|
|
|
|
2015-03-20 17:35:09 +08:00
|
|
|
// Theme returns the currently used theme.
|
|
|
|
func Theme() ColorScheme {
|
|
|
|
return theme
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTheme sets a new, custom theme.
|
|
|
|
func SetTheme(newTheme ColorScheme) {
|
|
|
|
theme = newTheme
|
|
|
|
}
|
|
|
|
|
|
|
|
// UseTheme sets a predefined scheme. Currently available: "hello-world" and
|
|
|
|
// "black-and-white".
|
2015-03-12 04:15:59 +08:00
|
|
|
func UseTheme(th string) {
|
|
|
|
switch th {
|
|
|
|
case "helloworld":
|
|
|
|
theme = themeHelloWorld
|
|
|
|
default:
|
|
|
|
theme = themeDefault
|
|
|
|
}
|
|
|
|
}
|
2015-09-21 15:11:58 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
var ColorMap = map[string]Attribute{
|
|
|
|
"fg": ColorWhite,
|
|
|
|
"bg": ColorDefault,
|
|
|
|
"border.fg": ColorWhite,
|
|
|
|
"label.fg": ColorGreen,
|
|
|
|
"par.fg": ColorYellow,
|
|
|
|
"par.label.bg": ColorWhite,
|
|
|
|
}
|
|
|
|
|
|
|
|
func ThemeAttr(name string) Attribute {
|
|
|
|
return lookUpAttr(ColorMap, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func lookUpAttr(clrmap map[string]Attribute, name string) Attribute {
|
|
|
|
|
|
|
|
a, ok := clrmap[name]
|
|
|
|
if ok {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
ns := strings.Split(name, ".")
|
|
|
|
for i := range ns {
|
|
|
|
nn := strings.Join(ns[i:len(ns)], ".")
|
|
|
|
a, ok = ColorMap[nn]
|
|
|
|
if ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
// 0<=r,g,b <= 5
|
|
|
|
func ColorRGB(r, g, b int) Attribute {
|
|
|
|
within := func(n int) int {
|
|
|
|
if n < 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
if n > 5 {
|
|
|
|
return 5
|
|
|
|
}
|
|
|
|
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
r, b, g = within(r), within(b), within(g)
|
|
|
|
return Attribute(0x0f + 36*r + 6*g + b)
|
|
|
|
}
|