config enhancement: write location & size as a single array (flow style)

This commit is contained in:
sqshq 2019-03-17 22:41:23 -04:00
parent 61aa7d2e44
commit 1affe37594
6 changed files with 49 additions and 81 deletions

View File

@ -11,7 +11,7 @@ type Component struct {
*data.Consumer *data.Consumer
Type config.ComponentType Type config.ComponentType
Title string Title string
Position config.Position Location config.Location
Size config.Size Size config.Size
RefreshRateMs int RefreshRateMs int
} }
@ -22,15 +22,15 @@ func NewComponent(dbl ui.Drawable, cmr *data.Consumer, cfg config.ComponentConfi
Consumer: cmr, Consumer: cmr,
Type: cfg.Type, Type: cfg.Type,
Title: cfg.Title, Title: cfg.Title,
Position: cfg.Position, Location: cfg.GetLocation(),
Size: cfg.Size, Size: cfg.GetSize(),
RefreshRateMs: *cfg.RefreshRateMs, RefreshRateMs: *cfg.RefreshRateMs,
} }
} }
func (c *Component) Move(x, y int) { func (c *Component) Move(x, y int) {
c.Position.X += x c.Location.X += x
c.Position.Y += y c.Location.Y += y
c.normalize() c.normalize()
} }
@ -41,10 +41,10 @@ func (c *Component) Resize(x, y int) {
} }
func (c *Component) normalize() { func (c *Component) normalize() {
if c.Position.X < 0 { if c.Location.X < 0 {
c.Position.X = 0 c.Location.X = 0
} }
if c.Position.Y < 0 { if c.Location.Y < 0 {
c.Position.Y = 0 c.Location.Y = 0
} }
} }

View File

@ -250,8 +250,8 @@ func (l *Layout) Draw(buffer *ui.Buffer) {
for _, c := range l.Components { for _, c := range l.Components {
x1 := math.Floor(float64(c.Position.X) * columnWidth) x1 := math.Floor(float64(c.Location.X) * columnWidth)
y1 := math.Floor(float64(c.Position.Y) * rowHeight) y1 := math.Floor(float64(c.Location.Y) * rowHeight)
x2 := x1 + math.Floor(float64(c.Size.X))*columnWidth x2 := x1 + math.Floor(float64(c.Size.X))*columnWidth
y2 := y1 + math.Floor(float64(c.Size.Y))*rowHeight y2 := y1 + math.Floor(float64(c.Size.Y))*rowHeight

View File

@ -1,11 +1,6 @@
runcharts: runcharts:
- title: SEARCH ENGINE RESPONSE TIME (sec) - title: SEARCH ENGINE RESPONSE TIME (sec)
position: position: [[0, 0], [53, 16]]
w: 0
h: 0
size:
w: 53
h: 16
triggers: triggers:
- title: Latency threshold exceeded - title: Latency threshold exceeded
condition: echo "$prev < 0.35 && $cur > 0.35" |bc -l condition: echo "$prev < 0.35 && $cur > 0.35" |bc -l
@ -23,12 +18,7 @@ runcharts:
- label: BING - label: BING
value: curl -o /dev/null -s -w '%{time_total}' https://www.bing.com value: curl -o /dev/null -s -w '%{time_total}' https://www.bing.com
- title: MONGO COLLECTIONS COUNT - title: MONGO COLLECTIONS COUNT
position: position: [[53, 0], [27, 8]]
w: 53
h: 0
size:
w: 27
h: 8
legend: legend:
enabled: true enabled: true
details: false details: false
@ -41,12 +31,7 @@ runcharts:
barcharts: barcharts:
- title: EVENTS BY STATUS - title: EVENTS BY STATUS
refresh-rate-ms: 1000 refresh-rate-ms: 1000
position: position: [[0, 17], [53, 12]]
w: 0
h: 17
size:
w: 53
h: 12
scale: 0 scale: 0
items: items:
- label: NEW - label: NEW
@ -61,45 +46,25 @@ barcharts:
value: mongo --quiet --host=localhost blog --eval "db.getCollection('posts').find({status:'INACTIVE'}).itcount()" value: mongo --quiet --host=localhost blog --eval "db.getCollection('posts').find({status:'INACTIVE'}).itcount()"
gauges: gauges:
- title: YEAR PROGRESS - title: YEAR PROGRESS
position: position: [[53, 8], [27, 2]]
w: 53
h: 8
size:
w: 27
h: 2
values: values:
cur: date +%j cur: date +%j
max: echo 365 max: echo 365
min: echo 0 min: echo 0
- title: DAY PROGRESS - title: DAY PROGRESS
position: position: [[53, 10], [27, 2]]
w: 53
h: 10
size:
w: 27
h: 2
values: values:
cur: date +%H cur: date +%H
max: echo 24 max: echo 24
min: echo 0 min: echo 0
- title: HOUR PROGRESS - title: HOUR PROGRESS
position: position: [[53, 12], [27, 2]]
w: 53
h: 12
size:
w: 27
h: 2
values: values:
cur: date +%M cur: date +%M
max: echo 60 max: echo 60
min: echo 0 min: echo 0
- title: MINUTE PROGRESS - title: MINUTE PROGRESS
position: position: [[53, 14], [27, 2]]
w: 53
h: 14
size:
w: 27
h: 2
triggers: triggers:
- title: CLOCK BELL EVERY MINUTE - title: CLOCK BELL EVERY MINUTE
condition: '[ $label == "cur" ] && [ $cur -eq 0 ] && echo 1 || echo 0' condition: '[ $label == "cur" ] && [ $cur -eq 0 ] && echo 1 || echo 0'
@ -112,19 +77,9 @@ gauges:
min: echo 0 min: echo 0
asciiboxes: asciiboxes:
- title: LOCAL TIME - title: LOCAL TIME
position: position: [[53, 17], [27, 5]]
w: 53
h: 17
size:
w: 27
h: 5
value: date +%r value: date +%r
- title: UTC TIME - title: UTC TIME
position: position: [[53, 22], [27, 7]]
w: 53
h: 22
size:
w: 27
h: 7
value: env TZ=UTC date +%r value: env TZ=UTC date +%r
font: 3d font: 3d

View File

@ -19,12 +19,19 @@ const (
type ComponentConfig struct { type ComponentConfig struct {
Title string `yaml:"title"` Title string `yaml:"title"`
RefreshRateMs *int `yaml:"refresh-rate-ms,omitempty"` RefreshRateMs *int `yaml:"refresh-rate-ms,omitempty"`
Position Position `yaml:"position"` Position [][]int `yaml:"position,flow"`
Size Size `yaml:"size"`
Triggers []TriggerConfig `yaml:"triggers,omitempty"` Triggers []TriggerConfig `yaml:"triggers,omitempty"`
Type ComponentType `yaml:",omitempty"` Type ComponentType `yaml:",omitempty"`
} }
func (c *ComponentConfig) GetLocation() Location {
return Location{X: c.Position[0][0], Y: c.Position[0][1]}
}
func (c *ComponentConfig) GetSize() Size {
return Size{X: c.Position[1][0], Y: c.Position[1][1]}
}
type TriggerConfig struct { type TriggerConfig struct {
Title string `yaml:"title"` Title string `yaml:"title"`
Condition string `yaml:"condition"` Condition string `yaml:"condition"`
@ -70,25 +77,32 @@ type LegendConfig struct {
Details bool `yaml:"details"` Details bool `yaml:"details"`
} }
type Position struct {
X int `yaml:"w"`
Y int `yaml:"h"`
}
type Size struct {
X int `yaml:"w"`
Y int `yaml:"h"`
}
type Item struct { type Item struct {
Label *string `yaml:"label,omitempty"` Label *string `yaml:"label,omitempty"`
Script string `yaml:"value"` Script string `yaml:"value"`
Color *ui.Color `yaml:"color,omitempty"` Color *ui.Color `yaml:"color,omitempty"`
} }
type Location struct {
X int
Y int
}
type Size struct {
X int
Y int
}
type ComponentSettings struct { type ComponentSettings struct {
Type ComponentType Type ComponentType
Title string Title string
Size Size Size Size
Position Position Location Location
}
func getPosition(location Location, size Size) [][]int {
return [][]int{
{location.X, location.Y},
{size.X, size.Y},
}
} }

View File

@ -42,8 +42,7 @@ func Update(settings []ComponentSettings) {
cfg := readFile(os.Args[1]) cfg := readFile(os.Args[1])
for _, s := range settings { for _, s := range settings {
componentConfig := cfg.findComponent(s.Type, s.Title) componentConfig := cfg.findComponent(s.Type, s.Title)
componentConfig.Size = s.Size componentConfig.Position = getPosition(s.Location, s.Size)
componentConfig.Position = s.Position
} }
saveFile(cfg) saveFile(cfg)
} }

View File

@ -75,7 +75,7 @@ func (h *Handler) handleExit() {
var settings []config.ComponentSettings var settings []config.ComponentSettings
for _, c := range h.layout.Components { for _, c := range h.layout.Components {
settings = append(settings, settings = append(settings,
config.ComponentSettings{Type: c.Type, Title: c.Title, Size: c.Size, Position: c.Position}) config.ComponentSettings{Type: c.Type, Title: c.Title, Size: c.Size, Location: c.Location})
} }
config.Update(settings) config.Update(settings)
} }