From db1b2fbc70536b0b7ad8f36b1fdee297094eb139 Mon Sep 17 00:00:00 2001 From: Caleb Bassi Date: Tue, 18 Sep 2018 13:42:49 -0700 Subject: [PATCH] Add custom colorscheme loading. Closes #55 --- README.md | 5 ++-- {src/colorschemes => colorschemes}/default.go | 0 colorschemes/default.json | 23 ++++++++++++++++ .../default_dark.go | 0 {src/colorschemes => colorschemes}/monokai.go | 0 .../colorschemes => colorschemes}/monokai.png | Bin .../solarized.go | 0 .../solarized.png | Bin .../colorschemes => colorschemes}/template.go | 0 main.go | 25 +++++++++++++++++- 10 files changed, 50 insertions(+), 3 deletions(-) rename {src/colorschemes => colorschemes}/default.go (100%) create mode 100644 colorschemes/default.json rename {src/colorschemes => colorschemes}/default_dark.go (100%) rename {src/colorschemes => colorschemes}/monokai.go (100%) rename {src/colorschemes => colorschemes}/monokai.png (100%) rename {src/colorschemes => colorschemes}/solarized.go (100%) rename {src/colorschemes => colorschemes}/solarized.png (100%) rename {src/colorschemes => colorschemes}/template.go (100%) diff --git a/README.md b/README.md index e1062ac..690179b 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/colorschemes/default.go b/colorschemes/default.go similarity index 100% rename from src/colorschemes/default.go rename to colorschemes/default.go diff --git a/colorschemes/default.json b/colorschemes/default.json new file mode 100644 index 0000000..5d26a8b --- /dev/null +++ b/colorschemes/default.json @@ -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 +} diff --git a/src/colorschemes/default_dark.go b/colorschemes/default_dark.go similarity index 100% rename from src/colorschemes/default_dark.go rename to colorschemes/default_dark.go diff --git a/src/colorschemes/monokai.go b/colorschemes/monokai.go similarity index 100% rename from src/colorschemes/monokai.go rename to colorschemes/monokai.go diff --git a/src/colorschemes/monokai.png b/colorschemes/monokai.png similarity index 100% rename from src/colorschemes/monokai.png rename to colorschemes/monokai.png diff --git a/src/colorschemes/solarized.go b/colorschemes/solarized.go similarity index 100% rename from src/colorschemes/solarized.go rename to colorschemes/solarized.go diff --git a/src/colorschemes/solarized.png b/colorschemes/solarized.png similarity index 100% rename from src/colorschemes/solarized.png rename to colorschemes/solarized.png diff --git a/src/colorschemes/template.go b/colorschemes/template.go similarity index 100% rename from src/colorschemes/template.go rename to colorschemes/template.go diff --git a/main.go b/main.go index a3956ed..8021ae5 100644 --- a/main.go +++ b/main.go @@ -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() {