-
Notifications
You must be signed in to change notification settings - Fork 1
/
mode.go
61 lines (52 loc) · 1.67 KB
/
mode.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
package anansi
import (
"fmt"
"github.com/jcorbin/anansi/ansi"
)
// Mode holds a set/reset byte buffer to write to a terminal file during
// Enter/Exit. Primary useful for set/reset mode control sequences.
type Mode struct {
Set, Reset []byte
// TODO support active use with an *os.File
}
// Enter writes the modes' Set() string to the terminal's output file.
func (mode *Mode) Enter(term *Term) error {
if term.Output.File != nil {
if _, err := term.Output.File.Write(mode.Set); err != nil {
return fmt.Errorf("failed to write mode set string: %v", err)
}
}
return nil
}
// Exit writes the modes' Reset() string to the terminal's output file.
func (mode *Mode) Exit(term *Term) error {
if term.Output.File != nil {
if _, err := term.Output.File.Write(mode.Reset); err != nil {
return fmt.Errorf("failed to write mode reset string: %v", err)
}
}
return nil
}
// AddMode adds Set/Reset pairs from one or more ansi.Mode values.
func (mode *Mode) AddMode(ms ...ansi.Mode) {
for _, m := range ms {
mode.AddModePair(m.Set(), m.Reset())
}
}
// AddModePair adds the byte representation of the given set/reset sequences
// to the mode's Set/Reset byte buffers; appends to Set, prepends to Reset.
func (mode *Mode) AddModePair(set, reset ansi.Seq) {
var b [128]byte
mode.Set = set.AppendTo(mode.Set)
mode.Reset = append(reset.AppendTo(b[:0]), mode.Reset...)
}
// AddModeSeq appends one or more ansi sequences to the set buffer and
// prepends them to the reset buffer.
func (mode *Mode) AddModeSeq(seqs ...ansi.Seq) {
for _, seq := range seqs {
n := len(mode.Set)
mode.Set = seq.AppendTo(mode.Set)
m := len(mode.Set)
mode.Reset = append(mode.Set[n:m:m], mode.Reset...)
}
}