-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext.go
More file actions
90 lines (79 loc) · 1.88 KB
/
text.go
File metadata and controls
90 lines (79 loc) · 1.88 KB
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
package console
import (
"bytes"
"errors"
"fmt"
"io"
)
type Text struct {
Stylesheet Stylesheet
Text []Cell
}
func (t *Text) Add(s string, fg, bg Color) (err error) {
text_cells := make([]Cell, string_cell_count(s))
_, err = WriteText(text_cells, s, fg, bg)
if err != nil {
return
}
t.Text = append(t.Text, text_cells...)
return
}
func (t *Text) Render(line []Cell) (n int, err error) {
margin_left := t.Stylesheet.Margin[Left]
margin_right := t.Stylesheet.Margin[Right]
if t.Stylesheet.Width < margin_left+margin_right {
err = fmt.Errorf("console: not enough width to render text margins")
return
}
text_cells := make([]Cell, t.Stylesheet.Width)
copy(text_cells[margin_left:len(text_cells)-margin_right], t.Text)
// set margins
for i := 0; i < margin_left; i++ {
text_cells[i] = t.Stylesheet.Sequence[Left]
}
right_margin_index := len(text_cells) - margin_right
for i := 0; i < t.Stylesheet.Margin[Right]; i++ {
text_cells[right_margin_index+i] = t.Stylesheet.Sequence[Right]
}
// copy based on alignment
var start_index int
switch t.Stylesheet.Alignment {
case Left:
start_index = 0
case Right:
start_index = len(line) - t.Stylesheet.Width
case Center:
start_index = (len(line) / 2) - (t.Stylesheet.Width / 2)
}
n = copy(line[start_index:], text_cells)
return
}
func WriteText(text []Cell, s string, fg, bg Color) (n int, err error) {
buffer := bytes.NewBufferString(s)
var r rune
var padding int
for i := 0; i < len(text); i++ {
// we may need to insert padding cells
// if a previous cell was wider than 1
if padding > 0 {
text[i].Rune = 0x80
n = i + 1
padding--
continue
}
r, _, err = buffer.ReadRune()
if errors.Is(err, io.EOF) {
err = nil
break
}
text[i].Rune = r
text[i].Fg = fg
text[i].Bg = bg
n = i + 1
cell_width := rune_cell_count(r)
if cell_width > 1 {
padding = cell_width - 1
}
}
return
}