From f8a641d22cdacdb8495658d9d07371b94a801432 Mon Sep 17 00:00:00 2001 From: sqshq Date: Sun, 10 Feb 2019 22:26:51 -0500 Subject: [PATCH] add move/resize limitation for X and Y axes --- console/key.go | 14 -------------- console/signal.go | 17 +++++++++++++++++ event/handler.go | 4 +--- main.go | 10 +++++----- widgets/component.go | 29 ++++++++++++++++++++++------ widgets/layout.go | 18 +++++++++--------- widgets/menu.go | 45 ++++++++++++++++++++++++++------------------ 7 files changed, 82 insertions(+), 55 deletions(-) delete mode 100644 console/key.go create mode 100644 console/signal.go diff --git a/console/key.go b/console/key.go deleted file mode 100644 index 95ac467..0000000 --- a/console/key.go +++ /dev/null @@ -1,14 +0,0 @@ -package console - -const ( - KeyPause = "p" - KeyQuit = "q" - KeyResize = "" - KeyExit = "" - KeyLeft = "" - KeyRight = "" - KeyUp = "" - KeyDown = "" - KeyEnter = "" - KeyEsc = "" -) diff --git a/console/signal.go b/console/signal.go new file mode 100644 index 0000000..b6fd8a5 --- /dev/null +++ b/console/signal.go @@ -0,0 +1,17 @@ +package console + +const ( + SignalResize = "" +) + +const ( + KeyPause = "p" + KeyQuit = "q" + KeyExit = "" + KeyLeft = "" + KeyRight = "" + KeyUp = "" + KeyDown = "" + KeyEnter = "" + KeyEsc = "" +) diff --git a/event/handler.go b/event/handler.go index ce6ca83..2c88601 100644 --- a/event/handler.go +++ b/event/handler.go @@ -29,11 +29,9 @@ func (self *Handler) HandleEvents() { return case console.KeyPause: pause = !pause - case console.KeyResize: + case console.SignalResize: payload := e.Payload.(ui.Resize) self.Layout.ChangeDimensions(payload.Width, payload.Height) - //case "a": - // self.Layout.GetComponent(0).DisableSelection() default: self.Layout.HandleConsoleEvent(e.ID) } diff --git a/main.go b/main.go index 4242660..d563670 100644 --- a/main.go +++ b/main.go @@ -20,13 +20,13 @@ func main() { width, height := ui.TerminalDimensions() layout := widgets.NewLayout(width, height, widgets.NewMenu()) - for _, chartConfig := range cfg.RunCharts { + for _, c := range cfg.RunCharts { - chart := widgets.NewRunChart(chartConfig.Title, chartConfig.Precision, chartConfig.RefreshRateMs) - layout.AddComponent(chart, chartConfig.Title, chartConfig.Position, chartConfig.Size, widgets.TypeRunChart) + chart := widgets.NewRunChart(c.Title, c.Precision, c.RefreshRateMs) + layout.AddComponent(chart, c.Title, c.Position, c.Size, widgets.TypeRunChart) - for _, item := range chartConfig.Items { - data.NewSampler(chart, item, chartConfig.RefreshRateMs) + for _, item := range c.Items { + data.NewSampler(chart, item, c.RefreshRateMs) } } diff --git a/widgets/component.go b/widgets/component.go index 2ddcfa5..41c5135 100644 --- a/widgets/component.go +++ b/widgets/component.go @@ -29,12 +29,29 @@ type Size struct { Y int `yaml:"y"` } -func (self *Component) Move(x, y int) { - self.Position.X += x - self.Position.Y += y +func (c *Component) Move(x, y int) { + c.Position.X += x + c.Position.Y += y + c.normalize() } -func (self *Component) Resize(x, y int) { - self.Size.X += x - self.Size.Y += y +func (c *Component) Resize(x, y int) { + c.Size.X += x + c.Size.Y += y + c.normalize() +} + +func (c *Component) normalize() { + if c.Size.X < 0 { + c.Size.X = 0 + } + if c.Size.Y < 0 { + c.Size.Y = 0 + } + if c.Position.X < 0 { + c.Position.X = 0 + } + if c.Position.Y < 0 { + c.Position.Y = 0 + } } diff --git a/widgets/layout.go b/widgets/layout.go index e22d7f0..7c9dd62 100644 --- a/widgets/layout.go +++ b/widgets/layout.go @@ -60,15 +60,6 @@ func (l *Layout) GetComponents(Type ComponentType) []ui.Drawable { return components } -// TODO func to get prev/next component navigating left/right/top/bottom -func (l *Layout) getComponent(i int) Component { - return l.components[i] -} - -func (l *Layout) getSelectedComponent() *Component { - return &l.components[l.selection] -} - func (l *Layout) HandleConsoleEvent(e string) { switch e { case console.KeyEnter: @@ -175,6 +166,15 @@ func (l *Layout) ChangeDimensions(width, height int) { l.SetRect(0, 0, width, height) } +// TODO func to get prev/next component navigating left/right/top/bottom +func (l *Layout) getComponent(i int) Component { + return l.components[i] +} + +func (l *Layout) getSelectedComponent() *Component { + return &l.components[l.selection] +} + func (l *Layout) Draw(buffer *ui.Buffer) { columnWidth := float64(l.GetRect().Dx()) / columnsCount diff --git a/widgets/menu.go b/widgets/menu.go index 1361186..d2d5245 100644 --- a/widgets/menu.go +++ b/widgets/menu.go @@ -116,25 +116,34 @@ func (m *Menu) renderHighlight(buffer *ui.Buffer) { m.printAllDirectionsArrowSign(buffer, -2) arrowsText := "Use arrows for selection" - buffer.SetString( - arrowsText, - ui.NewStyle(console.ColorDarkGrey), - getMiddlePoint(m.Block, arrowsText, 2), - ) + arrowsTextPoint := getMiddlePoint(m.Block, arrowsText, 2) + if arrowsTextPoint.In(m.Rectangle) { + buffer.SetString( + arrowsText, + ui.NewStyle(console.ColorDarkGrey), + arrowsTextPoint, + ) + } optionsText := " to view options" - buffer.SetString( - optionsText, - ui.NewStyle(console.ColorDarkGrey), - getMiddlePoint(m.Block, optionsText, 3), - ) + optionsTextPoint := getMiddlePoint(m.Block, optionsText, 3) + if optionsTextPoint.In(m.Rectangle) { + buffer.SetString( + optionsText, + ui.NewStyle(console.ColorDarkGrey), + getMiddlePoint(m.Block, optionsText, 3), + ) + } resumeText := " to resume" - buffer.SetString( - resumeText, - ui.NewStyle(console.ColorDarkGrey), - getMiddlePoint(m.Block, resumeText, 4), - ) + resumeTextPoint := getMiddlePoint(m.Block, resumeText, 4) + if resumeTextPoint.In(m.Rectangle) { + buffer.SetString( + resumeText, + ui.NewStyle(console.ColorDarkGrey), + resumeTextPoint, + ) + } } func (m *Menu) renderMoveAndResize(buffer *ui.Buffer) { @@ -142,12 +151,12 @@ func (m *Menu) renderMoveAndResize(buffer *ui.Buffer) { m.printAllDirectionsArrowSign(buffer, -2) saveText := " to save changes" - textPoint := getMiddlePoint(m.Block, saveText, 4) - if textPoint.In(m.Rectangle) { + saveTextPoint := getMiddlePoint(m.Block, saveText, 4) + if saveTextPoint.In(m.Rectangle) { buffer.SetString( saveText, ui.NewStyle(console.ColorDarkGrey), - textPoint, + saveTextPoint, ) } }