aboutsummaryrefslogtreecommitdiff
path: root/ui/messages/html/text.go
blob: ea8f7374facd81559c7229051752ffb4d0934f4d (plain)
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
146
// gomuks - A terminal Matrix client written in Go.
// Copyright (C) 2019 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"
	"regexp"

	"github.com/mattn/go-runewidth"

	"maunium.net/go/gomuks/ui/widget"
	"maunium.net/go/mauview"
)

type TextEntity struct {
	*BaseEntity
	// Text in this entity.
	Text string

	buffer []string
}

// NewTextEntity creates a new text-only Entity.
func NewTextEntity(text string) *TextEntity {
	return &TextEntity{
		BaseEntity: &BaseEntity{
			Tag: "text",
		},
		Text: text,
	}
}

func (te *TextEntity) Clone() Entity {
	return &TextEntity{
		BaseEntity: te.BaseEntity.Clone().(*BaseEntity),
		Text:       te.Text,
	}
}

func (te *TextEntity) PlainText() string {
	return te.Text
}

func (te *TextEntity) String() string {
	return fmt.Sprintf("&html.TextEntity{Text=%s, Base=%s},\n", te.Text, te.BaseEntity)
}

func (te *TextEntity) Draw(screen mauview.Screen) {
	width, _ := screen.Size()
	x := te.startX
	for y, line := range te.buffer {
		widget.WriteLine(screen, mauview.AlignLeft, line, x, y, width, te.Style)
		x = 0
	}
}

func (te *TextEntity) CalculateBuffer(width, startX int, bare bool) int {
	te.BaseEntity.CalculateBuffer(width, startX, bare)
	if len(te.Text) == 0 {
		return te.startX
	}
	te.height = 0
	te.prevWidth = width
	if te.buffer == nil {
		te.buffer = []string{}
	}
	bufPtr := 0
	text := te.Text
	textStartX := te.startX
	for {
		// TODO add option no wrap and character wrap options
		extract := runewidth.Truncate(text, width-textStartX, "")
		extract, wordWrapped := trim(extract, text, bare)
		if !wordWrapped && textStartX > 0 {
			if bufPtr < len(te.buffer) {
				te.buffer[bufPtr] = ""
			} else {
				te.buffer = append(te.buffer, "")
			}
			bufPtr++
			textStartX = 0
			continue
		}
		if bufPtr < len(te.buffer) {
			te.buffer[bufPtr] = extract
		} else {
			te.buffer = append(te.buffer, extract)
		}
		bufPtr++
		text = text[len(extract):]
		if len(text) == 0 {
			te.buffer = te.buffer[:bufPtr]
			te.height += len(te.buffer)
			// This entity is over, return the startX for the next entity
			if te.Block {
				// ...except if it's a block entity
				return 0
			}
			return textStartX + runewidth.StringWidth(extract)
		}
		textStartX = 0
	}
}

var (
	boundaryPattern     = regexp.MustCompile(`([[:punct:]]\s*|\s+)`)
	bareBoundaryPattern = regexp.MustCompile(`(\s+)`)
	spacePattern        = regexp.MustCompile(`\s+`)
)

func trim(extract, full string, bare bool) (string, bool) {
	if len(extract) == len(full) {
		return extract, true
	}
	if spaces := spacePattern.FindStringIndex(full[len(extract):]); spaces != nil && spaces[0] == 0 {
		extract = full[:len(extract)+spaces[1]]
	}
	regex := boundaryPattern
	if bare {
		regex = bareBoundaryPattern
	}
	matches := regex.FindAllStringIndex(extract, -1)
	if len(matches) > 0 {
		if match := matches[len(matches)-1]; len(match) >= 2 {
			if until := match[1]; until < len(extract) {
				extract = extract[:until]
				return extract, true
			}
		}
	}
	return extract, len(extract) > 0 && extract[len(extract)-1] == ' '
}