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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
package layout
import (
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type SinglePaneLayout interface {
tea.Model
Focusable
Sizeable
Bindings
Pane() tea.Model
}
type singlePaneLayout struct {
width int
height int
focusable bool
focused bool
bordered bool
borderText map[BorderPosition]string
content tea.Model
padding []int
activeColor lipgloss.TerminalColor
}
type SinglePaneOption func(*singlePaneLayout)
func (s *singlePaneLayout) Init() tea.Cmd {
return s.content.Init()
}
func (s *singlePaneLayout) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
s.SetSize(msg.Width, msg.Height)
return s, nil
}
u, cmd := s.content.Update(msg)
s.content = u
return s, cmd
}
func (s *singlePaneLayout) View() string {
style := lipgloss.NewStyle().Width(s.width).Height(s.height)
if s.bordered {
style = style.Width(s.width - 2).Height(s.height - 2)
}
if s.padding != nil {
style = style.Padding(s.padding...)
}
content := style.Render(s.content.View())
if s.bordered {
if s.borderText == nil {
s.borderText = map[BorderPosition]string{}
}
if bordered, ok := s.content.(Bordered); ok {
s.borderText = bordered.BorderText()
}
return Borderize(content, BorderOptions{
Active: s.focused,
EmbeddedText: s.borderText,
})
}
return content
}
func (s *singlePaneLayout) Blur() tea.Cmd {
if s.focusable {
s.focused = false
}
if blurable, ok := s.content.(Focusable); ok {
return blurable.Blur()
}
return nil
}
func (s *singlePaneLayout) Focus() tea.Cmd {
if s.focusable {
s.focused = true
}
if focusable, ok := s.content.(Focusable); ok {
return focusable.Focus()
}
return nil
}
func (s *singlePaneLayout) SetSize(width, height int) {
s.width = width
s.height = height
childWidth, childHeight := s.width, s.height
if s.bordered {
childWidth -= 2
childHeight -= 2
}
if s.padding != nil {
if len(s.padding) == 1 {
childWidth -= s.padding[0] * 2
childHeight -= s.padding[0] * 2
} else if len(s.padding) == 2 {
childWidth -= s.padding[0] * 2
childHeight -= s.padding[1] * 2
} else if len(s.padding) == 3 {
childWidth -= s.padding[0] * 2
childHeight -= s.padding[1] + s.padding[2]
} else if len(s.padding) == 4 {
childWidth -= s.padding[0] + s.padding[2]
childHeight -= s.padding[1] + s.padding[3]
}
}
if s.content != nil {
if c, ok := s.content.(Sizeable); ok {
c.SetSize(childWidth, childHeight)
}
}
}
func (s *singlePaneLayout) IsFocused() bool {
return s.focused
}
func (s *singlePaneLayout) GetSize() (int, int) {
return s.width, s.height
}
func (s *singlePaneLayout) BindingKeys() []key.Binding {
if b, ok := s.content.(Bindings); ok {
return b.BindingKeys()
}
return []key.Binding{}
}
func (s *singlePaneLayout) Pane() tea.Model {
return s.content
}
func NewSinglePane(content tea.Model, opts ...SinglePaneOption) SinglePaneLayout {
layout := &singlePaneLayout{
content: content,
}
for _, opt := range opts {
opt(layout)
}
return layout
}
func WithSinglePaneSize(width, height int) SinglePaneOption {
return func(opts *singlePaneLayout) {
opts.width = width
opts.height = height
}
}
func WithSinglePaneFocusable(focusable bool) SinglePaneOption {
return func(opts *singlePaneLayout) {
opts.focusable = focusable
}
}
func WithSinglePaneBordered(bordered bool) SinglePaneOption {
return func(opts *singlePaneLayout) {
opts.bordered = bordered
}
}
func WithSinglePaneBorderText(borderText map[BorderPosition]string) SinglePaneOption {
return func(opts *singlePaneLayout) {
opts.borderText = borderText
}
}
func WithSinglePanePadding(padding ...int) SinglePaneOption {
return func(opts *singlePaneLayout) {
opts.padding = padding
}
}
func WithSinglePaneActiveColor(color lipgloss.TerminalColor) SinglePaneOption {
return func(opts *singlePaneLayout) {
opts.activeColor = color
}
}
|