From 7334efa9ff61116427effa24f14db4d956b9fb41 Mon Sep 17 00:00:00 2001 From: sqshq Date: Fri, 26 Apr 2019 01:11:40 -0400 Subject: [PATCH] added auto arrangement, so user can add new components without specified position --- component/layout/layout.go | 15 +++--- config/arrangement.go | 108 +++++++++++++++++++++++++++++++++++++ config/component.go | 9 ++++ config/default.go | 6 +-- console/console.go | 2 + 5 files changed, 129 insertions(+), 11 deletions(-) create mode 100644 config/arrangement.go diff --git a/component/layout/layout.go b/component/layout/layout.go index f79cce9..1c1fdbc 100644 --- a/component/layout/layout.go +++ b/component/layout/layout.go @@ -34,8 +34,6 @@ const ( ) const ( - columnsCount = 80 - rowsCount = 40 minDimension = 3 statusbarHeight = 1 ) @@ -132,6 +130,11 @@ func (l *Layout) HandleKeyboardEvent(e string) { case ModeMenuOptionSelect: l.menu.Idle() l.changeMode(ModeDefault) + case ModeComponentMove: + fallthrough + case ModeComponentResize: + l.menu.Idle() + l.changeMode(ModeDefault) } case console.KeyLeft: switch l.mode { @@ -273,8 +276,8 @@ func (l *Layout) moveSelection(direction string) { func (l *Layout) Draw(buffer *ui.Buffer) { - columnWidth := float64(l.GetRect().Dx()) / float64(columnsCount) - rowHeight := float64(l.GetRect().Dy()-statusbarHeight) / float64(rowsCount) + columnWidth := float64(l.GetRect().Dx()) / float64(console.ColumnsCount) + rowHeight := float64(l.GetRect().Dy()-statusbarHeight) / float64(console.RowsCount) for _, c := range l.Components { rectangle := calculateComponentCoordinates(c, columnWidth, rowHeight) @@ -292,8 +295,8 @@ func (l *Layout) Draw(buffer *ui.Buffer) { func (l *Layout) findComponentAtPoint(point image.Point) (*component.Component, int) { - columnWidth := float64(l.GetRect().Dx()) / float64(columnsCount) - rowHeight := float64(l.GetRect().Dy()-statusbarHeight) / float64(rowsCount) + columnWidth := float64(l.GetRect().Dx()) / float64(console.ColumnsCount) + rowHeight := float64(l.GetRect().Dy()-statusbarHeight) / float64(console.RowsCount) for i, c := range l.Components { diff --git a/config/arrangement.go b/config/arrangement.go new file mode 100644 index 0000000..d52d2a2 --- /dev/null +++ b/config/arrangement.go @@ -0,0 +1,108 @@ +package config + +import ( + "github.com/sqshq/sampler/console" +) + +func (c *Config) setDefaultArrangement() { + + components := getComponents(c) + + if allHaveNoPosition(components) { + setSingleComponentPosition(components[0]) + } + + for _, component := range components { + if component.Position == nil { + + lc := getLargestComponent(components) + lr := lc.GetRectangle() + + var w, h, ws, hs int + + if lr.Dx()/2 > lr.Dy() { + w = lr.Dx() / 2 + h = lr.Dy() + ws = w + hs = 0 + } else { + w = lr.Dx() + h = lr.Dy() / 2 + ws = 0 + hs = h + } + + component.Position = [][]int{ + {lr.Min.X + ws, lr.Min.Y + hs}, + {w, h}, + } + + lc.Position = [][]int{ + {lr.Min.X, lr.Min.Y}, + {w, h}, + } + } + } +} + +func allHaveNoPosition(components []*ComponentConfig) bool { + for _, component := range components { + if component.Position != nil { + return false + } + } + return true +} + +func getLargestComponent(components []*ComponentConfig) *ComponentConfig { + + largestComponent := components[0] + + for _, component := range components { + if getSquare(component) > getSquare(largestComponent) { + largestComponent = component + } + } + + return largestComponent +} + +func getSquare(c *ComponentConfig) int { + r := c.GetRectangle() + return r.Dx() * r.Dy() +} + +func getComponents(c *Config) []*ComponentConfig { + + var components []*ComponentConfig + + for i := range c.RunCharts { + components = append(components, &c.RunCharts[i].ComponentConfig) + } + for i := range c.BarCharts { + components = append(components, &c.BarCharts[i].ComponentConfig) + } + for i := range c.Gauges { + components = append(components, &c.Gauges[i].ComponentConfig) + } + for i := range c.SparkLines { + components = append(components, &c.SparkLines[i].ComponentConfig) + } + for i := range c.AsciiBoxes { + components = append(components, &c.AsciiBoxes[i].ComponentConfig) + } + for i := range c.TextBoxes { + components = append(components, &c.TextBoxes[i].ComponentConfig) + } + + return components +} + +func setSingleComponentPosition(c *ComponentConfig) { + w := int(console.ColumnsCount * 0.8) + h := int(console.RowsCount * 0.7) + c.Position = [][]int{ + {(console.ColumnsCount - w) / 2, (console.RowsCount - h) / 2}, + {w, h}, + } +} diff --git a/config/component.go b/config/component.go index 2e19aa8..6fbd171 100644 --- a/config/component.go +++ b/config/component.go @@ -3,6 +3,7 @@ package config import ( ui "github.com/gizak/termui/v3" "github.com/sqshq/sampler/console" + "image" ) type ComponentType rune @@ -32,6 +33,14 @@ func (c *ComponentConfig) GetSize() Size { return Size{X: c.Position[1][0], Y: c.Position[1][1]} } +func (c *ComponentConfig) GetRectangle() image.Rectangle { + if c.Position == nil { + return image.ZR + } else { + return image.Rect(c.Position[0][0], c.Position[0][1], c.Position[0][0]+c.Position[1][0], c.Position[0][1]+c.Position[1][1]) + } +} + type TriggerConfig struct { Title string `yaml:"title"` Condition string `yaml:"condition"` diff --git a/config/default.go b/config/default.go index 59aba40..be4fa8c 100644 --- a/config/default.go +++ b/config/default.go @@ -13,7 +13,7 @@ const ( func (c *Config) setDefaults() { c.setDefaultValues() c.setDefaultColors() - c.setDefaultLayout() + c.setDefaultArrangement() } func (c *Config) setDefaultValues() { @@ -173,10 +173,6 @@ func setDefaultTriggersValues(triggers []TriggerConfig) { } } -func (c *Config) setDefaultLayout() { - // TODO auto-arrange components -} - func (c *Config) setDefaultColors() { palette := console.GetPalette(*c.Theme) diff --git a/console/console.go b/console/console.go index ea9b919..9d7ab29 100644 --- a/console/console.go +++ b/console/console.go @@ -10,6 +10,8 @@ import ( const ( MaxRenderInterval = 1000 * time.Millisecond MinRenderInterval = 100 * time.Millisecond + ColumnsCount = 80 + RowsCount = 40 AppTitle = "sampler" AppVersion = "0.9.0" )