From d77dd1e5240390d3298a332fc0edfee4d5fa07fb Mon Sep 17 00:00:00 2001 From: sqshq Date: Sat, 23 Mar 2019 20:16:59 -0400 Subject: [PATCH] added palette support for all the components, adjusted light theme colors --- component/asciibox/asciibox.go | 14 +++++++++++--- component/barchart/barchart.go | 8 +++++--- component/block.go | 11 +++++++++-- component/gauge/gauge.go | 10 ++++++---- component/menu.go | 19 +++++++++---------- component/runchart/grid.go | 15 +++++++-------- component/runchart/legend.go | 3 +-- component/runchart/runchart.go | 6 ++++-- component/statusbar.go | 6 +++--- config.yml | 2 +- config/default.go | 12 ++++-------- console/palette.go | 23 +++++++++++++++++++---- main.go | 12 +++++++----- 13 files changed, 86 insertions(+), 55 deletions(-) diff --git a/component/asciibox/asciibox.go b/component/asciibox/asciibox.go index ca1b9d2..5159816 100644 --- a/component/asciibox/asciibox.go +++ b/component/asciibox/asciibox.go @@ -6,6 +6,7 @@ import ( "github.com/sqshq/sampler/asset" "github.com/sqshq/sampler/component" "github.com/sqshq/sampler/config" + "github.com/sqshq/sampler/console" "github.com/sqshq/sampler/data" "image" ) @@ -19,11 +20,12 @@ type AsciiBox struct { style ui.Style render *fl.AsciiRender options *fl.RenderOptions + palette console.Palette } const asciiFontExtension = ".flf" -func NewAsciiBox(c config.AsciiBoxConfig) *AsciiBox { +func NewAsciiBox(c config.AsciiBoxConfig, palette console.Palette) *AsciiBox { options := fl.NewRenderOptions() options.FontName = string(*c.Font) @@ -36,12 +38,18 @@ func NewAsciiBox(c config.AsciiBoxConfig) *AsciiBox { render := fl.NewAsciiRender() _ = render.LoadBindataFont(fontStr, options.FontName) + color := c.Color + if color == nil { + color = &palette.BaseColor + } + box := AsciiBox{ - Block: component.NewBlock(c.Title, true), + Block: component.NewBlock(c.Title, true, palette), Consumer: data.NewConsumer(), - style: ui.NewStyle(*c.Color), + style: ui.NewStyle(*color), render: render, options: options, + palette: palette, } go func() { diff --git a/component/barchart/barchart.go b/component/barchart/barchart.go index 16a22f8..101a8f1 100644 --- a/component/barchart/barchart.go +++ b/component/barchart/barchart.go @@ -25,6 +25,7 @@ type BarChart struct { scale int maxValue float64 count int64 + palette console.Palette } type Bar struct { @@ -34,14 +35,15 @@ type Bar struct { delta float64 } -func NewBarChart(c config.BarChartConfig) *BarChart { +func NewBarChart(c config.BarChartConfig, palette console.Palette) *BarChart { chart := BarChart{ - Block: component.NewBlock(c.Title, true), + Block: component.NewBlock(c.Title, true, palette), Consumer: data.NewConsumer(), bars: []Bar{}, scale: *c.Scale, maxValue: -math.MaxFloat64, + palette: palette, } for _, i := range c.Items { @@ -119,7 +121,7 @@ func (b *BarChart) Draw(buffer *ui.Buffer) { barWidth := int(math.Ceil(float64(b.Inner.Dx()-2*barIndent-len(b.bars)*barIndent) / float64(len(b.bars)))) barXCoordinate := b.Inner.Min.X + barIndent - labelStyle := ui.NewStyle(console.ColorWhite) + labelStyle := ui.NewStyle(b.palette.BaseColor) for _, bar := range b.bars { diff --git a/component/block.go b/component/block.go index cade7a6..2691d46 100644 --- a/component/block.go +++ b/component/block.go @@ -1,12 +1,19 @@ package component import ( + "fmt" ui "github.com/gizak/termui/v3" + "github.com/sqshq/sampler/console" ) -func NewBlock(title string, border bool) *ui.Block { +func NewBlock(title string, border bool, palette console.Palette) *ui.Block { + style := ui.Style{Fg: palette.BaseColor, Bg: ui.ColorClear} block := ui.NewBlock() - block.Title = title block.Border = border + block.BorderStyle = style + block.TitleStyle = style + if len(title) > 0 { + block.Title = fmt.Sprintf(" %s ", title) + } return block } diff --git a/component/gauge/gauge.go b/component/gauge/gauge.go index 14946fd..25860d4 100644 --- a/component/gauge/gauge.go +++ b/component/gauge/gauge.go @@ -27,15 +27,17 @@ type Gauge struct { curValue float64 color ui.Color scale int + palette console.Palette } -func NewGauge(c config.GaugeConfig) *Gauge { +func NewGauge(c config.GaugeConfig, palette console.Palette) *Gauge { gauge := Gauge{ - Block: component.NewBlock(c.Title, true), + Block: component.NewBlock(c.Title, true, palette), Consumer: data.NewConsumer(), scale: *c.Scale, color: *c.Color, + palette: palette, } go func() { @@ -105,9 +107,9 @@ func (g *Gauge) Draw(buffer *ui.Buffer) { labelYCoordinate := g.Inner.Min.Y + ((g.Inner.Dy() - 1) / 2) if labelYCoordinate < g.Inner.Max.Y { for i, char := range label { - style := ui.NewStyle(console.ColorWhite) + style := ui.NewStyle(g.palette.BaseColor) if labelXCoordinate+i+1 <= g.Inner.Min.X+barWidth { - style = ui.NewStyle(console.ColorWhite, ui.ColorClear) + style = ui.NewStyle(g.palette.BaseColor, ui.ColorClear) } buffer.SetCell(ui.NewCell(char, style), image.Pt(labelXCoordinate+i, labelYCoordinate)) } diff --git a/component/menu.go b/component/menu.go index cba6767..00c50b8 100644 --- a/component/menu.go +++ b/component/menu.go @@ -8,11 +8,12 @@ import ( ) type Menu struct { - ui.Block + *ui.Block options []MenuOption component Component mode MenuMode option MenuOption + palette console.Palette } type MenuMode rune @@ -37,14 +38,13 @@ const ( minimalMenuHeight = 8 ) -func NewMenu() *Menu { - block := *ui.NewBlock() - block.Border = true +func NewMenu(palette console.Palette) *Menu { return &Menu{ - Block: block, + Block: NewBlock("", true, palette), options: []MenuOption{MenuOptionMove, MenuOptionResize, MenuOptionPinpoint, MenuOptionResume}, mode: MenuModeIdle, option: MenuOptionMove, + palette: palette, } } @@ -102,7 +102,7 @@ func (m *Menu) Draw(buffer *ui.Buffer) { } m.updateDimensions() - buffer.Fill(ui.NewCell(' ', ui.NewStyle(ui.ColorBlack)), m.GetRect()) + buffer.Fill(ui.NewCell(' ', ui.NewStyle(m.palette.ReverseColor)), m.GetRect()) if m.Dy() > minimalMenuHeight { m.drawInnerBorder(buffer) @@ -198,9 +198,8 @@ func (m *Menu) printAllDirectionsArrowSign(buffer *ui.Buffer, y int) { func (m *Menu) renderOptions(buffer *ui.Buffer) { - // TODO extract styles to console.Palette - highlightedStyle := ui.NewStyle(console.ColorBlack, console.ColorOlive) - regularStyle := ui.NewStyle(console.ColorWhite, console.ColorBlack) + highlightedStyle := ui.NewStyle(m.palette.BaseColor, console.ColorOlive) + regularStyle := ui.NewStyle(m.palette.BaseColor, m.palette.ReverseColor) offset := 1 for _, option := range m.options { @@ -212,7 +211,7 @@ func (m *Menu) renderOptions(buffer *ui.Buffer) { if option != MenuOptionPinpoint || m.component.Type == config.TypeRunChart { offset += 2 - point := getMiddlePoint(m.Block.Rectangle, string(option), offset-5) + point := getMiddlePoint(m.Block.Rectangle, string(option), offset-6) buffer.SetString(string(option), style, point) } } diff --git a/component/runchart/grid.go b/component/runchart/grid.go index 314f85e..231d74f 100644 --- a/component/runchart/grid.go +++ b/component/runchart/grid.go @@ -2,7 +2,6 @@ package runchart import ( ui "github.com/gizak/termui/v3" - "github.com/sqshq/sampler/console" "image" "math" "time" @@ -35,13 +34,13 @@ func (c *RunChart) newChartGrid() ChartGrid { func (c *RunChart) renderAxes(buffer *ui.Buffer) { // draw origin cell buffer.SetCell( - ui.NewCell(ui.BOTTOM_LEFT, ui.NewStyle(console.ColorWhite)), + ui.NewCell(ui.BOTTOM_LEFT, ui.NewStyle(c.palette.BaseColor)), image.Pt(c.Inner.Min.X+c.grid.minTimeWidth, c.Inner.Max.Y-xAxisLabelsHeight-1)) // draw x axis line for i := c.grid.minTimeWidth + 1; i < c.Inner.Dx(); i++ { buffer.SetCell( - ui.NewCell(ui.HORIZONTAL_DASH, ui.NewStyle(console.ColorWhite)), + ui.NewCell(ui.HORIZONTAL_DASH, ui.NewStyle(c.palette.BaseColor)), image.Pt(i+c.Inner.Min.X, c.Inner.Max.Y-xAxisLabelsHeight-1)) } @@ -49,7 +48,7 @@ func (c *RunChart) renderAxes(buffer *ui.Buffer) { for y := 0; y < c.Inner.Dy()-xAxisLabelsHeight-2; y = y + 2 { for x := 1; x <= c.grid.linesCount; x++ { buffer.SetCell( - ui.NewCell(ui.VERTICAL_DASH, ui.NewStyle(console.ColorDarkGrey)), + ui.NewCell(ui.VERTICAL_DASH, ui.NewStyle(c.palette.MediumColor)), image.Pt(c.grid.maxTimeWidth-x*xAxisGridWidth, y+c.Inner.Min.Y+1)) } } @@ -57,7 +56,7 @@ func (c *RunChart) renderAxes(buffer *ui.Buffer) { // draw y axis line for i := 0; i < c.Inner.Dy()-xAxisLabelsHeight-1; i++ { buffer.SetCell( - ui.NewCell(ui.VERTICAL_DASH, ui.NewStyle(console.ColorWhite)), + ui.NewCell(ui.VERTICAL_DASH, ui.NewStyle(c.palette.BaseColor)), image.Pt(c.Inner.Min.X+c.grid.minTimeWidth, i+c.Inner.Min.Y)) } @@ -66,7 +65,7 @@ func (c *RunChart) renderAxes(buffer *ui.Buffer) { labelTime := c.grid.timeRange.max.Add(time.Duration(-i) * c.timescale) buffer.SetString( labelTime.Format("15:04:05"), - ui.NewStyle(console.ColorWhite), + ui.NewStyle(c.palette.BaseColor), image.Pt(c.grid.maxTimeWidth-xAxisLabelsWidth/2-i*(xAxisGridWidth), c.Inner.Max.Y-1)) } @@ -78,13 +77,13 @@ func (c *RunChart) renderAxes(buffer *ui.Buffer) { value := c.grid.valueExtrema.max - (valuePerY * float64(i) * (yAxisLabelsIndent + yAxisLabelsHeight)) buffer.SetString( formatValue(value, c.scale), - ui.NewStyle(console.ColorWhite), + ui.NewStyle(c.palette.BaseColor), image.Pt(c.Inner.Min.X, 1+c.Inner.Min.Y+i*(yAxisLabelsIndent+yAxisLabelsHeight))) } } else { buffer.SetString( formatValue(c.grid.valueExtrema.max, c.scale), - ui.NewStyle(console.ColorWhite), + ui.NewStyle(c.palette.BaseColor), image.Pt(c.Inner.Min.X, c.Inner.Min.Y+c.Inner.Dy()/2)) } } diff --git a/component/runchart/legend.go b/component/runchart/legend.go index 321c51f..2145c98 100644 --- a/component/runchart/legend.go +++ b/component/runchart/legend.go @@ -3,7 +3,6 @@ package runchart import ( "fmt" ui "github.com/gizak/termui/v3" - "github.com/sqshq/sampler/console" "image" "math" ) @@ -55,7 +54,7 @@ func (c *RunChart) renderLegend(buffer *ui.Buffer, rectangle image.Rectangle) { y := c.Inner.Min.Y + yAxisLegendIndent + row*height titleStyle := ui.NewStyle(line.color) - detailsStyle := ui.NewStyle(console.ColorWhite) + detailsStyle := ui.NewStyle(c.palette.BaseColor) buffer.SetString(string(ui.DOT), titleStyle, image.Pt(x-2, y)) buffer.SetString(line.label, titleStyle, image.Pt(x, y)) diff --git a/component/runchart/runchart.go b/component/runchart/runchart.go index 057ebc0..29ef3e3 100644 --- a/component/runchart/runchart.go +++ b/component/runchart/runchart.go @@ -51,6 +51,7 @@ type RunChart struct { selection time.Time scale int legend Legend + palette console.Palette } type TimePoint struct { @@ -78,10 +79,10 @@ type ValueExtrema struct { min float64 } -func NewRunChart(c config.RunChartConfig) *RunChart { +func NewRunChart(c config.RunChartConfig, palette console.Palette) *RunChart { chart := RunChart{ - Block: component.NewBlock(c.Title, true), + Block: component.NewBlock(c.Title, true, palette), Consumer: data.NewConsumer(), lines: []TimeLine{}, timescale: calculateTimescale(*c.RefreshRateMs), @@ -89,6 +90,7 @@ func NewRunChart(c config.RunChartConfig) *RunChart { scale: *c.Scale, mode: ModeDefault, legend: Legend{Enabled: c.Legend.Enabled, Details: c.Legend.Details}, + palette: palette, } for _, i := range c.Items { diff --git a/component/statusbar.go b/component/statusbar.go index 87c9686..99f9ed5 100644 --- a/component/statusbar.go +++ b/component/statusbar.go @@ -12,16 +12,16 @@ const ( ) type StatusBar struct { - ui.Block + *ui.Block keyBindings []string configFileName string } -func NewStatusLine(configFileName string) *StatusBar { +func NewStatusLine(configFileName string, palette console.Palette) *StatusBar { block := *ui.NewBlock() block.Border = false return &StatusBar{ - Block: block, + Block: NewBlock("", false, palette), configFileName: configFileName, keyBindings: []string{ "(Q) quit", diff --git a/config.yml b/config.yml index e1b3fa8..7a16454 100644 --- a/config.yml +++ b/config.yml @@ -80,6 +80,6 @@ asciiboxes: position: [[53, 17], [27, 5]] value: date +%r - title: UTC TIME - position: [[53, 22], [27, 7]] + position: [[53, 22], [27, 6]] value: env TZ=UTC date +%r font: 3d diff --git a/config/default.go b/config/default.go index 848ccc0..49f1be5 100644 --- a/config/default.go +++ b/config/default.go @@ -98,10 +98,6 @@ func (c *Config) setDefaultValues() { font := console.AsciiFontFlat box.Font = &font } - if box.Color == nil { - color := console.ColorWhite - box.Color = &color - } c.AsciiBoxes[i] = box } } @@ -139,12 +135,12 @@ func (c *Config) setDefaultLayout() { func (c *Config) setDefaultColors() { palette := console.GetPalette(*c.Theme) - colorsCount := len(palette.Colors) + colorsCount := len(palette.ContentColors) for _, ch := range c.RunCharts { for j, item := range ch.Items { if item.Color == nil { - item.Color = &palette.Colors[j%colorsCount] + item.Color = &palette.ContentColors[j%colorsCount] ch.Items[j] = item } } @@ -153,7 +149,7 @@ func (c *Config) setDefaultColors() { for _, b := range c.BarCharts { for j, item := range b.Items { if item.Color == nil { - item.Color = &palette.Colors[j%colorsCount] + item.Color = &palette.ContentColors[j%colorsCount] b.Items[j] = item } } @@ -161,7 +157,7 @@ func (c *Config) setDefaultColors() { for i, g := range c.Gauges { if g.Color == nil { - g.Color = &palette.Colors[i%colorsCount] + g.Color = &palette.ContentColors[i%colorsCount] c.Gauges[i] = g } } diff --git a/console/palette.go b/console/palette.go index dc82e95..0eeefee 100644 --- a/console/palette.go +++ b/console/palette.go @@ -20,7 +20,10 @@ const ( ColorOrange ui.Color = 166 ColorPurple ui.Color = 129 ColorGreen ui.Color = 64 + ColorDarkRed ui.Color = 88 + ColorBlueViolet ui.Color = 57 ColorDarkGrey ui.Color = 238 + ColorLightGrey ui.Color = 254 ColorGrey ui.Color = 242 ColorWhite ui.Color = 15 ColorBlack ui.Color = 0 @@ -33,16 +36,28 @@ const ( ) type Palette struct { - Colors []ui.Color - // TODO Menu colors, like Dark, Medium, Light etc + ContentColors []ui.Color + BaseColor ui.Color + MediumColor ui.Color + ReverseColor ui.Color } func GetPalette(theme Theme) Palette { switch theme { case ThemeDark: - return Palette{Colors: []ui.Color{ColorOlive, ColorDeepSkyBlue, ColorDeepPink, ColorWhite, ColorGrey, ColorGreen, ColorOrange, ColorCian, ColorPurple}} + return Palette{ + ContentColors: []ui.Color{ColorOlive, ColorDeepSkyBlue, ColorDeepPink, ColorWhite, ColorGrey, ColorGreen, ColorOrange, ColorCian, ColorPurple}, + BaseColor: ColorWhite, + MediumColor: ColorDarkGrey, + ReverseColor: ColorBlack, + } case ThemeLight: - return Palette{Colors: []ui.Color{ColorOlive, ColorDeepSkyBlue, ColorDeepPink, ColorWhite, ColorGrey, ColorGreen, ColorOrange, ColorCian, ColorPurple}} + return Palette{ + ContentColors: []ui.Color{ColorBlack, ColorDarkRed, ColorBlueViolet, ColorGrey, ColorGreen}, + BaseColor: ColorBlack, + MediumColor: ColorLightGrey, + ReverseColor: ColorWhite, + } default: panic(fmt.Sprintf("Following theme is not supported: %v", theme)) } diff --git a/main.go b/main.go index e478009..4e90869 100644 --- a/main.go +++ b/main.go @@ -38,28 +38,30 @@ func main() { player := asset.NewAudioPlayer() defer player.Close() + palette := console.GetPalette(*cfg.Theme) width, height := ui.TerminalDimensions() - lout := layout.NewLayout(width, height, component.NewStatusLine(opt.ConfigFile), component.NewMenu()) + + lout := layout.NewLayout(width, height, component.NewStatusLine(opt.ConfigFile, palette), component.NewMenu(palette)) starter := &Starter{lout, player, opt} for _, c := range cfg.RunCharts { - cpt := runchart.NewRunChart(c) + cpt := runchart.NewRunChart(c, palette) starter.start(cpt, cpt.Consumer, c.ComponentConfig, c.Items, c.Triggers) } for _, c := range cfg.AsciiBoxes { - cpt := asciibox.NewAsciiBox(c) + cpt := asciibox.NewAsciiBox(c, palette) starter.start(cpt, cpt.Consumer, c.ComponentConfig, []config.Item{c.Item}, c.Triggers) } for _, c := range cfg.BarCharts { - cpt := barchart.NewBarChart(c) + cpt := barchart.NewBarChart(c, palette) starter.start(cpt, cpt.Consumer, c.ComponentConfig, c.Items, c.Triggers) } for _, c := range cfg.Gauges { - cpt := gauge.NewGauge(c) + cpt := gauge.NewGauge(c, palette) starter.start(cpt, cpt.Consumer, c.ComponentConfig, c.Items, c.Triggers) }