added layout

This commit is contained in:
sqshq 2019-01-29 19:21:57 -05:00
parent 38128270b2
commit 34a3c0845d
6 changed files with 108 additions and 30 deletions

View File

@ -9,14 +9,14 @@ line-charts:
script: curl -o /dev/null -s -w '%{time_total}' http://google.com
color: yellow
refresh-rate-ms: 100
time-scale-sec: 1
style: dots/lines
scale: log
position:
x: 0
y: 0
x: 1
y: 2
size:
x: 3
y: 2
y: 4
- title: mongo-count
data:
- label: posts

View File

@ -4,6 +4,7 @@ type LineChartConfig struct {
Title string `yaml:"title"`
Data []Data `yaml:"data"`
Position Position `yaml:"position"`
Size Size `yaml:"size"`
RefreshRateMs int `yaml:"refresh-rate-ms"`
Scale string `yaml:"scale"`
}

41
main.go
View File

@ -21,10 +21,14 @@ func main() {
p1 := widgets.NewTimePlot()
p1.Title = " CURL LATENCY STATISTICS (sec) "
p1.SetRect(0, 20, 148, 40)
p1.LineColors[0] = ui.ColorYellow
p1.Marker = widgets.MarkerBraille
p2 := widgets.NewTimePlot()
p2.Title = " CURL LATENCY STATISTICS 2 (sec) "
p2.LineColors[0] = ui.ColorYellow
p2.Marker = widgets.MarkerBraille
if err := ui.Init(); err != nil {
//log.Fatalf("failed to initialize termui: %v", err)
}
@ -32,6 +36,10 @@ func main() {
defer ui.Close()
uiEvents := ui.PollEvents()
layout := widgets.NewLayout(ui.TerminalDimensions())
layout.AddItem(p1, 0, 0, 6, 6)
layout.AddItem(p2, 0, 6, 6, 12)
dataTicker := time.NewTicker(200 * time.Millisecond)
uiTicker := time.NewTicker(50 * time.Millisecond)
@ -48,6 +56,7 @@ func main() {
break
}
p1.AddValue(value)
p2.AddValue(value)
}
}
}
@ -59,6 +68,9 @@ func main() {
switch e.ID {
case "q", "<C-c>": // press 'q' or 'C-c' to quit
return
case "<Resize>":
payload := e.Payload.(ui.Resize)
layout.ChangeDimensions(payload.Width, payload.Height)
}
//case "<MouseLeft>":
// payload := e.Payload.(ui.Mouse)
@ -69,38 +81,21 @@ func main() {
case ui.KeyboardEvent: // handle all key presses
//log.Printf("key: %v", e.ID)
switch e.ID {
// TODO refactor + control moving out of range
case "<Left>":
rect := p1.GetRect()
min := rect.Min
max := rect.Max
p1.SetRect(min.X-1, min.Y, max.X-1, max.Y)
ui.Clear()
layout.MoveItem(-1, 0)
case "<Right>":
rect := p1.GetRect()
min := rect.Min
max := rect.Max
p1.SetRect(min.X+1, min.Y, max.X+1, max.Y)
ui.Clear()
layout.MoveItem(1, 0)
case "<Down>":
rect := p1.GetRect()
min := rect.Min
max := rect.Max
p1.SetRect(min.X, min.Y+1, max.X, max.Y+1)
ui.Clear()
layout.MoveItem(0, 1)
case "<Up>":
rect := p1.GetRect()
min := rect.Min
max := rect.Max
p1.SetRect(min.X, min.Y-1, max.X, max.Y-1)
ui.Clear()
layout.MoveItem(0, -1)
case "p":
pause = !pause
}
}
case <-uiTicker.C:
if !pause {
ui.Render(p1)
ui.Render(layout)
}
}
}

81
widgets/layout.go Normal file
View File

@ -0,0 +1,81 @@
package widgets
import (
. "github.com/sqshq/termui"
)
type Item struct {
drawable Drawable
coordinates ItemCoordinates
}
type ItemCoordinates struct {
x1 int
y1 int
x2 int
y2 int
}
type LayoutDimensions struct {
width int
height int
}
type Layout struct {
Block
dimensions LayoutDimensions
items []Item
}
const (
columnsCount = 12
rowsCount = 12
)
func NewLayout(width, height int) *Layout {
b := *NewBlock()
b.SetRect(0, 0, width, height)
return &Layout{
Block: b,
dimensions: LayoutDimensions{width, height},
items: make([]Item, 0),
}
}
func (self *Layout) AddItem(drawable interface{}, x1, y1, x2, y2 int) {
self.items = append(self.items, Item{
drawable: drawable.(Drawable),
coordinates: ItemCoordinates{x1, y1, x2, y2},
})
}
func (self *Layout) MoveItem(x, y int) {
self.items[0].coordinates.x1 += x
self.items[0].coordinates.y1 += y
self.items[0].coordinates.x2 += x
self.items[0].coordinates.y2 += y
}
func (self *Layout) ChangeDimensions(width, height int) {
self.dimensions = LayoutDimensions{width, height}
self.SetRect(0, 0, width, height)
}
func (self *Layout) Draw(buf *Buffer) {
columnWidth := float64(self.dimensions.width) / columnsCount
rowHeight := float64(self.dimensions.height) / rowsCount
for _, item := range self.items {
x1 := float64(item.coordinates.x1) * columnWidth
y1 := float64(item.coordinates.y1) * rowHeight
x2 := float64(item.coordinates.x2) * columnWidth
y2 := float64(item.coordinates.y2) * rowHeight
item.drawable.SetRect(int(x1), int(y1), int(x2), int(y2))
item.drawable.Draw(buf)
}
}

View File

@ -18,9 +18,10 @@ type TimePlot struct {
DotRune rune
HorizontalScale int
Marker PlotMarker
timePoints []TimePoint
dataMutex *sync.Mutex
grid PlotGrid
timePoints []TimePoint
dataMutex *sync.Mutex
grid PlotGrid
}
const (