-
Notifications
You must be signed in to change notification settings - Fork 21
/
main.go
148 lines (121 loc) · 3.24 KB
/
main.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
package main
import (
"fmt"
"log"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/evertras/bubble-table/table"
)
const (
columnKeyName = "name"
columnKeyElement = "element"
columnKeyDescription = "description"
minWidth = 30
minHeight = 8
// Add a fixed margin to account for description & instructions
fixedVerticalMargin = 4
)
type Model struct {
flexTable table.Model
// Window dimensions
totalWidth int
totalHeight int
// Table dimensions
horizontalMargin int
verticalMargin int
}
func NewModel() Model {
return Model{
flexTable: table.New([]table.Column{
table.NewColumn(columnKeyName, "Name", 10),
// This table uses flex columns, but it will still need a target
// width in order to know what width it should fill. In this example
// the target width is set below in `recalculateTable`, which sets
// the table to the width of the screen to demonstrate resizing
// with flex columns.
table.NewFlexColumn(columnKeyElement, "Element", 1),
table.NewFlexColumn(columnKeyDescription, "Description", 3),
}).WithRows([]table.Row{
table.NewRow(table.RowData{
columnKeyName: "Pikachu",
columnKeyElement: "Electric",
columnKeyDescription: "Super zappy mouse, handle with care",
}),
table.NewRow(table.RowData{
columnKeyName: "Charmander",
columnKeyElement: "Fire",
columnKeyDescription: "直立した恐竜のような身体と、尻尾の先端に常に燃えている炎が特徴。",
}),
}).WithStaticFooter("A footer!"),
}
}
func (m Model) Init() tea.Cmd {
return nil
}
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var (
cmd tea.Cmd
cmds []tea.Cmd
)
m.flexTable, cmd = m.flexTable.Update(msg)
cmds = append(cmds, cmd)
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "esc", "q":
cmds = append(cmds, tea.Quit)
case "left":
if m.calculateWidth() > minWidth {
m.horizontalMargin++
m.recalculateTable()
}
case "right":
if m.horizontalMargin > 0 {
m.horizontalMargin--
m.recalculateTable()
}
case "up":
if m.calculateHeight() > minHeight {
m.verticalMargin++
m.recalculateTable()
}
case "down":
if m.verticalMargin > 0 {
m.verticalMargin--
m.recalculateTable()
}
}
case tea.WindowSizeMsg:
m.totalWidth = msg.Width
m.totalHeight = msg.Height
m.recalculateTable()
}
return m, tea.Batch(cmds...)
}
func (m *Model) recalculateTable() {
m.flexTable = m.flexTable.
WithTargetWidth(m.calculateWidth()).
WithMinimumHeight(m.calculateHeight())
}
func (m Model) calculateWidth() int {
return m.totalWidth - m.horizontalMargin
}
func (m Model) calculateHeight() int {
return m.totalHeight - m.verticalMargin - fixedVerticalMargin
}
func (m Model) View() string {
strs := []string{
"A flexible table that fills available space (Name column is fixed-width)",
fmt.Sprintf("Target size: %d W ⨉ %d H (arrow keys to adjust)",
m.calculateWidth(), m.calculateHeight()),
"Press q or ctrl+c to quit",
m.flexTable.View(),
}
return lipgloss.JoinVertical(lipgloss.Left, strs...) + "\n"
}
func main() {
p := tea.NewProgram(NewModel())
if err := p.Start(); err != nil {
log.Fatal(err)
}
}