diff --git a/component/component.go b/component/component.go index fbdd3e9..5485ee9 100644 --- a/component/component.go +++ b/component/component.go @@ -11,7 +11,7 @@ type Component struct { *data.Consumer Type config.ComponentType Title string - Position config.Position + Location config.Location Size config.Size RefreshRateMs int } @@ -22,15 +22,15 @@ func NewComponent(dbl ui.Drawable, cmr *data.Consumer, cfg config.ComponentConfi Consumer: cmr, Type: cfg.Type, Title: cfg.Title, - Position: cfg.Position, - Size: cfg.Size, + Location: cfg.GetLocation(), + Size: cfg.GetSize(), RefreshRateMs: *cfg.RefreshRateMs, } } func (c *Component) Move(x, y int) { - c.Position.X += x - c.Position.Y += y + c.Location.X += x + c.Location.Y += y c.normalize() } @@ -41,10 +41,10 @@ func (c *Component) Resize(x, y int) { } func (c *Component) normalize() { - if c.Position.X < 0 { - c.Position.X = 0 + if c.Location.X < 0 { + c.Location.X = 0 } - if c.Position.Y < 0 { - c.Position.Y = 0 + if c.Location.Y < 0 { + c.Location.Y = 0 } } diff --git a/component/layout/layout.go b/component/layout/layout.go index c7da540..d5958a6 100644 --- a/component/layout/layout.go +++ b/component/layout/layout.go @@ -250,8 +250,8 @@ func (l *Layout) Draw(buffer *ui.Buffer) { for _, c := range l.Components { - x1 := math.Floor(float64(c.Position.X) * columnWidth) - y1 := math.Floor(float64(c.Position.Y) * rowHeight) + x1 := math.Floor(float64(c.Location.X) * columnWidth) + y1 := math.Floor(float64(c.Location.Y) * rowHeight) x2 := x1 + math.Floor(float64(c.Size.X))*columnWidth y2 := y1 + math.Floor(float64(c.Size.Y))*rowHeight diff --git a/config.yml b/config.yml index c992266..1f76807 100644 --- a/config.yml +++ b/config.yml @@ -1,11 +1,6 @@ runcharts: - title: SEARCH ENGINE RESPONSE TIME (sec) - position: - w: 0 - h: 0 - size: - w: 53 - h: 16 + position: [[0, 0], [53, 16]] triggers: - title: Latency threshold exceeded condition: echo "$prev < 0.35 && $cur > 0.35" |bc -l @@ -23,12 +18,7 @@ runcharts: - label: BING value: curl -o /dev/null -s -w '%{time_total}' https://www.bing.com - title: MONGO COLLECTIONS COUNT - position: - w: 53 - h: 0 - size: - w: 27 - h: 8 + position: [[53, 0], [27, 8]] legend: enabled: true details: false @@ -41,12 +31,7 @@ runcharts: barcharts: - title: EVENTS BY STATUS refresh-rate-ms: 1000 - position: - w: 0 - h: 17 - size: - w: 53 - h: 12 + position: [[0, 17], [53, 12]] scale: 0 items: - label: NEW @@ -61,45 +46,25 @@ barcharts: value: mongo --quiet --host=localhost blog --eval "db.getCollection('posts').find({status:'INACTIVE'}).itcount()" gauges: - title: YEAR PROGRESS - position: - w: 53 - h: 8 - size: - w: 27 - h: 2 + position: [[53, 8], [27, 2]] values: cur: date +%j max: echo 365 min: echo 0 - title: DAY PROGRESS - position: - w: 53 - h: 10 - size: - w: 27 - h: 2 + position: [[53, 10], [27, 2]] values: cur: date +%H max: echo 24 min: echo 0 - title: HOUR PROGRESS - position: - w: 53 - h: 12 - size: - w: 27 - h: 2 + position: [[53, 12], [27, 2]] values: cur: date +%M max: echo 60 min: echo 0 - title: MINUTE PROGRESS - position: - w: 53 - h: 14 - size: - w: 27 - h: 2 + position: [[53, 14], [27, 2]] triggers: - title: CLOCK BELL EVERY MINUTE condition: '[ $label == "cur" ] && [ $cur -eq 0 ] && echo 1 || echo 0' @@ -112,19 +77,9 @@ gauges: min: echo 0 asciiboxes: - title: LOCAL TIME - position: - w: 53 - h: 17 - size: - w: 27 - h: 5 + position: [[53, 17], [27, 5]] value: date +%r - title: UTC TIME - position: - w: 53 - h: 22 - size: - w: 27 - h: 7 + position: [[53, 22], [27, 7]] value: env TZ=UTC date +%r font: 3d diff --git a/config/component.go b/config/component.go index 68577c1..1e06aa9 100644 --- a/config/component.go +++ b/config/component.go @@ -19,12 +19,19 @@ const ( type ComponentConfig struct { Title string `yaml:"title"` RefreshRateMs *int `yaml:"refresh-rate-ms,omitempty"` - Position Position `yaml:"position"` - Size Size `yaml:"size"` + Position [][]int `yaml:"position,flow"` Triggers []TriggerConfig `yaml:"triggers,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 { Title string `yaml:"title"` Condition string `yaml:"condition"` @@ -70,25 +77,32 @@ type LegendConfig struct { 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 { Label *string `yaml:"label,omitempty"` Script string `yaml:"value"` Color *ui.Color `yaml:"color,omitempty"` } +type Location struct { + X int + Y int +} + +type Size struct { + X int + Y int +} + type ComponentSettings struct { Type ComponentType Title string Size Size - Position Position + Location Location +} + +func getPosition(location Location, size Size) [][]int { + return [][]int{ + {location.X, location.Y}, + {size.X, size.Y}, + } } diff --git a/config/config.go b/config/config.go index ce64c29..0d410b9 100644 --- a/config/config.go +++ b/config/config.go @@ -42,8 +42,7 @@ func Update(settings []ComponentSettings) { cfg := readFile(os.Args[1]) for _, s := range settings { componentConfig := cfg.findComponent(s.Type, s.Title) - componentConfig.Size = s.Size - componentConfig.Position = s.Position + componentConfig.Position = getPosition(s.Location, s.Size) } saveFile(cfg) } diff --git a/event/handler.go b/event/handler.go index b5f4c6f..deeb8e8 100644 --- a/event/handler.go +++ b/event/handler.go @@ -75,7 +75,7 @@ func (h *Handler) handleExit() { var settings []config.ComponentSettings for _, c := range h.layout.Components { 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) }