1
0
mirror of https://github.com/mum4k/termdash.git synced 2025-05-04 22:18:07 +08:00
termdash/terminal/faketerm/faketerm.go
Jakub Sobon 59e1bd6472
Implementing cell, buffer, container options and fake terminal.
Push after a partial commit to prevent data loss.
This isn't complete and doesn't have complete test coverage.
2018-03-28 21:34:20 +03:00

102 lines
2.1 KiB
Go

// Package faketerm is a fake implementation of the terminal for the use in tests.
package faketerm
import (
"context"
"errors"
"fmt"
"image"
"log"
"github.com/mum4k/termdash/cell"
"github.com/mum4k/termdash/terminalapi"
)
// Option is used to provide options.
type Option interface {
// set sets the provided option.
set(*Terminal)
}
// option implements Option.
type option func(*Terminal)
// set implements Option.set.
func (o option) set(t *Terminal) {
o(t)
}
// Terminal is a fake terminal.
// This implementation is thread-safe.
type Terminal struct {
// buffer holds the terminal cells.
buffer cell.Buffer
}
// New returns a new fake Terminal.
func New(size image.Point, opts ...Option) (*Terminal, error) {
b, err := cell.NewBuffer(size)
if err != nil {
return nil, err
}
t := &Terminal{
buffer: b,
}
for _, opt := range opts {
opt.set(t)
}
return t, nil
}
// Implements terminalapi.Terminal.Size.
func (t *Terminal) Size() image.Point {
return t.buffer.Size()
}
// Implements terminalapi.Terminal.Clear.
func (t *Terminal) Clear(opts ...cell.Option) error {
b, err := cell.NewBuffer(t.buffer.Size())
if err != nil {
return err
}
t.buffer = b
return nil
}
// Implements terminalapi.Terminal.Flush.
func (t *Terminal) Flush() error {
return errors.New("unimplemented")
}
// Implements terminalapi.Terminal.SetCursor.
func (t *Terminal) SetCursor(p image.Point) {
log.Fatal("unimplemented")
}
// Implements terminalapi.Terminal.HideCursor.
func (t *Terminal) HideCursor() {
log.Fatal("unimplemented")
}
// Implements terminalapi.Terminal.SetCell.
func (t *Terminal) SetCell(p image.Point, r rune, opts ...cell.Option) error {
if area := t.buffer.Area(); !p.In(area) {
return fmt.Errorf("cell at point %+v falls out of the terminal area %+v", p, area)
}
cell := t.buffer[p.X][p.Y]
cell.Rune = r
cell.Apply(opts...)
return nil
}
// Implements terminalapi.Terminal.Event.
func (t *Terminal) Event(ctx context.Context) terminalapi.Event {
log.Fatal("unimplemented")
return nil
}
// Closes the terminal. This is a no-op on the fake terminal.
func (t *Terminal) Close() {}