termdash/doc/hld.md

214 lines
7.9 KiB
Markdown
Raw Normal View History

2018-03-25 01:05:01 +08:00
# Terminal Dashboard High-Level Design
## Objective
2018-03-26 22:40:23 +08:00
Develop infrastructure of dashboard widgets. The widgets should support both
input (mouse and keyboard) and output (display of information to the user).
2019-05-04 03:58:17 +08:00
Fulfil the requirements outlined in the main
2018-03-26 22:40:23 +08:00
[README](http://github.com/mum4k/termdash).
2018-03-25 01:05:01 +08:00
## Background
2018-03-26 22:40:23 +08:00
The terminal dashboard allows placement of configurable widgets onto the terminal.
A widget displays some information to the user, e.g. A graph, a chart, a
progress bar. A widget can receive information from the user in the form of
events, e.g. Mouse or keyboard input.
The widgets aren't placed onto the terminal directly, instead the terminal is
organized into containers. Each container can contain either a widget or
other containers.
2018-03-25 01:05:01 +08:00
## Overview
2018-03-26 22:40:23 +08:00
The terminal dashboard consists of the following layers:
- Terminal.
2018-03-26 22:40:23 +08:00
- Infrastructure.
- Widgets.
The **terminal layer** abstracts the terminal implementation. A real terminal
implementation is used in production when displaying data to the user. A fake
2018-03-26 22:40:23 +08:00
terminal implementation is used in widget unit tests and system tests. Other
implementations are possible, e.g. Image export. The terminal layer is private,
neither the users of this library nor the widgets interact with the terminal
2018-03-26 22:40:23 +08:00
directly.
The **infrastructure layer** is responsible for container management, tracking
2019-02-20 13:52:51 +08:00
of keyboard and mouse focus and distribution and handling of external events
like resizing of the terminal. The infrastructure layer also decides when to
flush the buffer and refresh the screen. I.e. The widgets update content of a
back buffer and the infrastructure decides when it is synchronized to the
terminal.
2018-03-26 22:40:23 +08:00
The **widgets layer** contains the implementations of individual widgets. Each
widget receives a canvas from the container on which it presents its content to
the user. Widgets indicate to the infrastructure layer if they support input
events, which are then forwarded from the infrastructure layer.
The user interacts with the widget API when constructing individual widgets and
with the container API when placing the widgets onto the dashboard.
<p align="center">
<img src="images/hld.png" width="50%">
</p>
2018-03-26 22:40:23 +08:00
2018-03-25 01:05:01 +08:00
## Detailed design
### Terminal
The terminal provides access to the input and output.
It allows to:
- Set values and attributes of cells on a back buffer representing a 2-D
canvas.
- Flush the content of the back buffer to the output.
- Manipulate the cursor position and visibility.
2019-02-20 13:52:51 +08:00
- Allow the infrastructure to read input events (keyboard, mouse, terminal
resize, etc...).
2018-03-26 22:40:23 +08:00
### Infrastructure
The infrastructure handles terminal setup, input events and manages containers.
2019-02-20 13:52:51 +08:00
#### Input events
The infrastructure regularly polls events from the terminal layer and feeds
2019-05-04 03:58:17 +08:00
them into the event distribution system (EDS). The EDS fulfils the following
2019-02-20 13:52:51 +08:00
functions:
- Allow subscribers to specify the type of events they want to receive.
2019-05-04 03:58:17 +08:00
- Distributes events in a non-blocking manner, i.e. a single slow subscriber
2019-02-20 13:52:51 +08:00
cannot slow down other subscribers.
- Events to each subscriber are throttled, if a subscriber builds a long tail
of unprocessed input events, the EDS selectively drops repetitive events
towards the subscriber and eventually implements a tail-drop strategy.
The infrastructure itself is an input event subscriber and processes resize and
error events. The infrastructure panics on error events by default, unless an
error handler is provided by the user. Each widget that registers for keyboard
or mouse events is also an event subscriber. Any errors that happen while
processing an input event are send back to the EDS in the form of an Error
event and are processed by the infrastructure.
2019-02-20 13:52:51 +08:00
#### Input keyboard focus
2019-02-20 13:52:51 +08:00
The infrastructure tracks focus. Only the focused widget receives keyboard
events. Focus can be changed using mouse or keyboard shortcuts. The focused
widget is highlighted on the dashboard.
#### Containers
The container provides a way of splitting the dashboard down to smaller
elements where individual widgets can be placed. Each container can be split to
multiple sub containers. A container contains either a sub container or a
widget.
Container is responsible for coordinate translation. Each widget receives a
virtual canvas it can draw on. Each of these canvases starts at coordinates
image.Point{0, 0}. The parent container translates coordinates from the virtual
canvas to the real terminal. The container therefore enforces limits for widgets.
Containers can be styled with borders and other options.
#### Flushing the terminal
2019-02-20 13:52:51 +08:00
All widgets indirectly write to the back buffer of the terminal implementation.
The changes to the back buffer only become visible when the infrastructure
flushes its content.
#### Terminal resizing
The terminal resize events are processed by the infrastructure. Each widget
indicates its desired and minimum size for its canvas when registering with its
parent container.
The parent container in turn informs the widget what is the actual size of its
canvas. The infrastructure guarantees that the actual size won't ever be
smaller than the advertised minimum and guarantees that the size will keep the
aspect ration requested by the widget.
When the size of terminal changes, the infrastructure resizes all containers
according to the rules outlined above, asks all widgets to redraw their
canvases and flushes to the back buffer to the terminal.
2018-03-26 22:40:23 +08:00
### Widgets
2018-03-27 01:04:09 +08:00
Users of the terminal dashboard construct the widgets directly. Therefore each
widget can define its own options and API for setting values (e.g. The
displayed percentage on a progress bar). The users then create the desired
container splits and place each widget into a dedicated container.
Each widget receives a canvas from the parent container, the widget can draw
anything on the canvas as long as it stays within the limits. Helper libraries
are developed that allow placement and drawing of common elements like lines or
2019-02-20 13:52:51 +08:00
geometric shapes.
2018-03-27 01:04:09 +08:00
2018-03-26 22:40:23 +08:00
## APIs
### Terminal API
2018-03-26 22:40:23 +08:00
The Terminal API is an interface private to the terminal dashboard library. Its
primary purpose is to act as a shim layer over different terminal
2018-03-26 22:40:23 +08:00
implementations.
2018-03-28 02:01:35 +08:00
The Terminal API is defined in the
[terminalapi](http://github.com/mum4k/termdash/terminalapi/terminalapi.go)
package.
2018-03-26 22:40:23 +08:00
The **Event()** method returns the next input event. Different input event
2018-03-28 02:01:35 +08:00
types are defined in the
[event.go](http://github.com/mum4k/termdash/terminalapi/event.go)
file.
2018-03-26 22:40:23 +08:00
### Container API
2018-03-28 02:01:35 +08:00
The Container API is used to split the terminal and place the widgets. Each
2018-03-27 01:16:58 +08:00
container can be split to two sub containers or have a widget placed into it.
A container can be split either horizontally or vertically.
The containers further accept styling options and alignment options. The
2018-03-28 02:01:35 +08:00
following indicates how the Container API will be used.
The Container API is defined in the
[container](http://github.com/mum4k/termdash/container/container.go)
package.
A demonstration how this is used from the client perspective is in the
[container_test.go](http://github.com/mum4k/termdash/container/container_test.go)
file.
2018-03-27 01:16:58 +08:00
2018-03-26 22:40:23 +08:00
### Widget API
2018-03-28 02:01:35 +08:00
Each widget must implement the Widget API. All widget implementations must
2018-03-27 01:04:09 +08:00
be thread-safe since the calls that update the displayed values come in
concurrently with requests and events from the infrastructure.
2018-03-28 02:01:35 +08:00
The Widget API is defined in the
[widget](http://github.com/mum4k/termdash/widget/widget.go)
package.
Each widget gets a Canvas to draw on. The Canvas API is defined in the
[canvas](http://github.com/mum4k/termdash/canvas/canvas.go)
package.
2018-03-27 01:04:09 +08:00
2018-03-25 01:05:01 +08:00
## Testing plan
2019-02-20 13:52:51 +08:00
Unit test helpers are provided with the terminal dashboard library, these
include:
2018-03-25 01:05:01 +08:00
2018-03-27 01:16:58 +08:00
- A fake implementation of the terminal API.
- Unit test comparison helpers to verify the content of the fake terminal.
2019-02-20 13:52:51 +08:00
- Visualization tools to display differences between the expected and the
actual.
2018-03-25 01:05:01 +08:00
## Document history
2018-03-26 22:40:23 +08:00
Date | Author | Description
2019-02-20 13:52:51 +08:00
------------|--------|-----------------------------------
2018-03-26 22:40:23 +08:00
24-Mar-2018 | mum4k | Initial draft.
2019-02-20 13:52:51 +08:00
20-Feb-2019 | mum4k | Added notes on event distribution.