-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
simple.go
115 lines (99 loc) · 2.47 KB
/
simple.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
package termimg
import (
"image"
// _ "image/jpeg" // ...
// _ "image/png" // ...
"github.com/srlehn/termimg/internal"
"github.com/srlehn/termimg/query/qdefault"
"github.com/srlehn/termimg/resize/rdefault"
"github.com/srlehn/termimg/term"
"github.com/srlehn/termimg/tty/gotty"
"github.com/srlehn/termimg/wm"
"github.com/srlehn/termimg/wm/wmimpl"
)
var (
// chosen defaults
ttyDefault = internal.DefaultTTYDevice()
ttyProvider = gotty.New
querier = qdefault.NewQuerier()
wmImplementation = wmimpl.Impl()
resizer term.Resizer = &rdefault.Resizer{}
)
var (
DefaultConfig = term.Options{
term.SetPTYName(ttyDefault),
term.SetTTYProvider(ttyProvider, false),
term.SetQuerier(querier, true),
term.SetWindowProvider(wm.SetImpl(wmImplementation), true),
term.SetResizer(resizer),
}
)
var (
termActive *term.Terminal
)
// Terminal ...
func Terminal() (*term.Terminal, error) {
return termActive, initTerm()
}
func initTerm() error {
if termActive != nil {
return nil
}
var err error
termActive, err = term.NewTerminal(DefaultConfig)
if err != nil {
return err
}
return nil
}
// Query ...
func Query(qs string, p term.Parser) (string, error) {
tm, err := Terminal()
if err != nil {
return ``, err
}
return tm.Query(qs, p)
}
// Draw ...
func Draw(img image.Image, bounds image.Rectangle) error {
tm, err := Terminal()
if err != nil {
return err
}
return tm.Draw(img, bounds)
}
// DrawBytes - for use with "embed", etc.
// requires the prior registration of a decoder. e.g.:
//
// import _ "image/png"
func DrawBytes(imgBytes []byte, bounds image.Rectangle) error {
tm, err := Terminal()
if err != nil {
return err
}
return tm.Draw(NewImageBytes(imgBytes), bounds)
}
// DrawFile ...
func DrawFile(imgFile string, bounds image.Rectangle) error {
tm, err := Terminal()
if err != nil {
return err
}
return tm.Draw(term.NewImageFilename(imgFile), bounds)
}
// CleanUp ...
func CleanUp() error {
if termActive == nil {
return nil
}
return termActive.Close()
}
// NewImage ...
func NewImage(img image.Image) *term.Image { return term.NewImage(img) }
// NewImageFileName ...
func NewImageFileName(imgfile string) *term.Image { return term.NewImageFilename(imgfile) }
// NewImageBytes - for use with "embed", etc.
// requires the prior registration of a decoder. e.g.:
//
// import _ "image/png"
func NewImageBytes(imgBytes []byte) *term.Image { return term.NewImageBytes(imgBytes) }