forked from mum4k/termdash
/
termbox.go
164 lines (139 loc) · 4.03 KB
/
termbox.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// 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.
// Package termbox implements terminal using the nsf/termbox-go library.
package termbox
import (
"context"
"image"
"github.com/mum4k/termdash/cell"
"github.com/mum4k/termdash/internal/event/eventqueue"
"github.com/mum4k/termdash/terminal/terminalapi"
tbx "github.com/nsf/termbox-go"
)
// 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)
}
// DefaultColorMode is the default value for the ColorMode option.
const DefaultColorMode = terminalapi.ColorMode256
// ColorMode sets the terminal color mode.
// Defaults to DefaultColorMode.
func ColorMode(cm terminalapi.ColorMode) Option {
return option(func(t *Terminal) {
t.colorMode = cm
})
}
// Terminal provides input and output to a real terminal. Wraps the
// nsf/termbox-go terminal implementation. This object is not thread-safe.
// Implements terminalapi.Terminal.
type Terminal struct {
// events is a queue of input events.
events *eventqueue.Unbound
// done gets closed when Close() is called.
done chan struct{}
// Options.
colorMode terminalapi.ColorMode
}
// 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
}
// New returns a new termbox based Terminal.
// Call Close() when the terminal isn't required anymore.
func New(opts ...Option) (*Terminal, error) {
if err := tbx.Init(); err != nil {
return nil, err
}
tbx.SetInputMode(tbx.InputEsc | tbx.InputMouse)
t := newTerminal(opts...)
om, err := colorMode(t.colorMode)
if err != nil {
return nil, err
}
tbx.SetOutputMode(om)
go t.pollEvents() // Stops when Close() is called.
return t, nil
}
// Size implements terminalapi.Terminal.Size.
func (t *Terminal) Size() image.Point {
w, h := tbx.Size()
return image.Point{w, h}
}
// Clear implements terminalapi.Terminal.Clear.
func (t *Terminal) Clear(opts ...cell.Option) error {
o := cell.NewOptions(opts...)
return tbx.Clear(cellOptsToFg(o), cellOptsToBg(o))
}
// Flush implements terminalapi.Terminal.Flush.
func (t *Terminal) Flush() error {
return tbx.Flush()
}
// SetCursor implements terminalapi.Terminal.SetCursor.
func (t *Terminal) SetCursor(p image.Point) {
tbx.SetCursor(p.X, p.Y)
}
// HideCursor implements terminalapi.Terminal.HideCursor.
func (t *Terminal) HideCursor() {
tbx.HideCursor()
}
// SetCell implements terminalapi.Terminal.SetCell.
func (t *Terminal) SetCell(p image.Point, r rune, opts ...cell.Option) error {
o := cell.NewOptions(opts...)
tbx.SetCell(p.X, p.Y, r, cellOptsToFg(o), cellOptsToBg(o))
return nil
}
// 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)
}
}
}
// Event implements terminalapi.Terminal.Event.
func (t *Terminal) Event(ctx context.Context) terminalapi.Event {
ev := t.events.Pull(ctx)
if ev == nil {
return nil
}
return ev
}
// Close closes the terminal, should be called when the terminal isn't required
// anymore to return the screen to a sane state.
// Implements terminalapi.Terminal.Close.
func (t *Terminal) Close() {
close(t.done)
tbx.Close()
}