2018-04-23 21:26:26 +08:00
|
|
|
// Copyright 2018 Google Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-04-23 08:05:54 +08:00
|
|
|
/*
|
|
|
|
Package termdash implements a terminal based dashboard.
|
|
|
|
|
|
|
|
While running, the terminal dashboard performs the following:
|
|
|
|
- Periodic redrawing of the canvas and all the widgets.
|
|
|
|
- Event based redrawing of the widgets (i.e. on Keyboard or Mouse events).
|
|
|
|
- Forwards input events to widgets and optional subscribers.
|
|
|
|
- Handles terminal resize events.
|
|
|
|
*/
|
|
|
|
package termdash
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-06-19 03:13:20 +08:00
|
|
|
"errors"
|
2018-04-23 08:05:54 +08:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/mum4k/termdash/container"
|
2020-04-11 03:26:45 +08:00
|
|
|
"github.com/mum4k/termdash/private/event"
|
2019-02-24 14:27:17 +08:00
|
|
|
"github.com/mum4k/termdash/terminal/terminalapi"
|
2018-04-23 08:05:54 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// DefaultRedrawInterval is the default for the RedrawInterval option.
|
|
|
|
const DefaultRedrawInterval = 250 * time.Millisecond
|
|
|
|
|
|
|
|
// Option is used to provide options.
|
|
|
|
type Option interface {
|
|
|
|
// set sets the provided option.
|
|
|
|
set(td *termdash)
|
|
|
|
}
|
|
|
|
|
|
|
|
// option implements Option.
|
|
|
|
type option func(td *termdash)
|
|
|
|
|
|
|
|
// set implements Option.set.
|
|
|
|
func (o option) set(td *termdash) {
|
|
|
|
o(td)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RedrawInterval sets how often termdash redraws the container and all the widgets.
|
2018-06-19 03:13:20 +08:00
|
|
|
// Defaults to DefaultRedrawInterval. Use the controller to disable the
|
|
|
|
// periodic redraw.
|
2018-04-23 08:05:54 +08:00
|
|
|
func RedrawInterval(t time.Duration) Option {
|
|
|
|
return option(func(td *termdash) {
|
|
|
|
td.redrawInterval = t
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-05-08 04:30:38 +08:00
|
|
|
// ErrorHandler is used to provide a function that will be called with all
|
|
|
|
// errors that occur while the dashboard is running. If not provided, any
|
|
|
|
// errors panic the application.
|
2019-02-25 07:03:45 +08:00
|
|
|
// The provided function must be thread-safe.
|
2018-05-08 04:30:38 +08:00
|
|
|
func ErrorHandler(f func(error)) Option {
|
2018-04-23 08:05:54 +08:00
|
|
|
return option(func(td *termdash) {
|
2018-05-08 04:30:38 +08:00
|
|
|
td.errorHandler = f
|
2018-04-23 08:05:54 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// KeyboardSubscriber registers a subscriber for Keyboard events. Each
|
|
|
|
// keyboard event is forwarded to the container and the registered subscriber.
|
2019-02-22 13:41:51 +08:00
|
|
|
// The provided function must be thread-safe.
|
2018-04-23 08:05:54 +08:00
|
|
|
func KeyboardSubscriber(f func(*terminalapi.Keyboard)) Option {
|
|
|
|
return option(func(td *termdash) {
|
|
|
|
td.keyboardSubscriber = f
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// MouseSubscriber registers a subscriber for Mouse events. Each mouse event
|
|
|
|
// is forwarded to the container and the registered subscriber.
|
2019-02-22 13:41:51 +08:00
|
|
|
// The provided function must be thread-safe.
|
2018-04-23 08:05:54 +08:00
|
|
|
func MouseSubscriber(f func(*terminalapi.Mouse)) Option {
|
|
|
|
return option(func(td *termdash) {
|
|
|
|
td.mouseSubscriber = f
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-03-03 07:24:10 +08:00
|
|
|
// withEDS indicates that termdash should run with the provided event
|
|
|
|
// distribution system instead of creating one.
|
|
|
|
// Useful for tests.
|
|
|
|
func withEDS(eds *event.DistributionSystem) Option {
|
|
|
|
return option(func(td *termdash) {
|
|
|
|
td.eds = eds
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-04-23 08:05:54 +08:00
|
|
|
// Run runs the terminal dashboard with the provided container on the terminal.
|
2018-06-19 03:13:20 +08:00
|
|
|
// Redraws the terminal periodically. If you prefer a manual redraw, use the
|
|
|
|
// Controller instead.
|
2018-04-23 08:05:54 +08:00
|
|
|
// Blocks until the context expires.
|
|
|
|
func Run(ctx context.Context, t terminalapi.Terminal, c *container.Container, opts ...Option) error {
|
|
|
|
td := newTermdash(t, c, opts...)
|
|
|
|
|
2018-07-03 11:05:38 +08:00
|
|
|
err := td.start(ctx)
|
|
|
|
// Only return the status (error or nil) after the termdash event
|
|
|
|
// processing goroutine actually exits.
|
|
|
|
td.stop()
|
|
|
|
return err
|
2018-04-23 08:05:54 +08:00
|
|
|
}
|
|
|
|
|
2018-06-19 03:13:20 +08:00
|
|
|
// Controller controls a termdash instance.
|
|
|
|
// The controller instance is only valid until Close() is called.
|
|
|
|
// The controller is not thread-safe.
|
|
|
|
type Controller struct {
|
|
|
|
td *termdash
|
|
|
|
cancel context.CancelFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewController initializes termdash and returns an instance of the controller.
|
|
|
|
// Periodic redrawing is disabled when using the controller, the RedrawInterval
|
|
|
|
// option is ignored.
|
|
|
|
// Close the controller when it isn't needed anymore.
|
|
|
|
func NewController(t terminalapi.Terminal, c *container.Container, opts ...Option) (*Controller, error) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
ctrl := &Controller{
|
|
|
|
td: newTermdash(t, c, opts...),
|
|
|
|
cancel: cancel,
|
|
|
|
}
|
|
|
|
|
|
|
|
// stops when Close() is called.
|
|
|
|
go ctrl.td.processEvents(ctx)
|
|
|
|
if err := ctrl.td.periodicRedraw(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ctrl, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Redraw triggers redraw of the terminal.
|
|
|
|
func (c *Controller) Redraw() error {
|
|
|
|
if c.td == nil {
|
|
|
|
return errors.New("the termdash instance is no longer running, this controller is now invalid")
|
|
|
|
}
|
|
|
|
|
|
|
|
c.td.mu.Lock()
|
|
|
|
defer c.td.mu.Unlock()
|
|
|
|
return c.td.redraw()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the Controller and its termdash instance.
|
|
|
|
func (c *Controller) Close() {
|
|
|
|
c.cancel()
|
|
|
|
c.td.stop()
|
|
|
|
c.td = nil
|
|
|
|
}
|
|
|
|
|
2018-04-23 08:05:54 +08:00
|
|
|
// termdash is a terminal based dashboard.
|
|
|
|
// This object is thread-safe.
|
|
|
|
type termdash struct {
|
|
|
|
// term is the terminal the dashboard runs on.
|
|
|
|
term terminalapi.Terminal
|
|
|
|
|
|
|
|
// container maintains terminal splits and places widgets.
|
|
|
|
container *container.Container
|
|
|
|
|
2019-02-20 14:18:44 +08:00
|
|
|
// eds distributes input events to subscribers.
|
|
|
|
eds *event.DistributionSystem
|
|
|
|
|
2018-04-23 08:05:54 +08:00
|
|
|
// closeCh gets closed when Stop() is called, which tells the event
|
|
|
|
// collecting goroutine to exit.
|
|
|
|
closeCh chan struct{}
|
|
|
|
// exitCh gets closed when the event collecting goroutine actually exits.
|
|
|
|
exitCh chan struct{}
|
|
|
|
|
|
|
|
// clearNeeded indicates if the terminal needs to be cleared next time
|
|
|
|
// we're drawing it. Terminal needs to be cleared if its sized changed.
|
|
|
|
clearNeeded bool
|
|
|
|
|
|
|
|
// mu protects termdash.
|
|
|
|
mu sync.Mutex
|
|
|
|
|
|
|
|
// Options.
|
|
|
|
redrawInterval time.Duration
|
2018-05-08 04:30:38 +08:00
|
|
|
errorHandler func(error)
|
2018-04-23 08:05:54 +08:00
|
|
|
mouseSubscriber func(*terminalapi.Mouse)
|
|
|
|
keyboardSubscriber func(*terminalapi.Keyboard)
|
|
|
|
}
|
|
|
|
|
|
|
|
// newTermdash creates a new termdash.
|
|
|
|
func newTermdash(t terminalapi.Terminal, c *container.Container, opts ...Option) *termdash {
|
|
|
|
td := &termdash{
|
|
|
|
term: t,
|
|
|
|
container: c,
|
2019-02-20 14:18:44 +08:00
|
|
|
eds: event.NewDistributionSystem(),
|
2018-04-23 08:05:54 +08:00
|
|
|
closeCh: make(chan struct{}),
|
|
|
|
exitCh: make(chan struct{}),
|
|
|
|
redrawInterval: DefaultRedrawInterval,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt.set(td)
|
|
|
|
}
|
2019-02-20 14:18:44 +08:00
|
|
|
td.subscribers()
|
2019-02-20 15:20:11 +08:00
|
|
|
c.Subscribe(td.eds)
|
2018-04-23 08:05:54 +08:00
|
|
|
return td
|
|
|
|
}
|
|
|
|
|
2019-02-20 14:18:44 +08:00
|
|
|
// subscribers subscribes event receivers that live in this package to EDS.
|
|
|
|
func (td *termdash) subscribers() {
|
|
|
|
// Handler for all errors that occur during input event processing.
|
|
|
|
td.eds.Subscribe([]terminalapi.Event{terminalapi.NewError("")}, func(ev terminalapi.Event) {
|
|
|
|
td.handleError(ev.(*terminalapi.Error).Error())
|
|
|
|
})
|
|
|
|
|
|
|
|
// Handles terminal resize events.
|
|
|
|
td.eds.Subscribe([]terminalapi.Event{&terminalapi.Resize{}}, func(terminalapi.Event) {
|
|
|
|
td.setClearNeeded()
|
|
|
|
})
|
|
|
|
|
|
|
|
// Redraws the screen on Keyboard and Mouse events.
|
|
|
|
// These events very likely change the content of the widgets (e.g. zooming
|
|
|
|
// a LineChart) so a redraw is needed to make that visible.
|
2019-02-21 14:20:07 +08:00
|
|
|
td.eds.Subscribe([]terminalapi.Event{
|
|
|
|
&terminalapi.Keyboard{},
|
|
|
|
&terminalapi.Mouse{},
|
|
|
|
}, func(terminalapi.Event) {
|
2019-02-20 15:20:11 +08:00
|
|
|
td.evRedraw()
|
2019-02-21 14:20:07 +08:00
|
|
|
}, event.MaxRepetitive(0)) // No repetitive events that cause terminal redraw.
|
2019-02-20 14:18:44 +08:00
|
|
|
|
|
|
|
// Keyboard and Mouse subscribers specified via options.
|
|
|
|
if td.keyboardSubscriber != nil {
|
|
|
|
td.eds.Subscribe([]terminalapi.Event{&terminalapi.Keyboard{}}, func(ev terminalapi.Event) {
|
|
|
|
td.keyboardSubscriber(ev.(*terminalapi.Keyboard))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if td.mouseSubscriber != nil {
|
|
|
|
td.eds.Subscribe([]terminalapi.Event{&terminalapi.Mouse{}}, func(ev terminalapi.Event) {
|
|
|
|
td.mouseSubscriber(ev.(*terminalapi.Mouse))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-08 04:30:38 +08:00
|
|
|
// handleError forwards the error to the error handler if one was
|
|
|
|
// provided or panics.
|
|
|
|
func (td *termdash) handleError(err error) {
|
|
|
|
if td.errorHandler != nil {
|
|
|
|
td.errorHandler(err)
|
|
|
|
} else {
|
|
|
|
panic(err)
|
2018-04-23 08:05:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// setClearNeeded flags that the terminal needs to be cleared next time we're
|
|
|
|
// drawing it.
|
|
|
|
func (td *termdash) setClearNeeded() {
|
|
|
|
td.mu.Lock()
|
|
|
|
defer td.mu.Unlock()
|
|
|
|
td.clearNeeded = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// redraw redraws the container and its widgets.
|
|
|
|
// The caller must hold td.mu.
|
|
|
|
func (td *termdash) redraw() error {
|
|
|
|
if td.clearNeeded {
|
|
|
|
if err := td.term.Clear(); err != nil {
|
|
|
|
return fmt.Errorf("term.Clear => error: %v", err)
|
|
|
|
}
|
|
|
|
td.clearNeeded = false
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := td.container.Draw(); err != nil {
|
|
|
|
return fmt.Errorf("container.Draw => error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := td.term.Flush(); err != nil {
|
|
|
|
return fmt.Errorf("term.Flush => error: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-20 15:20:11 +08:00
|
|
|
// evRedraw redraws the container and its widgets.
|
|
|
|
func (td *termdash) evRedraw() error {
|
2018-04-23 08:05:54 +08:00
|
|
|
td.mu.Lock()
|
|
|
|
defer td.mu.Unlock()
|
2019-02-21 12:35:26 +08:00
|
|
|
|
|
|
|
// Don't redraw immediately, give widgets that are performing enough time
|
|
|
|
// to update.
|
|
|
|
// We don't want to actually synchronize until all widgets update, we are
|
|
|
|
// purposefully leaving slow widgets behind.
|
|
|
|
time.Sleep(25 * time.Millisecond)
|
2018-04-23 08:05:54 +08:00
|
|
|
return td.redraw()
|
|
|
|
}
|
|
|
|
|
|
|
|
// periodicRedraw is called once each RedrawInterval.
|
|
|
|
func (td *termdash) periodicRedraw() error {
|
|
|
|
td.mu.Lock()
|
|
|
|
defer td.mu.Unlock()
|
|
|
|
return td.redraw()
|
|
|
|
}
|
|
|
|
|
|
|
|
// processEvents processes terminal input events.
|
|
|
|
// This is the body of the event collecting goroutine.
|
|
|
|
func (td *termdash) processEvents(ctx context.Context) {
|
|
|
|
defer close(td.exitCh)
|
|
|
|
|
|
|
|
for {
|
2019-02-20 14:18:44 +08:00
|
|
|
ev := td.term.Event(ctx)
|
|
|
|
if ev != nil {
|
|
|
|
td.eds.Event(ev)
|
2018-04-23 08:05:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// start starts the terminal dashboard. Blocks until the context expires or
|
|
|
|
// until stop() is called.
|
|
|
|
func (td *termdash) start(ctx context.Context) error {
|
2019-03-29 10:30:09 +08:00
|
|
|
// Redraw once to initialize the container sizes.
|
|
|
|
if err := td.periodicRedraw(); err != nil {
|
|
|
|
close(td.exitCh)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-04-23 08:05:54 +08:00
|
|
|
redrawTimer := time.NewTicker(td.redrawInterval)
|
|
|
|
defer redrawTimer.Stop()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// stops when stop() is called or the context expires.
|
|
|
|
go td.processEvents(ctx)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-redrawTimer.C:
|
|
|
|
if err := td.periodicRedraw(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case <-td.closeCh:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// stop tells the event collecting goroutine to stop.
|
|
|
|
// Blocks until it exits.
|
|
|
|
func (td *termdash) stop() {
|
|
|
|
close(td.closeCh)
|
|
|
|
<-td.exitCh
|
|
|
|
}
|