-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext.go
92 lines (76 loc) · 2.08 KB
/
text.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
package ttyprogress
import (
"github.com/mandelsoft/goutils/general"
"github.com/mandelsoft/object"
"github.com/mandelsoft/ttyprogress/ppi"
"github.com/mandelsoft/ttyprogress/specs"
)
// Text provides a range of lines of output
// until the action described by the element is
// calling Text.Close.
// The element can be used as writer to pass the
// intended output.
// After the writer is closed, the complete output is
// shown after earlier elements and outer containers are
// done.
type Text interface {
specs.TextInterface
}
type TextDefinition struct {
specs.TextDefinition[*TextDefinition]
}
func NewText(v ...int) *TextDefinition {
d := &TextDefinition{}
d.TextDefinition = specs.NewTextDefinition(specs.NewSelf(d))
lines := general.Optional(v...)
if lines > 0 {
d.SetView(lines)
}
return d
}
func (d *TextDefinition) Dup() *TextDefinition {
dup := &TextDefinition{}
dup.TextDefinition = d.TextDefinition.Dup(specs.NewSelf(dup))
return dup
}
func (d *TextDefinition) Add(c Container) (Text, error) {
return newText(c, d)
}
////////////////////////////////////////////////////////////////////////////////
type _Text struct {
*ppi.ElemBase[*_TextImpl]
elem *_TextImpl
}
func (t *_Text) Write(data []byte) (int, error) {
defer t.elem.Lock()()
return t.elem.Protected().Write(data)
}
type _TextImpl struct {
*ppi.ElemBaseImpl[*_TextImpl]
}
// NewText creates a new text stream with the given window size.
// With Text.SetAuto updates are triggered by the Text.Write calls.
// Otherwise, Text.Flush must be called to update the text window.
func newText(p Container, c specs.TextConfiguration) (Text, error) {
e := &_TextImpl{}
o := &_Text{elem: e}
b, s, err := ppi.NewElemBase[*_TextImpl](object.NewSelf[*_TextImpl, any](e, o), p, c, c.GetView())
if err != nil {
return nil, err
}
e.ElemBaseImpl = s
o.ElemBase = b
if c.GetAuto() {
e.Block().SetAuto()
}
return o, nil
}
func (t *_TextImpl) Update() bool {
return false
}
func (t *_TextImpl) Flush() error {
return t.Block().Flush()
}
func (t *_TextImpl) Write(data []byte) (int, error) {
return t.Block().Write(data)
}