Debian Codegraph @

ui/messages/html/container.go

raw symbols SCIP
1 // gomuks - A terminal Matrix client written in Go.
2 // Copyright (C) 2020 Tulir Asokan
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
13 //
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17 package html
18
19 import (
20 "fmt"
21 "strings"
22
23 "go.mau.fi/mauview"
24 )
25
26 type ContainerEntity struct {
27 *BaseEntity
28
29 // The children of this container entity.
30 Children []Entity
31 // Number of cells to indent children.
32 Indent int
33 }
34
35 func (ce *ContainerEntity) IsEmpty() bool {
36 return len(ce.Children) == 0
37 }
38
39 // PlainText returns the plaintext content in this entity and all its children.
40 func (ce *ContainerEntity) PlainText() string {
41 if len(ce.Children) == 0 {
42 return ""
43 }
44 var buf strings.Builder
45 newlined := false
46 for _, child := range ce.Children {
47 text := child.PlainText()
48 if !strings.HasPrefix(text, "\n") && child.IsBlock() && !newlined {
49 buf.WriteRune('\n')
50 }
51 newlined = false
52 buf.WriteString(text)
53 if child.IsBlock() {
54 if !strings.HasSuffix(text, "\n") {
55 buf.WriteRune('\n')
56 }
57 newlined = true
58 }
59 }
60 return strings.TrimSpace(buf.String())
61 }
62
63 // AdjustStyle recursively changes the style of this entity and all its children.
64 func (ce *ContainerEntity) AdjustStyle(fn AdjustStyleFunc, reason AdjustStyleReason) Entity {
65 for _, child := range ce.Children {
66 child.AdjustStyle(fn, reason)
67 }
68 ce.Style = fn(ce.Style)
69 return ce
70 }
71
72 // Clone creates a deep copy of this base entity.
73 func (ce *ContainerEntity) Clone() Entity {
74 children := make([]Entity, len(ce.Children))
75 for i, child := range ce.Children {
76 children[i] = child.Clone()
77 }
78 return &ContainerEntity{
79 BaseEntity: ce.BaseEntity.Clone().(*BaseEntity),
80 Children: children,
81 Indent: ce.Indent,
82 }
83 }
84
85 // String returns a textual representation of this BaseEntity struct.
86 func (ce *ContainerEntity) String() string {
87 if len(ce.Children) == 0 {
88 return fmt.Sprintf(`&html.ContainerEntity{Base=%s, Indent=%d, Children=[]}`, ce.BaseEntity, ce.Indent)
89 }
90 var buf strings.Builder
91 _, _ = fmt.Fprintf(&buf, `&html.ContainerEntity{Base=%s, Indent=%d, Children=[`, ce.BaseEntity, ce.Indent)
92 for _, child := range ce.Children {
93 buf.WriteString("\n ")
94 buf.WriteString(strings.Join(strings.Split(strings.TrimRight(child.String(), "\n"), "\n"), "\n "))
95 }
96 buf.WriteString("\n]},")
97 return buf.String()
98 }
99
100 // Draw draws this entity onto the given mauview Screen.
101 func (ce *ContainerEntity) Draw(screen mauview.Screen, ctx DrawContext) {
102 if len(ce.Children) == 0 {
103 return
104 }
105 width, _ := screen.Size()
106 prevBreak := false
107 proxyScreen := &mauview.ProxyScreen{Parent: screen, OffsetX: ce.Indent, Width: width - ce.Indent, Style: ce.Style}
108 for i, entity := range ce.Children {
109 if i != 0 && entity.getStartX() == 0 {
110 proxyScreen.OffsetY++
111 }
112 proxyScreen.Height = entity.Height()
113 entity.Draw(proxyScreen, ctx)
114 proxyScreen.SetStyle(ce.Style)
115 proxyScreen.OffsetY += entity.Height() - 1
116 _, isBreak := entity.(*BreakEntity)
117 if prevBreak && isBreak {
118 proxyScreen.OffsetY++
119 }
120 prevBreak = isBreak
121 }
122 }
123
124 // CalculateBuffer prepares this entity and all its children for rendering with the given parameters
125 func (ce *ContainerEntity) CalculateBuffer(width, startX int, ctx DrawContext) int {
126 ce.BaseEntity.CalculateBuffer(width, startX, ctx)
127 if len(ce.Children) > 0 {
128 ce.height = 0
129 childStartX := ce.startX
130 prevBreak := false
131 for _, entity := range ce.Children {
132 if entity.IsBlock() || childStartX == 0 || ce.height == 0 {
133 ce.height++
134 }
135 childStartX = entity.CalculateBuffer(width-ce.Indent, childStartX, ctx)
136 ce.height += entity.Height() - 1
137 _, isBreak := entity.(*BreakEntity)
138 if prevBreak && isBreak {
139 ce.height++
140 }
141 prevBreak = isBreak
142 }
143 if !ce.Block {
144 return childStartX
145 }
146 }
147 return ce.startX
148 }