Skip to content

Commit 152b149

Browse files
committed
Merge branch 'list-refactor'
Closes #2
2 parents 9f6d944 + 55d1635 commit 152b149

File tree

6 files changed

+71
-99
lines changed

6 files changed

+71
-99
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
EXE = podbit
22

33
UISRC = ui/ui.go ui/input.go colors/colors.go ui/library.go ui/player.go ui/queue.go ui/download.go ui/tray.go
4-
UICOMPS = ui/components/menu.go ui/components/table.go
4+
UICOMPS = ui/components/menu.go ui/components/table.go ui/components/list.go
55
SOUNDSRC = sound/sound.go sound/queue.go
66
DATASRC = data/data.go data/queue.go data/db.go data/cache.go data/download.go
77
SRC = main.go ver.go ${INPUTSRC} ${UISRC} ${DATASRC} ${UICOMPS} ${SOUNDSRC}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/ethanv2/podbit
22

3-
go 1.17
3+
go 1.18
44

55
require (
66
github.com/blang/mpv v0.0.0-20160810175505-d56d7352e068

ui/components/list.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package components
2+
3+
// A Selecter represents any object which has a visibly selected
4+
// element and which is capable of changing said element
5+
type Selecter interface {
6+
GetSelection() (int, interface{})
7+
ChangeSelection(index int)
8+
MoveSelection(offset int)
9+
}
10+
11+
// List represents a generic component which contains elements
12+
// of an arbitrary type. The list can be scrolled and has an
13+
// associated selected element
14+
type List[Item any] struct {
15+
W, H int
16+
Items []Item
17+
18+
scroll int
19+
sel int
20+
}
21+
22+
// GetSelection returns the currently selected menu element, or
23+
// nil if none is selected
24+
func (l *List[Item]) GetSelection() (int, Item) {
25+
if len(l.Items) < 1 {
26+
return 0, *new(Item)
27+
}
28+
29+
return l.sel, l.Items[l.sel]
30+
}
31+
32+
// ChangeSelection changes the selection to the index specified.
33+
// If index is out of range, no action is taken.
34+
func (l *List[Item]) ChangeSelection(index int) {
35+
if index >= len(l.Items) || index < 0 {
36+
return
37+
}
38+
39+
l.sel = index
40+
41+
scrollAt := l.H + l.scroll + 1
42+
underscrollAt := l.scroll - 1
43+
if l.sel >= scrollAt {
44+
l.scroll += (l.sel - scrollAt) + 1
45+
} else if l.sel <= underscrollAt {
46+
l.scroll = l.sel
47+
}
48+
}
49+
50+
// MoveSelection changes the selected item relative to the current
51+
// position. If the new selection would be out of range, no action
52+
// is taken.
53+
func (l *List[Item]) MoveSelection(offset int) {
54+
off := l.sel + offset
55+
l.ChangeSelection(off)
56+
}

ui/components/menu.go

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,14 @@ import (
1010
//
1111
// Menu handles focus, scrolling and the managing of elements
1212
// at each render.
13-
//
14-
// This component is not thread safe and should only be modified
15-
// directly one a single thread
1613
type Menu struct {
17-
W, H int
1814
X, Y int
19-
Items []string
2015
Win goncurses.Window
2116

2217
Selected bool
23-
24-
scroll int
25-
sel int
26-
2718
prevw, prevh int
19+
20+
List[string]
2821
}
2922

3023
// Render immediately renders the menu to the specified
@@ -73,40 +66,3 @@ func (m *Menu) Render() {
7366
m.Win.ColorOff(colors.BackgroundBlue)
7467
}
7568
}
76-
77-
// GetSelection returns the text of the currently
78-
// selected menu element. If there are no items selected,
79-
// GetSelection returns an empty string.
80-
func (m *Menu) GetSelection() string {
81-
if len(m.Items) < 1 {
82-
return ""
83-
}
84-
85-
return m.Items[m.sel]
86-
}
87-
88-
// ChangeSelection changes the selection to the index specified.
89-
// If index is out of range, no action is taken.
90-
func (m *Menu) ChangeSelection(index int) {
91-
if index >= len(m.Items) || index < 0 {
92-
return
93-
}
94-
95-
m.sel = index
96-
97-
scrollAt := m.H + m.scroll + 1
98-
underscrollAt := m.scroll - 1
99-
if m.sel >= scrollAt {
100-
m.scroll += (m.sel - scrollAt) + 1
101-
} else if m.sel <= underscrollAt {
102-
m.scroll -= (m.sel - underscrollAt) + 1
103-
}
104-
}
105-
106-
// MoveSelection changes the selected item relative to the current
107-
// position. If the new selection would be out of range, no action
108-
// is taken.
109-
func (m *Menu) MoveSelection(offset int) {
110-
off := m.sel + offset
111-
m.ChangeSelection(off)
112-
}

ui/components/table.go

Lines changed: 6 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,12 @@ type Column struct {
2525
// for displaying a slice of structs
2626
type Table struct {
2727
X, Y int
28-
W, H int
2928
Win *goncurses.Window
3029

3130
// See column struct for docs
3231
Columns []Column
33-
// Each sub-slice represents each column entry (eg, 0 = first column)
34-
Items [][]string
35-
36-
scroll int
37-
sel int
32+
// Each slice represents each column entry (eg, 0 = first column)
33+
List[[]string]
3834

3935
prevw, prevh int
4036
prevlen int
@@ -43,6 +39,9 @@ type Table struct {
4339
// Render immediately renders the table to the requested coords X and Y
4440
// with the space taken up limited to the space W*H
4541
func (t *Table) Render() {
42+
// Reduce height to be actual usable space (minus headings)
43+
t.H -= 2
44+
4645
items := t.Items[t.scroll:]
4746
if t.prevw != t.W || t.prevh != t.H {
4847
t.scroll = 0
@@ -77,7 +76,7 @@ func (t *Table) Render() {
7776
for j, entry := range items {
7877
if i >= len(entry) {
7978
panic("invalid table entry: missing fields")
80-
} else if j > t.H-2 {
79+
} else if j > t.H {
8180
break
8281
}
8382

@@ -110,43 +109,3 @@ func (t *Table) Render() {
110109
off += colw
111110
}
112111
}
113-
114-
// GetSelection returns the text of the currently
115-
// selected menu element. If there are no items selected,
116-
// GetSelection returns an empty slice.
117-
func (t *Table) GetSelection() (int, []string) {
118-
if len(t.Items) < 1 {
119-
return 0, []string{}
120-
}
121-
122-
return t.sel, t.Items[t.sel]
123-
}
124-
125-
// ChangeSelection changes the selection to the index specified.
126-
// If index is out of range, no action is taken.
127-
func (t *Table) ChangeSelection(index int) {
128-
if index >= len(t.Items) || index < 0 {
129-
return
130-
}
131-
132-
t.sel = index
133-
134-
// Scroll at the last possible visible element
135-
// Underscroll at the first possible visible element
136-
scrollAt := t.H + t.scroll - 1
137-
underscrollAt := t.scroll - 1
138-
139-
if t.sel >= scrollAt {
140-
t.scroll += (t.sel - scrollAt) + 1
141-
} else if t.sel <= underscrollAt {
142-
t.scroll -= (t.sel - underscrollAt) + 1
143-
}
144-
}
145-
146-
// MoveSelection changes the selected item relative to the current
147-
// position. If the new selection would be out of range, no action
148-
// is taken.
149-
func (t *Table) MoveSelection(offset int) {
150-
off := t.sel + offset
151-
t.ChangeSelection(off)
152-
}

ui/library.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ func (l *Library) renderEpisodes(x, y int) {
5757
l.men[1].Items = l.men[1].Items[:0]
5858

5959
data.Q.RevRange(func(i int, elem *data.QueueItem) bool {
60-
if data.DB.GetFriendlyName(elem.URL) == l.men[0].GetSelection() {
60+
_, sel := l.men[0].GetSelection()
61+
if data.DB.GetFriendlyName(elem.URL) == sel {
6162
var text string
6263
entry, ok := data.Downloads.Query(elem.Path)
6364
title := entry.Title
@@ -140,7 +141,7 @@ func (l *Library) StartDownload() {
140141

141142
targets := l.men[1].Items
142143
if l.menSel == 1 {
143-
target := l.men[1].GetSelection()
144+
_, target := l.men[1].GetSelection()
144145
item := data.Q.GetEpisodeByURL(target)
145146

146147
if item == nil {
@@ -186,7 +187,7 @@ func (l *Library) StartPlaying(immediate bool) {
186187
}
187188

188189
if l.menSel == 1 {
189-
entry := l.men[1].GetSelection()
190+
_, entry := l.men[1].GetSelection()
190191
if data.IsURL(entry) {
191192
if immediate {
192193
sound.PlayNow(data.Q.GetEpisodeByURL(entry))
@@ -207,7 +208,7 @@ func (l *Library) StartPlaying(immediate bool) {
207208
return
208209
}
209210

210-
entry := l.men[0].GetSelection()
211+
_, entry := l.men[0].GetSelection()
211212

212213
sound.EnqueueByPodcast(entry)
213214

0 commit comments

Comments
 (0)