diff --git a/config.yml b/config.yml index 74ac734..f31c5a8 100644 --- a/config.yml +++ b/config.yml @@ -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 diff --git a/config/linechart.go b/config/linechart.go index 95df044..99366c5 100644 --- a/config/linechart.go +++ b/config/linechart.go @@ -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"` } diff --git a/main.go b/main.go index e92aaf6..f4c9f7d 100644 --- a/main.go +++ b/main.go @@ -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", "": // press 'q' or 'C-c' to quit return + case "": + payload := e.Payload.(ui.Resize) + layout.ChangeDimensions(payload.Width, payload.Height) } //case "": // 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 "": - 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 "": - 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 "": - 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 "": - 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) } } } diff --git a/widgets/geom.go b/widgets/geometry.go similarity index 100% rename from widgets/geom.go rename to widgets/geometry.go diff --git a/widgets/layout.go b/widgets/layout.go new file mode 100644 index 0000000..3d909a4 --- /dev/null +++ b/widgets/layout.go @@ -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) + } +} diff --git a/widgets/timeplot.go b/widgets/timeplot.go index 86f1f77..d7a29ad 100644 --- a/widgets/timeplot.go +++ b/widgets/timeplot.go @@ -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 (