-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdownload.go
144 lines (122 loc) · 2.57 KB
/
download.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
package ui
import (
"fmt"
"strconv"
"github.com/ejv2/podbit/colors"
"github.com/ejv2/podbit/data"
ev "github.com/ejv2/podbit/event"
"github.com/ejv2/podbit/sound"
"github.com/ejv2/podbit/ui/components"
)
var downloadHeadings []components.Column = []components.Column{
{
Label: "ID",
Width: 0.1,
Color: colors.BackgroundGreen,
},
{
Label: "%",
Width: 0.1,
Color: colors.BackgroundYellow,
},
{
Label: "Episode",
Width: 0.4,
Color: colors.BackgroundBlue,
},
{
Label: "Status",
Width: 0.4,
Color: colors.BackgroundRed,
},
}
type Downloads struct {
tbl components.Table
}
func (q *Downloads) Name() string {
return "Downloads"
}
func (q *Downloads) Render(x, y int) {
q.tbl.X, q.tbl.Y = x, y
q.tbl.W, q.tbl.H = w, h-5
q.tbl.Win = root
q.tbl.Columns = downloadHeadings
q.tbl.Items = nil
for i, elem := range data.Downloads.Downloads() {
item := make([]string, len(downloadHeadings))
item[0] = strconv.FormatInt(int64(i), 10)
item[1] = strconv.FormatFloat(elem.Percentage*100, 'f', 2, 64)
ep, ok := data.Downloads.Query(elem.Path)
if ok {
item[2] = ep.Title
} else {
item[2] = elem.Path
}
if elem.Completed {
if elem.Success {
item[3] = "Finished"
} else {
item[3] = fmt.Sprintf("Failed (%s)", elem.Error)
}
} else {
if elem.Elem.Youtube && elem.Percentage == 1 {
item[3] = "Encoding"
} else {
item[3] = "In progress"
}
}
q.tbl.Items = append(q.tbl.Items, item)
}
q.tbl.Render()
}
func (q *Downloads) Should(event int) bool {
return event == ev.Keystroke || event == ev.DownloadChanged || event == ev.PlayerChanged
}
func (q *Downloads) Input(c rune) {
switch c {
case 'j':
q.tbl.MoveSelection(1)
case 'k':
q.tbl.MoveSelection(-1)
case 'g':
q.tbl.ChangeSelection(0)
case 'G':
q.tbl.ChangeSelection(len(q.tbl.Items) - 1)
case 'd':
q.Cancel()
case 13:
q.Enqueue()
}
}
func (q *Downloads) Enqueue() {
i, _ := q.tbl.GetSelection()
d := data.Downloads.Downloads()[i].Path
var found *data.QueueItem
data.Q.Range(func(i int, item *data.QueueItem) bool {
if item.Path == d {
found = item
return false
}
return true
})
if found != nil {
go StatusMessage("Enqueued: Download will play once completed")
sound.Enqueue(found)
}
q.tbl.MoveSelection(1)
}
func (q *Downloads) Cancel() {
i, _ := q.tbl.GetSelection()
if i >= len(data.Downloads.Downloads()) {
return
}
dl := data.Downloads.Downloads()[i]
if !dl.Completed {
go func() {
dl.Stop <- 1
go StatusMessage("Download cancelled")
}()
} else {
go StatusMessage("Cannot cancel completed download")
}
}