-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtitle.go
63 lines (50 loc) · 1.1 KB
/
title.go
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
package tooey
import (
"fmt"
"strings"
"github.com/gdamore/tcell/v2"
)
// NewTitle returns a basic empty title
func NewTitle(theme *Theme) *Title {
if theme == nil {
theme = DefaultTheme
}
return &Title{
Padding: NewTitlePadding(),
Theme: theme,
}
}
// Title represents a rendered title in the header of an Element
type Title struct {
Content string
Padding *Padding
Theme *Theme
}
// Draw the title
func (t *Title) Draw(s tcell.Screen, rect *Rectangle) {
if len(t.Content) == 0 {
return
}
w := rect.DrawableWidth()
//draw := TrimString(t.Content, w-1)
row := rect.Y1()
col := rect.X1() + rect.Padding.Left
leftPad := ""
if t.Padding.Left > 0 {
leftPad = strings.Repeat(" ", t.Padding.Left)
}
rightPad := ""
if t.Padding.Right > 0 {
rightPad = strings.Repeat(" ", t.Padding.Right)
}
draw := TrimString(t.Content, w-len(leftPad)-len(rightPad)-1)
draw = fmt.Sprintf("%s%s%s", leftPad, draw, rightPad)
for _, r := range draw {
s.SetContent(col, row, r, nil, t.Theme.Title.Style)
col++
if col+1 > rect.X2()-rect.Padding.Right {
// add ... at some point
break
}
}
}