-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtui.go
390 lines (306 loc) · 9.63 KB
/
tui.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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package main
import (
"context"
"fmt"
"os"
"sort"
"strconv"
"strings"
"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
trans "github.com/hekmon/transmissionrpc/v2"
)
type torrentInfo trans.Torrent
type torrentSelected map[int]trans.Torrent
type errMsg struct{ err error }
type callBackMsg bool
type status int
var Models []tea.Model
var torrentFields = []string{"activityDate", "addedDate", "bandwidthPriority", "comment", "corruptEver", "creator", "dateCreated", "desiredAvailable", "doneDate", "downloadDir", "downloadedEver", "downloadLimit", "downloadLimited", "error", "errorString", "eta", "etaIdle", "files", "fileStats", "hashString", "haveUnchecked", "haveValid", "honorsSessionLimits", "id", "isFinished", "isPrivate", "isStalled", "leftUntilDone", "magnetLink", "manualAnnounceTime", "maxConnectedPeers", "metadataPercentComplete", "name", "peer-limit", "peers", "peersConnected", "peersFrom", "peersGettingFromUs", "peersSendingToUs", "percentDone", "pieces", "pieceCount", "pieceSize", "priorities", "queuePosition", "rateDownload", "rateUpload", "recheckProgress", "secondsDownloading", "secondsSeeding", "seedIdleLimit", "seedIdleMode", "seedRatioLimit", "seedRatioMode", "sizeWhenDone", "startDate", "status", "trackers", "trackerStats", "totalSize", "torrentFile", "uploadedEver", "uploadLimit", "uploadLimited", "uploadRatio", "wanted", "webseeds", "webseedsSendingToUs"}
var baseStyle = lipgloss.NewStyle().BorderStyle(lipgloss.NormalBorder()).BorderForeground(lipgloss.Color("240"))
const (
MainModel status = iota
InfoView
TextInputView
)
func NewModel() *Model { return &Model{} }
func (m Model) Init() tea.Cmd { return nil }
func (e errMsg) Error() string { return e.err.Error() }
type TorrentTable struct {
selectedTorrents map[int]trans.Torrent
table table.Model
torrent trans.Torrent
height int
width int
}
type Model struct {
torrentTable TorrentTable
state status
err error
infoModel InfoModel
loaded bool
}
func (m *Model) Next() {
if m.state == MainModel {
m.state = InfoView
} else {
m.state++
}
}
func (m *Model) Prev() {
if m.state == InfoView {
m.state = MainModel
} else {
m.state--
}
}
func buildRow(torrent trans.Torrent, headers []string) table.Row {
var row table.Row
for _, key := range headers {
switch key {
case "ID":
row = append(row, strconv.Itoa(int(*torrent.ID)))
case "Name":
row = append(row, *torrent.Name)
case "Status":
row = append(row, parseStatus(torrent))
case "Size":
row = append(row, string(torrent.TotalSize.GBString()))
case "Ratio":
row = append(row, fmt.Sprintf("%.2f", *torrent.UploadRatio))
case "Location":
row = append(row, *torrent.DownloadDir)
case "Activity Date":
date := *torrent.ActivityDate
row = append(row, date.Format("2006-01-02"))
case "Download Rate":
row = append(row, strconv.Itoa(int(*torrent.RateDownload)))
case "Upload Rate":
row = append(row, strconv.Itoa(int(*torrent.RateUpload)))
case "Uploaded Ever":
row = append(row, strconv.Itoa(int(*torrent.UploadedEver)))
case "Error":
row = append(row, *torrent.ErrorString)
case "Percent Done":
row = append(row, fmt.Sprintf("%.2f%%", *torrent.PercentDone))
case "Lables":
var labels string
for _, label := range torrent.Labels {
labels += label + ","
}
row = append(row, labels)
case "Trackers":
var trackers string
for _, t := range torrent.TrackerStats {
tracker := strings.TrimPrefix(t.Host, "https://")
tracker = strings.TrimPrefix(tracker, "http://")
trackers += tracker + ","
}
row = append(row, trackers)
}
}
return row
}
func SetColumns(t TorrentTable) (columns []table.Column, headers []string) {
totalColumns := 1
for range Cfg.UI.Columns {
totalColumns++
}
offset := 0
for _, c := range Cfg.UI.Columns {
switch c {
case "ID":
offset += 4
case "Ratio":
offset += 5
case "Percent Done":
offset += 5
case "Upload Rate":
offset += 5
case "Size":
offset += 10
case "Activity Date":
offset += 11
case "Status":
offset += 20
case "Name":
offset -= 30
case "Location":
offset -= 10
}
}
maxColumnSize := (t.width + offset) / totalColumns
for _, c := range Cfg.UI.Columns {
var column table.Column
switch c {
case "ID":
column = table.Column{Title: c, Width: 4}
case "Ratio":
column = table.Column{Title: c, Width: 5}
case "Upload Rate":
column = table.Column{Title: c, Width: 5}
case "Percent Done":
column = table.Column{Title: c, Width: 5}
case "Size":
column = table.Column{Title: c, Width: 10}
case "Status":
column = table.Column{Title: c, Width: 20}
case "Name":
column = table.Column{Title: c, Width: maxColumnSize + 30}
case "Location":
column = table.Column{Title: c, Width: maxColumnSize + 10}
default:
column = table.Column{Title: c, Width: maxColumnSize}
}
columns = append(columns, column)
headers = append(headers, c)
}
return columns, headers
}
func (m *TorrentTable) updateTable() {
allTorrents := getAllTorrents(*TransmissionClient)
visibleColumns, headers := SetColumns(*m)
var rows []table.Row
for _, torrent := range allTorrents {
rows = append(rows, buildRow(torrent, headers))
}
myTable := table.New(table.WithColumns(visibleColumns), table.WithRows(rows), table.WithFocused(true), table.WithHeight(m.height), table.WithWidth(m.width))
style := table.DefaultStyles()
style.Header = style.Header.BorderStyle(lipgloss.NormalBorder()).BorderForeground(lipgloss.Color("240")).BorderBottom(true).Bold(false)
style.Selected = style.Selected.Foreground(lipgloss.Color("229")).Background(lipgloss.Color("57")).Bold(false)
myTable.SetStyles(style)
m.torrent = allTorrents[0]
m.table = myTable
}
// takes a torrentID and returns the torrent
func getTorrentInfo(m Model, offset int) tea.Cmd {
return func() tea.Msg {
torrentID, _ := strconv.Atoi(m.torrentTable.table.SelectedRow()[0])
max := len(m.torrentTable.table.Rows())
if torrentID+offset > 0 && torrentID+offset < max {
torrentID += offset
}
torrent, err := TransmissionClient.TorrentGet(context.TODO(), torrentFields, []int64{int64(torrentID)})
if err != nil {
return errMsg{err}
}
return torrentInfo(torrent[0])
}
}
// adds a torrent to the selcted list
func selectTorrent(m Model) tea.Cmd {
return func() tea.Msg {
offset := 1
cursor := m.torrentTable.table.Cursor()
_, exists := m.torrentTable.selectedTorrents[cursor]
if exists {
delete(m.torrentTable.selectedTorrents, cursor)
} else {
torrent, _ := TransmissionClient.TorrentGet(context.TODO(), torrentFields, []int64{int64(cursor + offset)})
m.torrentTable.selectedTorrents[m.torrentTable.table.Cursor()] = torrent[0]
}
return torrentSelected(m.torrentTable.selectedTorrents)
}
}
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
var cmds []tea.Cmd
switch msg := msg.(type) {
case tea.WindowSizeMsg:
if !m.loaded {
m.torrentTable.height = msg.Height - 10
m.torrentTable.width = msg.Width - 5
m.torrentTable.updateTable()
m.torrentTable.selectedTorrents = make(map[int]trans.Torrent)
m.loaded = true
return m, cmd
}
case errMsg:
m.err = msg
return m, tea.Quit
case torrentInfo:
m.torrentTable.torrent = trans.Torrent(torrentInfo(msg))
case torrentSelected:
m.torrentTable.selectedTorrents = msg
case callBackMsg:
m.torrentTable.updateTable()
case tea.KeyMsg:
switch msg.String() {
case "enter":
cmd = selectTorrent(m)
cmds = append(cmds, cmd)
return m, cmd
// Consider looking into binds: when you jump it won't update to the current torrent
case "j":
cmd = getTorrentInfo(m, 1)
cmds = append(cmds, cmd)
case "k":
cmd = getTorrentInfo(m, -1)
cmds = append(cmds, cmd)
case "left", "h":
m.Prev()
case "l":
Models[MainModel] = m
Models[InfoView] = createInfoModel(m.torrentTable.torrent)
return Models[InfoView].Update(nil)
case "M", "m":
cmd = getTorrentInfo(m, 0)
Models[MainModel] = m
Models[TextInputView] = NewTextInputModel(int(*m.torrentTable.torrent.ID), m.torrentTable.selectedTorrents)
return Models[TextInputView].Update(nil)
case "ctrl+c", "q":
return m, tea.Quit
}
}
m.torrentTable.table, cmd = m.torrentTable.table.Update(msg)
return m, tea.Batch(cmds...)
}
func (m Model) View() string {
if m.err != nil {
return fmt.Sprintf("\nWe had some trouble: %v\n\n", m.err)
}
cursor := strconv.Itoa(int(m.torrentTable.table.Cursor()))
torrentName := "N/A"
var tSplit string
var selectedTorrents string
if m.loaded {
torrentName = *m.torrentTable.torrent.Name
tSplit = m.torrentTable.table.SelectedRow()[0]
var selected []string
for _, element := range m.torrentTable.selectedTorrents {
selected = append(selected, *element.Name)
}
sort.Strings(selected)
selectedTorrents = fmt.Sprintln(strings.Join(selected, "\n"))
}
return baseStyle.Render(m.torrentTable.table.View()) + "\n" + "Cursor: " + cursor + "\n" + "Torrent: " + torrentName + "\nID: " + tSplit + "\n" + selectedTorrents
}
func getAllTorrents(transmissionClient trans.Client) []trans.Torrent {
torrents, err := transmissionClient.TorrentGetAll(context.TODO())
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
return torrents
}
func parseStatus(torrent trans.Torrent) string {
switch *torrent.Status {
case 0:
return "Stopped"
case 1:
return "Checking Files"
case 2:
return "Files Checked"
case 3:
return "Queued for Download"
case 4:
return "Downloading"
case 5:
return "Waiting for Seeds"
case 6:
return "Seeding"
case 7:
return "No Peers Found"
}
return "Status unknown???"
}