-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple.go
More file actions
74 lines (54 loc) · 1.72 KB
/
simple.go
File metadata and controls
74 lines (54 loc) · 1.72 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
package orvyn
import (
"github.com/charmbracelet/lipgloss"
)
// SimpleRenderable represents a very basic and reusable renderable.
type SimpleRenderable struct {
// BaseRenderable composing
BaseRenderable
// Style is the style that will be used to render the value of the SimpleRenderable.
Style lipgloss.Style
// SizeConstraint define if the style width and height should be taken into account when rendering the SimpleRenderable.
SizeConstraint bool
// value is the value of the SimpleRenderable.
value string
}
// VGap is a SimpleRenderable representing a new line. Useful for layout building.
var VGap = NewSimpleRenderable("\n")
// NewSimpleRenderable creates a new SimpleRenderable and returns it.
func NewSimpleRenderable(value string) *SimpleRenderable {
s := new(SimpleRenderable)
s.BaseRenderable = NewBaseRenderable()
s.value = value
s.Style = lipgloss.NewStyle()
s.SizeConstraint = false
return s
}
// SetValue changes the current value with the given one.
func (s *SimpleRenderable) SetValue(value string) {
s.value = value
}
func (s *SimpleRenderable) Render() string {
if !s.SizeConstraint {
return s.Style.Render(s.value)
}
size := s.GetSize()
size.Width -= s.Style.GetHorizontalFrameSize()
size.Height -= s.Style.GetVerticalFrameSize()
return s.Style.Width(size.Width).
Height(size.Height).Render(s.value)
}
func (s *SimpleRenderable) GetMinSize() Size {
vFrame, hFrame := s.Style.GetFrameSize()
if vFrame+hFrame == 0 {
s.BaseRenderable.GetMinSize()
}
return GetRenderSize(s.Style, "min")
}
func (s *SimpleRenderable) GetPreferredSize() Size {
vFrame, hFrame := s.Style.GetFrameSize()
if vFrame+hFrame == 0 {
s.BaseRenderable.GetPreferredSize()
}
return GetRenderSize(s.Style, "pref")
}