forked from charmbracelet/lipgloss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget.go
83 lines (72 loc) · 1.32 KB
/
get.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package lipgloss
import (
"strings"
"github.com/muesli/reflow/ansi"
)
// Returns whether or not the given property is set.
func (s Style) isSet(k propKey) bool {
_, exists := s.rules[k]
return exists
}
func (s Style) getAsBool(k propKey, defaultVal bool) bool {
v, ok := s.rules[k]
if !ok {
return defaultVal
}
if b, ok := v.(bool); ok {
return b
}
return defaultVal
}
func (s Style) getAsColor(k propKey) TerminalColor {
v, ok := s.rules[k]
if !ok {
return NoColor{}
}
if c, ok := v.(TerminalColor); ok {
return c
}
return NoColor{}
}
func (s Style) getAsInt(k propKey) int {
v, ok := s.rules[k]
if !ok {
return 0
}
if i, ok := v.(int); ok {
return i
}
return 0
}
func (s Style) getAsPosition(k propKey) Position {
v, ok := s.rules[k]
if !ok {
return Position(0)
}
if p, ok := v.(Position); ok {
return p
}
return Position(0)
}
func (s Style) getAsBorderStyle(k propKey) Border {
v, ok := s.rules[k]
if !ok {
return noBorder
}
if b, ok := v.(Border); ok {
return b
}
return noBorder
}
// Split a string into lines, additionally returning the size of the widest
// line.
func getLines(s string) (lines []string, widest int) {
lines = strings.Split(s, "\n")
for _, l := range lines {
w := ansi.PrintableRuneWidth(l)
if widest < w {
widest = w
}
}
return lines, widest
}