2018-04-15 06:06:57 +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-03-28 03:20:05 +08:00
|
|
|
// Package termbox implements terminal using the nsf/termbox-go library.
|
|
|
|
package termbox
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"image"
|
|
|
|
|
2019-02-24 14:13:26 +08:00
|
|
|
"github.com/mum4k/termdash/cell"
|
2020-04-11 03:26:45 +08:00
|
|
|
"github.com/mum4k/termdash/private/event/eventqueue"
|
2019-02-24 14:27:17 +08:00
|
|
|
"github.com/mum4k/termdash/terminal/terminalapi"
|
2018-04-05 11:02:43 +08:00
|
|
|
tbx "github.com/nsf/termbox-go"
|
2018-03-28 03:20:05 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2019-02-11 12:55:35 +08:00
|
|
|
// DefaultColorMode is the default value for the ColorMode option.
|
|
|
|
const DefaultColorMode = terminalapi.ColorMode256
|
|
|
|
|
2018-03-28 03:20:05 +08:00
|
|
|
// ColorMode sets the terminal color mode.
|
2019-02-11 12:55:35 +08:00
|
|
|
// Defaults to DefaultColorMode.
|
2018-03-28 03:20:05 +08:00
|
|
|
func ColorMode(cm terminalapi.ColorMode) Option {
|
|
|
|
return option(func(t *Terminal) {
|
|
|
|
t.colorMode = cm
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-04-23 07:47:12 +08:00
|
|
|
// Terminal provides input and output to a real terminal. Wraps the
|
|
|
|
// nsf/termbox-go terminal implementation. This object is not thread-safe.
|
2018-03-28 03:20:05 +08:00
|
|
|
// Implements terminalapi.Terminal.
|
|
|
|
type Terminal struct {
|
2018-04-06 01:35:02 +08:00
|
|
|
// events is a queue of input events.
|
|
|
|
events *eventqueue.Unbound
|
|
|
|
|
|
|
|
// done gets closed when Close() is called.
|
|
|
|
done chan struct{}
|
|
|
|
|
|
|
|
// Options.
|
2018-03-28 03:20:05 +08:00
|
|
|
colorMode terminalapi.ColorMode
|
|
|
|
}
|
|
|
|
|
2019-02-11 12:55:35 +08:00
|
|
|
// newTerminal creates the terminal and applies the options.
|
|
|
|
func newTerminal(opts ...Option) *Terminal {
|
|
|
|
t := &Terminal{
|
|
|
|
events: eventqueue.New(),
|
|
|
|
done: make(chan struct{}),
|
|
|
|
colorMode: DefaultColorMode,
|
|
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt.set(t)
|
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2018-03-28 03:20:05 +08:00
|
|
|
// New returns a new termbox based Terminal.
|
|
|
|
// Call Close() when the terminal isn't required anymore.
|
|
|
|
func New(opts ...Option) (*Terminal, error) {
|
2018-04-05 11:02:43 +08:00
|
|
|
if err := tbx.Init(); err != nil {
|
2018-03-28 03:20:05 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-06 01:35:02 +08:00
|
|
|
tbx.SetInputMode(tbx.InputEsc | tbx.InputMouse)
|
2018-03-28 03:20:05 +08:00
|
|
|
|
2019-02-11 12:55:35 +08:00
|
|
|
t := newTerminal(opts...)
|
2018-03-28 03:20:05 +08:00
|
|
|
om, err := colorMode(t.colorMode)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-05 11:02:43 +08:00
|
|
|
tbx.SetOutputMode(om)
|
2018-04-06 01:35:02 +08:00
|
|
|
|
|
|
|
go t.pollEvents() // Stops when Close() is called.
|
2018-03-28 03:20:05 +08:00
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
2018-05-11 23:32:37 +08:00
|
|
|
// Size implements terminalapi.Terminal.Size.
|
2018-03-28 03:20:05 +08:00
|
|
|
func (t *Terminal) Size() image.Point {
|
2018-04-05 11:02:43 +08:00
|
|
|
w, h := tbx.Size()
|
2018-03-28 03:20:05 +08:00
|
|
|
return image.Point{w, h}
|
|
|
|
}
|
|
|
|
|
2018-05-11 23:32:37 +08:00
|
|
|
// Clear implements terminalapi.Terminal.Clear.
|
2018-03-28 03:20:05 +08:00
|
|
|
func (t *Terminal) Clear(opts ...cell.Option) error {
|
|
|
|
o := cell.NewOptions(opts...)
|
2019-01-14 10:53:26 +08:00
|
|
|
return tbx.Clear(cellOptsToFg(o), cellOptsToBg(o))
|
2018-03-28 03:20:05 +08:00
|
|
|
}
|
|
|
|
|
2018-05-11 23:32:37 +08:00
|
|
|
// Flush implements terminalapi.Terminal.Flush.
|
2018-03-28 03:20:05 +08:00
|
|
|
func (t *Terminal) Flush() error {
|
2018-04-05 11:02:43 +08:00
|
|
|
return tbx.Flush()
|
2018-03-28 03:20:05 +08:00
|
|
|
}
|
|
|
|
|
2018-05-11 23:32:37 +08:00
|
|
|
// SetCursor implements terminalapi.Terminal.SetCursor.
|
2018-03-28 03:20:05 +08:00
|
|
|
func (t *Terminal) SetCursor(p image.Point) {
|
2018-04-05 11:02:43 +08:00
|
|
|
tbx.SetCursor(p.X, p.Y)
|
2018-03-28 03:20:05 +08:00
|
|
|
}
|
|
|
|
|
2018-05-11 23:32:37 +08:00
|
|
|
// HideCursor implements terminalapi.Terminal.HideCursor.
|
2018-03-28 03:20:05 +08:00
|
|
|
func (t *Terminal) HideCursor() {
|
2018-04-05 11:02:43 +08:00
|
|
|
tbx.HideCursor()
|
2018-03-28 03:20:05 +08:00
|
|
|
}
|
|
|
|
|
2018-05-11 23:32:37 +08:00
|
|
|
// SetCell implements terminalapi.Terminal.SetCell.
|
2018-03-28 03:20:05 +08:00
|
|
|
func (t *Terminal) SetCell(p image.Point, r rune, opts ...cell.Option) error {
|
|
|
|
o := cell.NewOptions(opts...)
|
2019-01-14 10:53:26 +08:00
|
|
|
tbx.SetCell(p.X, p.Y, r, cellOptsToFg(o), cellOptsToBg(o))
|
2018-03-28 03:20:05 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-06 01:35:02 +08:00
|
|
|
// pollEvents polls and enqueues the input events.
|
|
|
|
func (t *Terminal) pollEvents() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-t.done:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
events := toTermdashEvents(tbx.PollEvent())
|
|
|
|
for _, ev := range events {
|
|
|
|
t.events.Push(ev)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-11 23:32:37 +08:00
|
|
|
// Event implements terminalapi.Terminal.Event.
|
2018-03-28 03:20:05 +08:00
|
|
|
func (t *Terminal) Event(ctx context.Context) terminalapi.Event {
|
2019-02-20 13:20:58 +08:00
|
|
|
ev := t.events.Pull(ctx)
|
|
|
|
if ev == nil {
|
2019-02-20 14:18:44 +08:00
|
|
|
return nil
|
2018-04-06 01:35:02 +08:00
|
|
|
}
|
|
|
|
return ev
|
2018-03-28 03:20:05 +08:00
|
|
|
}
|
|
|
|
|
2018-05-11 23:32:37 +08:00
|
|
|
// Close closes the terminal, should be called when the terminal isn't required
|
2018-03-28 03:20:05 +08:00
|
|
|
// anymore to return the screen to a sane state.
|
2018-05-11 23:32:37 +08:00
|
|
|
// Implements terminalapi.Terminal.Close.
|
2018-03-28 03:20:05 +08:00
|
|
|
func (t *Terminal) Close() {
|
2018-04-06 01:35:02 +08:00
|
|
|
close(t.done)
|
2018-04-05 11:02:43 +08:00
|
|
|
tbx.Close()
|
2018-03-28 03:20:05 +08:00
|
|
|
}
|