Skip to content
This repository has been archived by the owner on Aug 29, 2020. It is now read-only.

Commit

Permalink
Add custom colorscheme loading. Closes #55
Browse files Browse the repository at this point in the history
  • Loading branch information
cjbassi committed Sep 18, 2018
1 parent bb36706 commit db1b2fb
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 3 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
File renamed without changes.
23 changes: 23 additions & 0 deletions colorschemes/default.json
Original file line number Diff line number Diff line change
@@ -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
}
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes.
25 changes: 24 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/signal"
"sort"
Expand All @@ -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"
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit db1b2fb

Please sign in to comment.