Skip to content

Commit

Permalink
Make arguments for GUI struct
Browse files Browse the repository at this point in the history
  • Loading branch information
nakabonne committed Oct 31, 2020
1 parent 82a0e39 commit 97c565d
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 8 deletions.
3 changes: 2 additions & 1 deletion diagnoser/diagnoser.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ func Run(addr *net.TCPAddr, scrapeInterval time.Duration) error {
if err != nil {
return err
}
return gui.Run(ctx, scrapeInterval, meta, statsCh)
g := gui.NewGUI(scrapeInterval, cancel, statsCh, meta)
return g.Run(ctx)
}

func startScraping(ctx context.Context, addr *net.TCPAddr, interval time.Duration, statsCh chan<- *stats.Stats) (*stats.Meta, error) {
Expand Down
4 changes: 4 additions & 0 deletions diagnoser/gui/drawer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package gui

type drawer struct {
}
70 changes: 63 additions & 7 deletions diagnoser/gui/gui.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package gui provides an ability to draw charts on the terminal.
package gui

import (
Expand All @@ -16,15 +17,38 @@ import (
)

const (
// How often termdash redraws the screen.
defaultRedrawInterval = time.Second
rootID = "root"
)

type runner func(ctx context.Context, t terminalapi.Terminal, c *container.Container, opts ...termdash.Option) error
type GUI struct {
// How often termdash redraws the screen.
RedrawInterval time.Duration
// The function to quit the application.
Cancel context.CancelFunc
// A channel for receiving data sources to draw on the chart.
StatsCh <-chan *stats.Stats
// Metadata of the process where the agent runs on.
Metadata stats.Meta
}

// Run stats drawing charts, and blocks until the quit operation is performed.
func Run(ctx context.Context, redrawIntarval time.Duration, meta *stats.Meta, statsCh <-chan *stats.Stats) error {
func NewGUI(redrawInterval time.Duration, cancel context.CancelFunc, statsCh <-chan *stats.Stats, metadata *stats.Meta) *GUI {
if redrawInterval == 0 {
redrawInterval = defaultRedrawInterval
}
if statsCh == nil {
statsCh = make(<-chan *stats.Stats)
}
return &GUI{
RedrawInterval: redrawInterval,
Cancel: cancel,
StatsCh: statsCh,
Metadata: *metadata,
}
}

// Run starts drawing charts, and blocks until the quit operation is performed.
func (g *GUI) Run(ctx context.Context) error {
var (
t terminalapi.Terminal
err error
Expand All @@ -38,9 +62,41 @@ func Run(ctx context.Context, redrawIntarval time.Duration, meta *stats.Meta, st
return fmt.Errorf("failed to generate terminal interface: %w", err)
}
defer t.Close()
return run(ctx, t, termdash.Run)
return g.run(ctx, t, termdash.Run)
}

type runner func(ctx context.Context, t terminalapi.Terminal, c *container.Container, opts ...termdash.Option) error

func (g *GUI) run(ctx context.Context, t terminalapi.Terminal, r runner) error {
c, err := container.New(t, container.ID(rootID))
if err != nil {
return fmt.Errorf("failed to generate container: %w", err)
}

w, err := newWidgets()
if err != nil {
return fmt.Errorf("failed to generate widgets: %w", err)
}

gridOpts, err := gridLayout(w)
if err != nil {
return fmt.Errorf("failed to build grid layout: %w", err)
}

if err := c.Update(rootID, gridOpts.base...); err != nil {
return fmt.Errorf("failed to update container: %w", err)
}
k := keybinds(ctx, g.Cancel)

return r(ctx, t, c, termdash.KeyboardSubscriber(k), termdash.RedrawInterval(g.RedrawInterval))
}

// gridOpts holds all options in our grid. It basically holds the container
// options (column, width, padding, etc) of our widgets.
type gridOpts struct {
base []container.Option
}

func run(ctx context.Context, t terminalapi.Terminal, r runner) error {
return nil
func gridLayout(w *widgets) (*gridOpts, error) {
return nil, nil
}
11 changes: 11 additions & 0 deletions diagnoser/gui/keybinds.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package gui

import (
"context"

"github.com/mum4k/termdash/terminal/terminalapi"
)

func keybinds(ctx context.Context, cancel context.CancelFunc) func(*terminalapi.Keyboard) {
return nil
}

0 comments on commit 97c565d

Please sign in to comment.