Added colorscheme support

This commit is contained in:
Caleb Bassi 2018-02-21 02:24:36 -08:00
parent 6a92c31be6
commit bcc1d35b55
12 changed files with 144 additions and 71 deletions

View File

@ -84,6 +84,7 @@ Feel free to add a new one. You can use 256 colors, bold, underline, and reverse
- zooming in and out of graphs
- add gif
- create a mac binary
- grid should create a filled buffer so that bg is written everywhere
* cleaning up code
- termui Blocks should ignore writing to the outside area
- Ignore writes to outside of inner area, or give error?

View File

@ -6,25 +6,20 @@ var DefaultCS = Colorscheme{
Bg: -1,
Border{
Labels: 0,
Line: 0,
},
BorderLabel: 7,
BorderLine: 6,
CPU{
Lines: []int{0, 0, 0, 0},
},
CPULines: []int{1, 2, 3, 4, 5, 6, 7, 8},
Mem{
Main: 0,
Swap: 0,
},
MainMem: 5,
SwapMem: 11,
Proc{
Cursor: 5,
},
ProcCursor: 4,
Sparkline{
Graph: 10,
},
Sparkline: 4,
DiskBar: 7,
TempLow: 2,
TempHigh: 1,
}

View File

@ -0,0 +1,25 @@
package colorschemes
var SolarizedCS = Colorscheme{
Name: "Default",
Author: "Caleb Bassi",
Bg: -1,
BorderLabel: 7,
BorderLine: 6,
CPULines: []int{1, 2, 3, 4, 5, 6, 7, 8},
MainMem: 5,
SwapMem: 11,
ProcCursor: 4,
Sparkline: 4,
DiskBar: 7,
TempLow: 2,
TempHigh: 1,
}

View File

@ -22,25 +22,20 @@ type Colorscheme struct {
Bg int
Border struct {
Labels int
Line int
}
BorderLabel int
BorderLine int
CPU struct {
Lines []int
}
CPULines []int
Mem struct {
Main int
Swap int
}
MainMem int
SwapMem int
Proc struct {
Cursor int
}
ProcCursor int
Sparkline struct {
Graph int
}
Sparkline int
DiskBar int
TempLow int
TempHigh int
}

View File

@ -1,13 +1,14 @@
package main
import (
// "fmt"
"fmt"
"os"
// "os/exec"
"os/signal"
"syscall"
"time"
"github.com/cjbassi/gotop/colorschemes"
ui "github.com/cjbassi/gotop/termui"
w "github.com/cjbassi/gotop/widgets"
"github.com/docopt/docopt-go"
@ -24,37 +25,61 @@ var (
procLoaded = make(chan bool, 1)
keyPressed = make(chan bool, 1)
cpu = w.NewCPU()
mem = w.NewMem()
proc = w.NewProc(procLoaded, keyPressed)
net = w.NewNet()
disk = w.NewDisk()
temp = w.NewTemp()
colorscheme = colorschemes.DefaultCS
help = w.NewHelpMenu()
cpu *w.CPU
mem *w.Mem
proc *w.Proc
net *w.Net
disk *w.Disk
temp *w.Temp
help *w.HelpMenu
)
// Sets up docopt which is a command line argument parser
func docoptInit() {
func arguments() {
usage := `
Usage: gotop [options]
Options:
-c, --color Set a colorscheme.
-h, --help Show this screen.
-u, --upgrade Updates gotop if needed.
-v, --version Show version.
-c, --color <name> Set a colorscheme.
-h, --help Show this screen.
-u, --upgrade Updates gotop if needed.
-v, --version Show version.
Colorschemes:
default
solarized
`
args, _ := docopt.ParseArgs(usage, os.Args[1:], VERSION)
if val, _ := args["--upgrade"]; val.(bool) {
updateGotop()
os.Exit(0)
}
if val, _ := args["--color"]; val.(bool) {
if val, _ := args["--color"]; val != nil {
handleColorscheme(val.(string))
}
}
func updateGotop() {
// cmd := exec.Command("sleep", "1")
// cmd.Run()
return
}
func handleColorscheme(cs string) {
switch cs {
case "solarized":
colorscheme = colorschemes.SolarizedCS
case "default":
colorscheme = colorschemes.DefaultCS
default:
fmt.Fprintf(os.Stderr, "error: colorscheme not recognized\n")
os.Exit(1)
}
}
@ -92,17 +117,48 @@ func keyBinds() {
})
}
func updateGotop() {
// cmd := exec.Command("sleep", "1")
// cmd.Run()
return
func termuiColors() {
ui.Theme.Fg = ui.Color(7)
ui.Theme.Bg = ui.Color(colorscheme.Bg)
ui.Theme.BorderBg = ui.Color(colorscheme.Bg)
ui.Theme.LabelBg = ui.Color(colorscheme.Bg)
ui.Theme.TableCursor = ui.Color(colorscheme.ProcCursor)
ui.Theme.Sparkline = ui.Color(colorscheme.Sparkline)
ui.Theme.BarColor = ui.Color(colorscheme.DiskBar)
ui.Theme.TempLow = ui.Color(colorscheme.TempLow)
ui.Theme.TempHigh = ui.Color(colorscheme.TempHigh)
}
func widgetColors() {
mem.LineColor["Main"] = ui.Color(colorscheme.MainMem)
mem.LineColor["Swap"] = ui.Color(colorscheme.SwapMem)
LineColor := make(map[string]ui.Color)
for i := 0; i < len(cpu.Data); i++ {
LineColor[fmt.Sprintf("CPU%d", i+1)] = ui.Color(colorscheme.CPULines[i])
}
cpu.LineColor = LineColor
}
func main() {
docoptInit()
arguments()
termuiColors()
keyBinds()
cpu = w.NewCPU()
mem = w.NewMem()
proc = w.NewProc(procLoaded, keyPressed)
net = w.NewNet()
disk = w.NewDisk()
temp = w.NewTemp()
help = w.NewHelpMenu()
widgetColors()
<-procLoaded
err := ui.Init()

View File

@ -18,8 +18,8 @@ type Block struct {
BorderBg Color
LabelFg Color
LabelBg Color
Bg Color
Fg Color
Bg Color
}
// NewBlock returns a *Block which inherits styles from current theme.

View File

@ -21,9 +21,12 @@ var DefaultTheme = Colorscheme{
BorderFg: 6,
BorderBg: -1,
SparkLine: 4,
Sparkline: 4,
LineGraph: -1,
TableCursor: 4,
BarColor: 7,
TempLow: 2,
TempHigh: 1,
}
// A ColorScheme represents the current look-and-feel of the dashboard.
@ -36,7 +39,10 @@ type Colorscheme struct {
BorderFg Color
BorderBg Color
SparkLine Color
Sparkline Color
LineGraph Color
TableCursor Color
BarColor Color
TempLow Color
TempHigh Color
}

View File

@ -18,7 +18,7 @@ func NewGauge() *Gauge {
return &Gauge{
Block: NewBlock(),
PercentColor: Theme.Fg,
BarColor: Theme.Bg,
BarColor: Theme.BarColor,
}
}
@ -30,11 +30,7 @@ func (g *Gauge) Buffer() *Buffer {
width := g.Percent * g.X / 100
for y := 1; y <= g.Y; y++ {
for x := 1; x <= width; x++ {
bg := g.BarColor
if bg == ColorDefault {
bg |= AttrReverse
}
buf.SetCell(x, y, Cell{' ', ColorDefault, bg})
buf.SetCell(x, y, Cell{' ', g.BarColor, g.BarColor})
}
}
@ -46,10 +42,12 @@ func (g *Gauge) Buffer() *Buffer {
for i, char := range s {
bg := g.Bg
fg := g.Fg
if x+i < width {
fg = g.BarColor
bg = AttrReverse
}
buf.SetCell(1+x+i, y, Cell{char, g.PercentColor, bg})
buf.SetCell(1+x+i, y, Cell{char, fg, bg})
}
return buf

View File

@ -29,13 +29,13 @@ func (bc *List) Buffer() *Buffer {
if y+1 > bc.Y {
break
}
bg := Color(2)
bg := Theme.TempLow
if bc.Data[y] >= bc.Threshold {
bg = Color(1)
bg = Theme.TempHigh
}
r := MaxString(text, (bc.X - 4))
buf.SetString(1, y+1, r, Color(7), ColorDefault)
buf.SetString(bc.X-2, y+1, fmt.Sprintf("%dC", bc.Data[y]), bg, ColorDefault)
buf.SetString(1, y+1, r, Color(7), bc.Bg)
buf.SetString(bc.X-2, y+1, fmt.Sprintf("%dC", bc.Data[y]), bg, bc.Bg)
}
return buf

View File

@ -27,7 +27,7 @@ func (s *Sparklines) Add(sl Sparkline) {
func NewSparkline() *Sparkline {
return &Sparkline{
TitleColor: Theme.Fg,
LineColor: Theme.SparkLine,
LineColor: Theme.Sparkline,
}
}

View File

@ -21,7 +21,6 @@ func NewCPU() *CPU {
for i := 0; i < c.count; i++ {
key := "CPU" + strconv.Itoa(i+1)
c.Data[key] = []float64{0}
c.LineColor[key] = ui.Color(1 + i)
}
go c.update()

View File

@ -17,8 +17,6 @@ func NewMem() *Mem {
m.Label = "Memory Usage"
m.Data["Main"] = []float64{0} // Sets initial data to 0
m.Data["Swap"] = []float64{0}
m.LineColor["Main"] = ui.Color(5)
m.LineColor["Swap"] = ui.Color(11)
go m.update()
ticker := time.NewTicker(m.interval)