-
Notifications
You must be signed in to change notification settings - Fork 134
/
layout.go
167 lines (136 loc) Β· 3.54 KB
/
layout.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
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
package huh
import (
"strings"
"github.com/charmbracelet/lipgloss"
)
// A Layout is responsible for laying out groups in a form.
type Layout interface {
View(f *Form) string
GroupWidth(f *Form, g *Group, w int) int
}
// Default layout shows a single group at a time.
var LayoutDefault Layout = &layoutDefault{}
// Stack layout stacks all groups on top of each other.
var LayoutStack Layout = &layoutStack{}
// Column layout distributes groups in even columns.
func LayoutColumns(columns int) Layout {
return &layoutColumns{columns: columns}
}
// Grid layout distributes groups in a grid.
func LayoutGrid(rows int, columns int) Layout {
return &layoutGrid{rows: rows, columns: columns}
}
type layoutDefault struct{}
func (l *layoutDefault) View(f *Form) string {
return f.selector.Selected().View()
}
func (l *layoutDefault) GroupWidth(_ *Form, _ *Group, w int) int {
return w
}
type layoutColumns struct {
columns int
}
func (l *layoutColumns) visibleGroups(f *Form) []*Group {
segmentIndex := f.selector.Index() / l.columns
start := segmentIndex * l.columns
end := start + l.columns
total := f.selector.Total()
if end > total {
end = total
}
var groups []*Group
f.selector.Range(func(i int, group *Group) bool {
if i >= start && i < end {
groups = append(groups, group)
return true
}
return false
})
return groups
}
func (l *layoutColumns) View(f *Form) string {
groups := l.visibleGroups(f)
if len(groups) == 0 {
return ""
}
columns := make([]string, 0, len(groups))
for _, group := range groups {
columns = append(columns, group.Content())
}
footer := f.selector.Selected().Footer()
return lipgloss.JoinVertical(lipgloss.Left,
lipgloss.JoinHorizontal(lipgloss.Top, columns...),
footer,
)
}
func (l *layoutColumns) GroupWidth(_ *Form, _ *Group, w int) int {
return w / l.columns
}
type layoutStack struct{}
func (l *layoutStack) View(f *Form) string {
var columns []string
f.selector.Range(func(_ int, group *Group) bool {
columns = append(columns, group.Content())
return true
})
footer := f.selector.Selected().Footer()
var view strings.Builder
view.WriteString(strings.Join(columns, "\n"))
view.WriteString(footer)
return view.String()
}
func (l *layoutStack) GroupWidth(_ *Form, _ *Group, w int) int {
return w
}
type layoutGrid struct {
rows, columns int
}
func (l *layoutGrid) visibleGroups(f *Form) [][]*Group {
total := l.rows * l.columns
segmentIndex := f.selector.Index() / total
start := segmentIndex * total
end := start + total
if glen := f.selector.Total(); end > glen {
end = glen
}
var visible []*Group
f.selector.Range(func(i int, group *Group) bool {
if i >= start && i < end {
visible = append(visible, group)
return true
}
return false
})
grid := make([][]*Group, l.rows)
for i := 0; i < l.rows; i++ {
startRow := i * l.columns
endRow := startRow + l.columns
if startRow >= len(visible) {
break
}
if endRow > len(visible) {
endRow = len(visible)
}
grid[i] = visible[startRow:endRow]
}
return grid
}
func (l *layoutGrid) View(f *Form) string {
grid := l.visibleGroups(f)
if len(grid) == 0 {
return ""
}
rows := make([]string, 0, len(grid))
for _, row := range grid {
var columns []string
for _, group := range row {
columns = append(columns, group.Content())
}
rows = append(rows, lipgloss.JoinHorizontal(lipgloss.Top, columns...))
}
footer := f.selector.Selected().Footer()
return lipgloss.JoinVertical(lipgloss.Left, strings.Join(rows, "\n"), footer)
}
func (l *layoutGrid) GroupWidth(_ *Form, _ *Group, w int) int {
return w / l.columns
}