added layout
This commit is contained in:
parent
38128270b2
commit
34a3c0845d
|
@ -9,14 +9,14 @@ line-charts:
|
||||||
script: curl -o /dev/null -s -w '%{time_total}' http://google.com
|
script: curl -o /dev/null -s -w '%{time_total}' http://google.com
|
||||||
color: yellow
|
color: yellow
|
||||||
refresh-rate-ms: 100
|
refresh-rate-ms: 100
|
||||||
|
time-scale-sec: 1
|
||||||
style: dots/lines
|
style: dots/lines
|
||||||
scale: log
|
|
||||||
position:
|
position:
|
||||||
x: 0
|
x: 1
|
||||||
y: 0
|
y: 2
|
||||||
size:
|
size:
|
||||||
x: 3
|
x: 3
|
||||||
y: 2
|
y: 4
|
||||||
- title: mongo-count
|
- title: mongo-count
|
||||||
data:
|
data:
|
||||||
- label: posts
|
- label: posts
|
||||||
|
|
|
@ -4,6 +4,7 @@ type LineChartConfig struct {
|
||||||
Title string `yaml:"title"`
|
Title string `yaml:"title"`
|
||||||
Data []Data `yaml:"data"`
|
Data []Data `yaml:"data"`
|
||||||
Position Position `yaml:"position"`
|
Position Position `yaml:"position"`
|
||||||
|
Size Size `yaml:"size"`
|
||||||
RefreshRateMs int `yaml:"refresh-rate-ms"`
|
RefreshRateMs int `yaml:"refresh-rate-ms"`
|
||||||
Scale string `yaml:"scale"`
|
Scale string `yaml:"scale"`
|
||||||
}
|
}
|
||||||
|
|
41
main.go
41
main.go
|
@ -21,10 +21,14 @@ func main() {
|
||||||
|
|
||||||
p1 := widgets.NewTimePlot()
|
p1 := widgets.NewTimePlot()
|
||||||
p1.Title = " CURL LATENCY STATISTICS (sec) "
|
p1.Title = " CURL LATENCY STATISTICS (sec) "
|
||||||
p1.SetRect(0, 20, 148, 40)
|
|
||||||
p1.LineColors[0] = ui.ColorYellow
|
p1.LineColors[0] = ui.ColorYellow
|
||||||
p1.Marker = widgets.MarkerBraille
|
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 {
|
if err := ui.Init(); err != nil {
|
||||||
//log.Fatalf("failed to initialize termui: %v", err)
|
//log.Fatalf("failed to initialize termui: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -32,6 +36,10 @@ func main() {
|
||||||
defer ui.Close()
|
defer ui.Close()
|
||||||
uiEvents := ui.PollEvents()
|
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)
|
dataTicker := time.NewTicker(200 * time.Millisecond)
|
||||||
uiTicker := time.NewTicker(50 * time.Millisecond)
|
uiTicker := time.NewTicker(50 * time.Millisecond)
|
||||||
|
|
||||||
|
@ -48,6 +56,7 @@ func main() {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
p1.AddValue(value)
|
p1.AddValue(value)
|
||||||
|
p2.AddValue(value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -59,6 +68,9 @@ func main() {
|
||||||
switch e.ID {
|
switch e.ID {
|
||||||
case "q", "<C-c>": // press 'q' or 'C-c' to quit
|
case "q", "<C-c>": // press 'q' or 'C-c' to quit
|
||||||
return
|
return
|
||||||
|
case "<Resize>":
|
||||||
|
payload := e.Payload.(ui.Resize)
|
||||||
|
layout.ChangeDimensions(payload.Width, payload.Height)
|
||||||
}
|
}
|
||||||
//case "<MouseLeft>":
|
//case "<MouseLeft>":
|
||||||
// payload := e.Payload.(ui.Mouse)
|
// payload := e.Payload.(ui.Mouse)
|
||||||
|
@ -69,38 +81,21 @@ func main() {
|
||||||
case ui.KeyboardEvent: // handle all key presses
|
case ui.KeyboardEvent: // handle all key presses
|
||||||
//log.Printf("key: %v", e.ID)
|
//log.Printf("key: %v", e.ID)
|
||||||
switch e.ID {
|
switch e.ID {
|
||||||
// TODO refactor + control moving out of range
|
|
||||||
case "<Left>":
|
case "<Left>":
|
||||||
rect := p1.GetRect()
|
layout.MoveItem(-1, 0)
|
||||||
min := rect.Min
|
|
||||||
max := rect.Max
|
|
||||||
p1.SetRect(min.X-1, min.Y, max.X-1, max.Y)
|
|
||||||
ui.Clear()
|
|
||||||
case "<Right>":
|
case "<Right>":
|
||||||
rect := p1.GetRect()
|
layout.MoveItem(1, 0)
|
||||||
min := rect.Min
|
|
||||||
max := rect.Max
|
|
||||||
p1.SetRect(min.X+1, min.Y, max.X+1, max.Y)
|
|
||||||
ui.Clear()
|
|
||||||
case "<Down>":
|
case "<Down>":
|
||||||
rect := p1.GetRect()
|
layout.MoveItem(0, 1)
|
||||||
min := rect.Min
|
|
||||||
max := rect.Max
|
|
||||||
p1.SetRect(min.X, min.Y+1, max.X, max.Y+1)
|
|
||||||
ui.Clear()
|
|
||||||
case "<Up>":
|
case "<Up>":
|
||||||
rect := p1.GetRect()
|
layout.MoveItem(0, -1)
|
||||||
min := rect.Min
|
|
||||||
max := rect.Max
|
|
||||||
p1.SetRect(min.X, min.Y-1, max.X, max.Y-1)
|
|
||||||
ui.Clear()
|
|
||||||
case "p":
|
case "p":
|
||||||
pause = !pause
|
pause = !pause
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case <-uiTicker.C:
|
case <-uiTicker.C:
|
||||||
if !pause {
|
if !pause {
|
||||||
ui.Render(p1)
|
ui.Render(layout)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
|
@ -18,6 +18,7 @@ type TimePlot struct {
|
||||||
DotRune rune
|
DotRune rune
|
||||||
HorizontalScale int
|
HorizontalScale int
|
||||||
Marker PlotMarker
|
Marker PlotMarker
|
||||||
|
|
||||||
timePoints []TimePoint
|
timePoints []TimePoint
|
||||||
dataMutex *sync.Mutex
|
dataMutex *sync.Mutex
|
||||||
grid PlotGrid
|
grid PlotGrid
|
||||||
|
|
Loading…
Reference in New Issue