Add custom colorscheme loading. Closes #55

This commit is contained in:
Caleb Bassi 2018-09-18 13:42:49 -07:00
parent bb36706e86
commit db1b2fbc70
10 changed files with 50 additions and 3 deletions

View File

@ -68,8 +68,9 @@ go get github.com/cjbassi/gotop
### Colorschemes
gotop ships with a few colorschemes which can be set with the `-c` flag followed by the name of one.
You can find all the colorschemes in [src/colorschemes](https://github.com/cjbassi/gotop/tree/master/src/colorschemes) and you can make your own by checking out the [template](https://github.com/cjbassi/gotop/blob/master/src/colorschemes/template.go). Colorschemes PR's are welcome!
gotop ships with a few colorschemes which can be set with the `-c` flag followed by the name of one. You can find all the colorschemes in [colorschemes](https://github.com/cjbassi/gotop/tree/master/colorschemes).
To make a custom colorscheme, check out the [template](https://github.com/cjbassi/gotop/blob/master/colorschemes/template.go) for instructions and then use [default.json](https://github.com/cjbassi/gotop/blob/master/colorschemes/default.json) as a starter. Then you can put the file at `~/.config/gotop/{name}.json` and load it with `gotop -c {name}`. Colorschemes PR's are welcome!
### CLI Options

23
colorschemes/default.json Normal file
View File

@ -0,0 +1,23 @@
// Example json file to put in `~/.config/gotop/{name}.json` and load with
// `gotop -c {name}`. MUST DELETE THESE COMMENTS in order to load.
{
"Fg": 7,
"Bg": -1,
"BorderLabel": 7,
"BorderLine": 6,
"CPULines": [4, 3, 2, 1, 5, 6, 7, 8],
"MainMem": 5,
"SwapMem": 11,
"ProcCursor": 4,
"Sparkline": 4,
"DiskBar": 7,
"TempLow": 2,
"TempHigh": 1
}

View File

Before

Width:  |  Height:  |  Size: 91 KiB

After

Width:  |  Height:  |  Size: 91 KiB

View File

Before

Width:  |  Height:  |  Size: 87 KiB

After

Width:  |  Height:  |  Size: 87 KiB

25
main.go
View File

@ -1,7 +1,9 @@
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/signal"
"sort"
@ -10,7 +12,7 @@ import (
"syscall"
"time"
"github.com/cjbassi/gotop/src/colorschemes"
"github.com/cjbassi/gotop/colorschemes"
w "github.com/cjbassi/gotop/src/widgets"
ui "github.com/cjbassi/termui"
"github.com/docopt/docopt-go"
@ -105,9 +107,30 @@ func handleColorscheme(cs string) {
case "default-dark":
colorscheme = colorschemes.DefaultDark
default:
colorscheme = getCustomColorscheme(cs)
}
}
// getCustomColorscheme tries to read a custom json colorscheme from
// {$XDG_CONFIG_HOME,~/.config}/gotop/{name}.json
func getCustomColorscheme(name string) colorschemes.Colorscheme {
xdg := os.Getenv("XDG_CONFIG_HOME")
if xdg == "" {
xdg = os.ExpandEnv("$HOME") + "/.config"
}
file := xdg + "/gotop/" + name + ".json"
dat, err := ioutil.ReadFile(file)
if err != nil {
fmt.Fprintf(os.Stderr, "error: colorscheme not recognized\n")
os.Exit(1)
}
var colorscheme colorschemes.Colorscheme
err = json.Unmarshal(dat, &colorscheme)
if err != nil {
fmt.Fprintf(os.Stderr, "error: could not parse colorscheme\n")
os.Exit(1)
}
return colorscheme
}
func setupGrid() {