-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
image.go
64 lines (56 loc) · 1.1 KB
/
image.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
package termuiimg
import (
"image"
"github.com/gizak/termui/v3"
"github.com/srlehn/termimg/internal/errors"
"github.com/srlehn/termimg/term"
)
type Image struct {
termui.Block
Canvas *term.Canvas
}
func NewImage(tm *term.Terminal, img image.Image, bounds image.Rectangle) (*Image, error) {
if err := errors.NilParam(tm); err != nil {
return nil, err
}
_ = tm.SetOptions(term.TUIMode)
m := &Image{
Block: *termui.NewBlock(),
}
m.SetRect(bounds.Min.X, bounds.Min.Y, bounds.Max.X, bounds.Max.Y)
canvas, err := tm.NewCanvas(bounds)
if err != nil {
return nil, err
}
if img != nil {
err := canvas.SetImage(img)
if err != nil {
canvas.Close()
return nil, err
}
}
m.Canvas = canvas
return m, nil
}
func (m *Image) SetImage(img image.Image) {
if m == nil || m.Canvas == nil {
return
}
_ = m.Canvas.SetImage(img)
}
func (m *Image) Draw(buf *termui.Buffer) {
if m == nil {
return
}
m.Block.Draw(buf) // draw border
if m.Canvas == nil {
return
}
_ = m.Canvas.Draw(nil)
}
func (m *Image) Close() error {
if m == nil || m.Canvas == nil {
return nil
}
return m.Canvas.Close()
}