blob: d88e35bf49793dba8ac8c42068cb40a105a1eef2 [file] [log] [blame]
Serge Bazanski0d9e1252024-09-03 12:16:47 +02001package tconsole
2
3import (
4 "strings"
5
6 "github.com/gdamore/tcell/v2"
7 "github.com/rivo/uniseg"
8)
9
10// drawText draws a single line of text from left to right, starting at x and y.
11func (c *Console) drawText(x, y int, text string, style tcell.Style) {
12 g := uniseg.NewGraphemes(text)
13 for g.Next() {
14 runes := g.Runes()
15 c.screen.SetContent(x, y, runes[0], runes[1:], style)
16 x += 1
17 }
18}
19
20// drawTextCentered draw a single line of text from left to right, starting at a
21// position so that the center of the line ends up at x and y.
22func (c *Console) drawTextCentered(x, y int, text string, style tcell.Style) {
23 g := uniseg.NewGraphemes(text)
24 var runes [][]rune
25 for g.Next() {
26 runes = append(runes, g.Runes())
27 }
28
29 x -= len(runes) / 2
30
31 for _, r := range runes {
32 c.screen.SetContent(x, y, r[0], r[1:], style)
33 x += 1
34 }
35}
36
37// fillRectangle fills a given rectangle [x0,x1) [y0,y1) with empty space of a
38// given style.
39func (c *Console) fillRectangle(x0, x1, y0, y1 int, style tcell.Style) {
40 for x := x0; x < x1; x++ {
41 for y := y0; y < y1; y++ {
42 c.screen.SetContent(x, y, ' ', nil, style)
43 }
44 }
45}
46
47const logo = `
48 _g@@@@g_ _g@@@@g_
49 _@@@@@@@@@@a g@@@@@@@@@@b
50 @@@@@@@@@@@@@@___@@@@@@@@@@@@@@
51 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
52 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
53 g@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@g
54 g@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@g
55 g@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@g
56 ;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@,
57 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
58 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
59 %@@@@@@@@@@P<@@@@@@@@@@@@@>%@@@@@@@@@@P
60 "+B@@B>' "@@@@@@@@B" '<B@@BP"
61 '"
62`
63
64// drawLogo draws the Monogon logo so that its top left corner is at x, y.
65func (c *Console) drawLogo(x, y int, style tcell.Style) {
66 for i, line := range strings.Split(logo, "\n") {
67 c.drawText(x, y+i, line, style)
68 }
69}
70
71// split calculates a mid-point in the [0, capacity) domain so that it splits it
72// into two parts fairly with minA and minB used as minimum size hints for each
73// section.
74func split(capacity, minA, minB int) int {
75 slack := capacity - (minA + minB)
76 propA := float64(minA) / float64(minA+minB)
77 slackA := int(propA * float64(slack))
78 return minA + slackA
79}
80
81// center calculates a point at which to start drawing a 'size'-sized element in
82// a 'capacity'-sized container so that it ends in the middle of said container.
83func center(capacity, size int) int {
84 return (capacity - size) / 2
85}