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
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
}
}

View File

@ -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

View File

@ -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

View File

@ -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},
}
}

View File

@ -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)
}

View File

@ -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)
}