This repository has been archived by the owner on May 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
171 lines (140 loc) · 4.23 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"fmt"
"log"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/evertras/bubble-table/table"
)
// Column field labels
const (
columnKeyID = "id"
columnKeyName = "name"
columnKeyStatus = "status"
columnKeyImage = "image"
)
type Model struct {
tableModel table.Model
}
// getDataRows calls getDistroBoxItems() from distrobox.go
// and returns the info as a slice of table.Row
func getDataRows() (rows []table.Row) {
distroBoxItems := getDistroboxItems()
for _, v := range distroBoxItems {
row := table.NewRow(table.RowData{
columnKeyID: v.id,
columnKeyName: v.name,
columnKeyStatus: v.status,
columnKeyImage: v.image,
})
rows = append(rows, row)
}
return rows
}
func NewModel() Model {
columns := []table.Column{
table.NewColumn(columnKeyID, "ID", 15).WithStyle(idColumnStyle),
table.NewColumn(columnKeyName, "Name", 30),
table.NewColumn(columnKeyStatus, "Status", 30),
table.NewColumn(columnKeyImage, "Image", 70).WithStyle(imageColumnStyle),
}
rows := getDataRows()
keys := table.DefaultKeyMap()
keys.RowDown.SetKeys("j", "down")
keys.RowUp.SetKeys("k", "up")
model := Model{
tableModel: table.New(columns).
WithRows(rows).
HeaderStyle(headerStyle).
SelectableRows(false).
Focused(true).
Border(customBorder).
WithKeyMap(keys).
WithPageSize(10).
WithBaseStyle(baseStyle).
HighlightStyle(highlightStyle).
SortByAsc(columnKeyName),
}
model.updateFooter()
return model
}
func (m Model) Init() tea.Cmd {
return nil
}
// updateFooter() updates the View's footer when a row is selected
// by the user. The columnKeyName of the data in that row will be
// displayed in the footer.
func (m *Model) updateFooter() {
highlightedRow := m.tableModel.HighlightedRow()
footerFormatStr := fmt.Sprintf(
"[Pg. %d/%d] Current selection: ",
m.tableModel.CurrentPage(),
m.tableModel.MaxPages())
var boxNameFmtStr string
if highlightedRow.Data == nil {
boxNameFmtStr = "No distroboxes available"
} else {
boxNameFmtStr = fmt.Sprintf("%s", highlightedRow.Data[columnKeyName])
}
footerText := lipgloss.JoinHorizontal(
0, footerStyle.Render(footerFormatStr),
footerBoxNameStyle.Render(boxNameFmtStr),
)
m.tableModel = m.tableModel.WithStaticFooter(footerText)
}
// Update() listens and processes input from the user and executes
// actions according to what keybindings the user inputs
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var (
cmd tea.Cmd
cmds []tea.Cmd
)
m.tableModel, cmd = m.tableModel.Update(msg)
cmds = append(cmds, cmd)
// We control the footer text, so make sure to update it
m.updateFooter()
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "Q":
cmds = append(cmds, tea.Quit)
cmds = append(cmds, clearScreen())
case "enter":
boxName := m.tableModel.HighlightedRow().Data[columnKeyName].(string)
cmds = append(cmds, enterDistroBox(boxName))
cmds = append(cmds, clearScreen())
case "S":
boxName := m.tableModel.HighlightedRow().Data[columnKeyName].(string)
cmds = append(cmds, stopDistroBox(boxName))
case "X", "delete":
boxName := m.tableModel.HighlightedRow().Data[columnKeyName].(string)
cmds = append(cmds, removeDistroBox(boxName))
case "R":
m = NewModel()
}
}
return m, tea.Batch(cmds...)
}
// View() defines what is displayed in the user interface
func (m Model) View() string {
view := lipgloss.JoinVertical(
lipgloss.Left,
titleStyle.MarginTop(1).MarginBottom(1).MarginLeft(1).Render("DistroBox TUI"),
lipgloss.JoinHorizontal(
0, subtleStyle.MarginRight(1).Render("[Left/right: change page]"),
subtleStyle.MarginLeft(1).MarginRight(1).Render("[Up/down, j/k: move]"),
subtleStyle.MarginLeft(1).MarginRight(1).Render("[Enter: enter distrobox]"),
subtleStyle.MarginLeft(1).MarginRight(1).Render("[S: stop]"),
subtleStyle.MarginLeft(1).MarginRight(1).Render("[X: remove]"),
subtleStyle.MarginLeft(1).MarginRight(1).Render("[R: refresh]"),
subtleStyle.MarginLeft(1).Render("[Q: quit]")),
m.tableModel.View(),
)
return lipgloss.NewStyle().MarginLeft(1).Render(view)
}
func main() {
p := tea.NewProgram(NewModel(), tea.WithAltScreen())
if err := p.Start(); err != nil {
log.Fatal(err)
}
}