/ veet / internal / ui / table.go
swadhinbiswas feat: live scan matrix, safety vacuuming & theme engine · 8/17/2026
30800be
254 lines · 5.7 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package ui

import (
	"fmt"

	"github.com/charmbracelet/bubbles/key"
	"github.com/charmbracelet/bubbles/table"
	tea "github.com/charmbracelet/bubbletea"
	"github.com/charmbracelet/lipgloss"

	"github.com/swadhinbiswas/veet/internal/model"
)

// AppTable wraps the bubbles table with checkbox + source badge columns.
type AppTable struct {
	table table.Model
	// apps mirrors the filtered rows so selection can map back to AppInfo.
	apps []*model.AppInfo
	// selected tracks chosen apps by name.
	selected map[string]bool
	lastW    int
	lastH    int
}

// NewAppTable builds the main list.
func NewAppTable() *AppTable {
	cols := defaultColumns(80)
	km := table.DefaultKeyMap()
	km.PageDown = key.NewBinding(
		key.WithKeys("down", "pgdown"),
		key.WithHelp("↓/pgdn", "page down"),
	)
	t := table.New(
		table.WithColumns(cols),
		table.WithHeight(8),
		table.WithFocused(true),
		table.WithKeyMap(km),
	)
	s := table.DefaultStyles()
	s.Header = lipgloss.NewStyle().Bold(true).Foreground(primary).Padding(0, 1)
	s.Selected = lipgloss.NewStyle().
		Foreground(lipgloss.Color("#FFFFFF")).
		Background(lipgloss.Color("#1E3A8A")).
		Bold(true)
	s.Cell = lipgloss.NewStyle().Foreground(neutral).Padding(0, 1)
	t.SetStyles(s)
	return &AppTable{table: t, selected: map[string]bool{}}
}

func defaultColumns(width int) []table.Column {
	avail := width - 6
	checkW := 3
	sourceW := 12
	dateW := 12
	sizeW := 10
	verW := 12
	if avail > 75 {
		verW = 16
	}
	nameW := avail - checkW - verW - sourceW - dateW - sizeW - 6
	if nameW < 18 {
		nameW = 18
	}

	return []table.Column{
		{Title: "", Width: checkW},
		{Title: "App/Package", Width: nameW},
		{Title: "Version", Width: verW},
		{Title: "Source", Width: sourceW},
		{Title: "Installed", Width: dateW},
		{Title: "Size", Width: sizeW},
	}
}

// SetApps replaces the visible (filtered) rows.
func (a *AppTable) SetApps(apps []*model.AppInfo) {
	a.apps = apps
	rows := make([]table.Row, len(apps))
	for i, app := range apps {
		rows[i] = a.row(app)
	}
	a.table.SetRows(rows)
	if a.table.Cursor() >= len(rows) && len(rows) > 0 {
		a.table.SetCursor(len(rows) - 1)
	}
	if len(rows) == 0 {
		a.table.SetCursor(0)
	}
	a.table.UpdateViewport()
}

func (a *AppTable) row(app *model.AppInfo) table.Row {
	sym := model.GetUISymbols()
	check := "[ ]"
	if a.selected[app.Name] {
		check = "[" + sym.Check + "]"
	}
	name := app.Name
	if app.Protected {
		name = sym.Shield + " " + name
	}
	inst := "-"
	if !app.InstalledOn.IsZero() {
		inst = app.InstalledOn.Format("2006-01-02")
	}
	meta := model.Meta(app.Source)
	sourceDisplay := meta.Icon + " " + meta.Label
	return table.Row{
		check,
		name,
		app.Version,
		sourceDisplay,
		inst,
		model.HumanSize(app.InstallSizeKB),
	}
}

// Selected reports whether the app at a given row index is checked.
func (a *AppTable) IsSelected(i int) bool {
	if i < 0 || i >= len(a.apps) {
		return false
	}
	return a.selected[a.apps[i].Name]
}

// Toggle flips selection for the current row; returns the app.
func (a *AppTable) Toggle() *model.AppInfo {
	app := a.Current()
	if app == nil {
		return nil
	}
	if app.Protected {
		return app // caller shows refusal note
	}
	if a.selected[app.Name] {
		delete(a.selected, app.Name)
	} else {
		a.selected[app.Name] = true
	}
	a.SetApps(a.apps)
	return app
}

// SelectAll checks every visible (filtered) app; protected ones are skipped.
// Returns the number of protected apps that were refused.
func (a *AppTable) SelectAll() int {
	refused := 0
	all := true
	for _, app := range a.apps {
		if app.Protected {
			refused++
			continue
		}
		if !a.selected[app.Name] {
			all = false
		}
	}
	if all {
		for _, app := range a.apps {
			if !app.Protected {
				delete(a.selected, app.Name)
			}
		}
	} else {
		for _, app := range a.apps {
			if !app.Protected {
				a.selected[app.Name] = true
			}
		}
	}
	a.SetApps(a.apps)
	return refused
}

// Current returns the app under the cursor.
func (a *AppTable) Current() *model.AppInfo {
	i := a.table.Cursor()
	if i < 0 || i >= len(a.apps) {
		return nil
	}
	return a.apps[i]
}

// SelectedApps returns all checked apps in row order.
func (a *AppTable) SelectedApps() []*model.AppInfo {
	var out []*model.AppInfo
	for _, app := range a.apps {
		if a.selected[app.Name] {
			out = append(out, app)
		}
	}
	return out
}

// Count returns the number of visible rows.
func (a *AppTable) Count() int { return len(a.apps) }

// Model exposes the underlying bubbles table.
func (a *AppTable) Model() *table.Model { return &a.table }

// Update forwards a message to the bubbles table, keeping the returned model.
func (a *AppTable) Update(msg tea.Msg) tea.Cmd {
	t, cmd := a.table.Update(msg)
	a.table = t
	return cmd
}

// Summary renders "N selected / Total / Reclaimable".
func (a *AppTable) Summary(extra string) string {
	sel := a.SelectedApps()
	total := int64(0)
	reclaim := int64(0)
	for _, app := range sel {
		total += app.InstallSizeKB
		if app.Removable.Staged() {
			reclaim += app.Removable.TotalKB()
		}
	}
	selCount := fmt.Sprintf("%d selected", len(sel))
	if len(sel) > 0 {
		selCount = HighlightText.Render(selCount)
	} else {
		selCount = Muted.Render(selCount)
	}
	line := fmt.Sprintf(" %s   Total: %s   Reclaimable: %s",
		selCount,
		InfoText.Render(model.HumanSize(total)),
		HighlightText.Render(model.HumanSize(reclaim)))
	if extra != "" {
		line += "   " + extra
	}
	return line
}

// Resize adjusts column widths and table dimensions.
func (a *AppTable) Resize(width, height int) {
	if width != a.lastW && width > 0 {
		a.lastW = width
		a.table.SetColumns(defaultColumns(width))
		a.table.SetWidth(width)
	}
	if height != a.lastH && height > 0 {
		a.lastH = height
		a.table.SetHeight(height)
	}
	a.table.UpdateViewport()
}

// View renders the table panel.
func (a *AppTable) View(width, height int) string {
	a.Resize(width, height)
	return PanelFocus.Render(a.table.View())
}