Initial commit

This commit is contained in:
sqshq 2019-01-24 23:10:38 -05:00
commit 2d8f958386
8 changed files with 199 additions and 0 deletions

28
app/config/config.go Normal file
View File

@ -0,0 +1,28 @@
package config
import (
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
)
type Config struct {
LineCharts []LineChartConfig `yaml:"line-charts"`
}
func Load(location string) *Config {
yamlFile, err := ioutil.ReadFile(location)
if err != nil {
log.Fatalf("Can't read config file: %s", location)
}
cfg := new(Config)
err = yaml.Unmarshal(yamlFile, cfg)
if err != nil {
log.Fatalf("Can't read config file: %v", err)
}
return cfg
}

31
app/config/data.go Normal file
View File

@ -0,0 +1,31 @@
package config
import (
"log"
"os/exec"
"strconv"
"strings"
)
type Data struct {
Label string `yaml:"label"`
Color string `yaml:"color"`
Script string `yaml:"script"`
}
func (d *Data) NextValue() (float64, error) {
output, err := exec.Command("sh", "-c", d.Script).Output()
if err != nil {
log.Printf("%s", err)
}
trimmedOutput := strings.TrimSpace(string(output))
floatValue, err := strconv.ParseFloat(trimmedOutput, 64)
if err != nil {
return 0, err
}
return floatValue, nil
}

9
app/config/linechart.go Normal file
View File

@ -0,0 +1,9 @@
package config
type LineChartConfig struct {
Title string `yaml:"title"`
Data []Data `yaml:"data"`
Position Position `yaml:"position"`
RefreshRateMs int `yaml:"refresh-rate-ms"`
Scale string `yaml:"scale"`
}

6
app/config/position.go Normal file
View File

@ -0,0 +1,6 @@
package config
type Position struct {
X int `yaml:"x"`
Y int `yaml:"y"`
}

6
app/config/size.go Normal file
View File

@ -0,0 +1,6 @@
package config
type Size struct {
X int `yaml:"x"`
Y int `yaml:"y"`
}

92
app/main.go Normal file
View File

@ -0,0 +1,92 @@
package main
import (
"github.com/sqshq/plot-try/app/config"
ui "github.com/sqshq/termui"
"github.com/sqshq/termui/widgets"
"log"
"math"
)
func main() {
cfg := config.Load("/Users/sqshq/config.yml")
for _, linechart := range cfg.LineCharts {
for _, data := range linechart.Data {
value, _ := data.NextValue()
log.Printf("%s: %s - %v", linechart.Title, data.Label, value)
}
}
printChart()
}
func printChart() {
if err := ui.Init(); err != nil {
log.Fatalf("failed to initialize termui: %v", err)
}
defer ui.Close()
sinData := func() [][]float64 {
n := 220
data := make([][]float64, 2)
data[0] = make([]float64, n)
data[1] = make([]float64, n)
for i := 0; i < n; i++ {
data[0][i] = 1 + math.Sin(float64(i)/5)
data[1][i] = 1 + math.Cos(float64(i)/5)
}
return data
}()
p0 := widgets.NewPlot()
p0.Title = "braille-mode Line Chart"
p0.Data = sinData
p0.SetRect(0, 0, 50, 15)
p0.AxesColor = ui.ColorWhite
p0.LineColors[0] = ui.ColorGreen
p1 := widgets.NewPlot()
p1.Title = "dot-mode line Chart"
p1.Marker = widgets.MarkerDot
p1.Data = [][]float64{[]float64{1, 2, 3, 4, 5}}
p1.SetRect(50, 0, 75, 10)
p1.DotRune = '+'
p1.AxesColor = ui.ColorWhite
p1.LineColors[0] = ui.ColorYellow
p1.DrawDirection = widgets.DrawLeft
p2 := widgets.NewPlot()
p2.Title = "dot-mode Scatter Plot"
p2.Marker = widgets.MarkerDot
p2.Data = make([][]float64, 2)
p2.Data[0] = []float64{1, 2, 3, 4, 5}
p2.Data[1] = sinData[1][4:]
p2.SetRect(0, 15, 50, 30)
p2.AxesColor = ui.ColorWhite
p2.LineColors[0] = ui.ColorCyan
p2.Type = widgets.ScatterPlot
p3 := widgets.NewPlot()
p3.Title = "braille-mode Scatter Plot"
p3.Data = make([][]float64, 2)
p3.Data[0] = []float64{1, 2, 3, 4, 5}
p3.Data[1] = sinData[1][4:]
p3.SetRect(45, 15, 80, 30)
p3.AxesColor = ui.ColorWhite
p3.LineColors[0] = ui.ColorCyan
p3.Marker = widgets.MarkerBraille
p3.Type = widgets.ScatterPlot
ui.Render(p0, p1, p2, p3)
uiEvents := ui.PollEvents()
for {
e := <-uiEvents
switch e.ID {
case "q", "<C-c>":
return
}
}
}

8
go.mod Normal file
View File

@ -0,0 +1,8 @@
module github.com/sqshq/plot-try
require (
github.com/mattn/go-runewidth v0.0.4 // indirect
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
github.com/sqshq/termui v0.0.0-20190125032456-731556c09f2c
gopkg.in/yaml.v2 v2.2.2
)

19
go.sum Normal file
View File

@ -0,0 +1,19 @@
github.com/google/pprof v0.0.0-20190109223431-e84dfd68c163/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y=
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
github.com/mitchellh/go-wordwrap v1.0.0 h1:6GlHJ/LTGMrIJbwgdqdl2eEH8o+Exx/0m8ir9Gns0u4=
github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d h1:x3S6kxmy49zXVVyhcnrFqxvNVCBPb2KZ9hV2RBdS840=
github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d/go.mod h1:IuKpRQcYE1Tfu+oAQqaLisqDeXgjyyltCfsaoYN18NQ=
github.com/sqshq/termui v0.0.0-20190125032456-731556c09f2c h1:BBEmIcD4UhAHDVWi3PVuA5TxVTZxcjmYzmdvhWkPfvE=
github.com/sqshq/termui v0.0.0-20190125032456-731556c09f2c/go.mod h1:puWaguPLLYPPKabYPVhZ8sDNe0nKSMiKWRfLVaaX8Zs=
golang.org/x/arch v0.0.0-20181203225421-5a4828bb7045/go.mod h1:cYlCBUl1MsqxdiKgmc4uh7TxZfWSFLOGSRR090WDxt8=
golang.org/x/crypto v0.0.0-20190103213133-ff983b9c42bc/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/sys v0.0.0-20190116161447-11f53e031339/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=