Debian Codegraph @

ui/messages/html/list.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 "maunium.net/go/gomuks/ui/widget"
26 "maunium.net/go/mautrix/format"
27 )
28
29 type ListEntity struct {
30 *ContainerEntity
31 Ordered bool
32 Start int
33 }
34
35 func NewListEntity(ordered bool, start int, children []Entity) *ListEntity {
36 entity := &ListEntity{
37 ContainerEntity: &ContainerEntity{
38 BaseEntity: &BaseEntity{
39 Tag: "ul",
40 Block: true,
41 },
42 Indent: 2,
43 Children: children,
44 },
45 Ordered: ordered,
46 Start: start,
47 }
48 if ordered {
49 entity.Tag = "ol"
50 entity.Indent += format.Digits(start + len(children) - 1)
51 }
52 return entity
53 }
54
55 func (le *ListEntity) AdjustStyle(fn AdjustStyleFunc, reason AdjustStyleReason) Entity {
56 le.BaseEntity = le.BaseEntity.AdjustStyle(fn, reason).(*BaseEntity)
57 le.ContainerEntity.AdjustStyle(fn, reason)
58 return le
59 }
60
61 func (le *ListEntity) Clone() Entity {
62 return &ListEntity{
63 ContainerEntity: le.ContainerEntity.Clone().(*ContainerEntity),
64 Ordered: le.Ordered,
65 Start: le.Start,
66 }
67 }
68
69 func (le *ListEntity) paddingFor(number int) string {
70 padding := le.Indent - 2 - format.Digits(number)
71 if padding <= 0 {
72 return ""
73 }
74 return strings.Repeat(" ", padding)
75 }
76
77 func (le *ListEntity) Draw(screen mauview.Screen, ctx DrawContext) {
78 width, _ := screen.Size()
79
80 proxyScreen := &mauview.ProxyScreen{Parent: screen, OffsetX: le.Indent, Width: width - le.Indent, Style: le.Style}
81 for i, entity := range le.Children {
82 proxyScreen.Height = entity.Height()
83 if le.Ordered {
84 number := le.Start + i
85 line := fmt.Sprintf("%d. %s", number, le.paddingFor(number))
86 widget.WriteLine(screen, mauview.AlignLeft, line, 0, proxyScreen.OffsetY, le.Indent, le.Style)
87 } else {
88 screen.SetContent(0, proxyScreen.OffsetY, '●', nil, le.Style)
89 }
90 entity.Draw(proxyScreen, ctx)
91 proxyScreen.SetStyle(le.Style)
92 proxyScreen.OffsetY += entity.Height()
93 }
94 }
95
96 func (le *ListEntity) PlainText() string {
97 if len(le.Children) == 0 {
98 return ""
99 }
100 var buf strings.Builder
101 for i, child := range le.Children {
102 indent := strings.Repeat(" ", le.Indent)
103 if le.Ordered {
104 number := le.Start + i
105 _, _ = fmt.Fprintf(&buf, "%d. %s", number, le.paddingFor(number))
106 } else {
107 buf.WriteString("● ")
108 }
109 for j, row := range strings.Split(child.PlainText(), "\n") {
110 if j != 0 {
111 buf.WriteRune('\n')
112 buf.WriteString(indent)
113 }
114 buf.WriteString(row)
115 }
116 buf.WriteRune('\n')
117 }
118 return strings.TrimSpace(buf.String())
119 }
120
121 func (le *ListEntity) String() string {
122 return fmt.Sprintf("&html.ListEntity{Ordered=%t, Start=%d, Base=%s},\n", le.Ordered, le.Start, le.BaseEntity)
123 }