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
|
// gomuks - A terminal Matrix client written in Go.
// Copyright (C) 2020 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package html
import (
"fmt"
"strings"
"maunium.net/go/mauview"
)
type ContainerEntity struct {
*BaseEntity
// The children of this container entity.
Children []Entity
// Number of cells to indent children.
Indent int
}
// PlainText returns the plaintext content in this entity and all its children.
func (ce *ContainerEntity) PlainText() string {
if len(ce.Children) == 0 {
return ""
}
var buf strings.Builder
newlined := false
for _, child := range ce.Children {
text := child.PlainText()
if !strings.HasPrefix(text, "\n") && child.IsBlock() && !newlined {
buf.WriteRune('\n')
}
newlined = false
buf.WriteString(text)
if child.IsBlock() {
if !strings.HasSuffix(text, "\n") {
buf.WriteRune('\n')
}
newlined = true
}
}
return strings.TrimSpace(buf.String())
}
// AdjustStyle recursively changes the style of this entity and all its children.
func (ce *ContainerEntity) AdjustStyle(fn AdjustStyleFunc) Entity {
for _, child := range ce.Children {
child.AdjustStyle(fn)
}
ce.Style = fn(ce.Style)
return ce
}
// clone creates a deep copy of this base entity.
func (ce *ContainerEntity) Clone() Entity {
children := make([]Entity, len(ce.Children))
for i, child := range ce.Children {
children[i] = child.Clone()
}
return &ContainerEntity{
BaseEntity: ce.BaseEntity.Clone().(*BaseEntity),
Children: children,
Indent: ce.Indent,
}
}
// String returns a textual representation of this BaseEntity struct.
func (ce *ContainerEntity) String() string {
if len(ce.Children) == 0 {
return fmt.Sprintf(`&html.ContainerEntity{Base=%s, Indent=%d, Children=[]}`, ce.BaseEntity, ce.Indent)
}
var buf strings.Builder
_, _ = fmt.Fprintf(&buf, `&html.ContainerEntity{Base=%s,
Indent=%d, Children=[`, ce.BaseEntity, ce.Indent)
for _, child := range ce.Children {
buf.WriteString("\n ")
buf.WriteString(strings.Join(strings.Split(strings.TrimRight(child.String(), "\n"), "\n"), "\n "))
}
buf.WriteString("\n]},")
return buf.String()
}
// Draw draws this entity onto the given mauview Screen.
func (ce *ContainerEntity) Draw(screen mauview.Screen) {
if len(ce.Children) == 0 {
return
}
width, _ := screen.Size()
prevBreak := false
proxyScreen := &mauview.ProxyScreen{Parent: screen, OffsetX: ce.Indent, Width: width - ce.Indent, Style: ce.Style}
for i, entity := range ce.Children {
if i != 0 && entity.getStartX() == 0 {
proxyScreen.OffsetY++
}
proxyScreen.Height = entity.Height()
entity.Draw(proxyScreen)
proxyScreen.SetStyle(ce.Style)
proxyScreen.OffsetY += entity.Height() - 1
_, isBreak := entity.(*BreakEntity)
if prevBreak && isBreak {
proxyScreen.OffsetY++
}
prevBreak = isBreak
}
}
// CalculateBuffer prepares this entity and all its children for rendering with the given parameters
func (ce *ContainerEntity) CalculateBuffer(width, startX int, bare bool) int {
ce.BaseEntity.CalculateBuffer(width, startX, bare)
if len(ce.Children) > 0 {
ce.height = 0
childStartX := ce.startX
prevBreak := false
for _, entity := range ce.Children {
if entity.IsBlock() || childStartX == 0 || ce.height == 0 {
ce.height++
}
childStartX = entity.CalculateBuffer(width-ce.Indent, childStartX, bare)
ce.height += entity.Height() - 1
_, isBreak := entity.(*BreakEntity)
if prevBreak && isBreak {
ce.height++
}
prevBreak = isBreak
}
if !ce.Block {
return childStartX
}
}
return ce.startX
}
|