add move/resize limitation for X and Y axes

This commit is contained in:
sqshq 2019-02-10 22:26:51 -05:00
parent b9cc09c1e4
commit f8a641d22c
7 changed files with 82 additions and 55 deletions

View File

@ -1,14 +0,0 @@
package console
const (
KeyPause = "p"
KeyQuit = "q"
KeyResize = "<Resize>"
KeyExit = "<C-c>"
KeyLeft = "<Left>"
KeyRight = "<Right>"
KeyUp = "<Up>"
KeyDown = "<Down>"
KeyEnter = "<Enter>"
KeyEsc = "<Escape>"
)

17
console/signal.go Normal file
View File

@ -0,0 +1,17 @@
package console
const (
SignalResize = "<Resize>"
)
const (
KeyPause = "p"
KeyQuit = "q"
KeyExit = "<C-c>"
KeyLeft = "<Left>"
KeyRight = "<Right>"
KeyUp = "<Up>"
KeyDown = "<Down>"
KeyEnter = "<Enter>"
KeyEsc = "<Escape>"
)

View File

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

10
main.go
View File

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

View File

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

View File

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

View File

@ -116,38 +116,47 @@ func (m *Menu) renderHighlight(buffer *ui.Buffer) {
m.printAllDirectionsArrowSign(buffer, -2)
arrowsText := "Use arrows for selection"
arrowsTextPoint := getMiddlePoint(m.Block, arrowsText, 2)
if arrowsTextPoint.In(m.Rectangle) {
buffer.SetString(
arrowsText,
ui.NewStyle(console.ColorDarkGrey),
getMiddlePoint(m.Block, arrowsText, 2),
arrowsTextPoint,
)
}
optionsText := "<ENTER> to view options"
optionsTextPoint := getMiddlePoint(m.Block, optionsText, 3)
if optionsTextPoint.In(m.Rectangle) {
buffer.SetString(
optionsText,
ui.NewStyle(console.ColorDarkGrey),
getMiddlePoint(m.Block, optionsText, 3),
)
}
resumeText := "<ESC> to resume"
resumeTextPoint := getMiddlePoint(m.Block, resumeText, 4)
if resumeTextPoint.In(m.Rectangle) {
buffer.SetString(
resumeText,
ui.NewStyle(console.ColorDarkGrey),
getMiddlePoint(m.Block, resumeText, 4),
resumeTextPoint,
)
}
}
func (m *Menu) renderMoveAndResize(buffer *ui.Buffer) {
m.printAllDirectionsArrowSign(buffer, -2)
saveText := "<ENTER> 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,
)
}
}