This repository has been archived by the owner on Apr 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
listui.go
168 lines (144 loc) · 3.9 KB
/
listui.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
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"text/tabwriter"
"github.com/cheynewallace/tabby"
"github.com/jroimartin/gocui"
)
func maxstring(str string, length int) string {
strlen := len(str)
if strlen > length {
return str[:length]
} else {
return str
}
}
func torparadise(g *gocui.Gui) error {
if strings.TrimSpace(query) == "" {
errorui = errors.New("No Query")
g.SetManagerFunc(errorfunc)
return nil
}
// construct API URL
finalquery := apistr + url.QueryEscape(query)
// Query the Torrent Paradise API to get result as response
response, err := http.Get(finalquery)
if err != nil {
errorui = err
g.SetManagerFunc(errorfunc)
return nil
}
// Read JSON Response
respbody, err := ioutil.ReadAll(response.Body)
if err != nil {
errorui = err
g.SetManagerFunc(errorfunc)
return nil
}
// Parse the json response into []Torrent
err = json.Unmarshal([]byte(respbody), &torrents)
if err != nil {
errorui = err
g.SetManagerFunc(errorfunc)
}
if len(torrents) == 0 {
errorui = errors.New("No Results")
g.SetManagerFunc(errorfunc)
return nil
}
query = ""
if len(datafilename) > 0 {
switch datatype {
case 4, 5:
g.SetManagerFunc(searchlisttor)
default:
g.SetManagerFunc(torrentdatawrite)
}
} else {
g.SetManagerFunc(searchlisttor)
}
return nil
}
func searchlisttor(g *gocui.Gui) error {
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
fmt.Println(err)
}
maxX, maxY := g.Size()
// Torrent List Box
if tllist, err := g.SetView("tl", -1, -1, maxX, maxY-2); err != nil {
if err != gocui.ErrUnknownView {
return err
}
tllist.Highlight = true
tllist.SelBgColor = gocui.ColorBlue
tllist.SelFgColor = gocui.ColorWhite
tllist.Wrap = true
tllist.MoveCursor(maxX-1, 1, true)
if err := g.SetKeybinding("tl", gocui.KeyArrowUp, gocui.ModNone, cursorUp); err != nil {
return err
}
if err := g.SetKeybinding("tl", gocui.KeyArrowDown, gocui.ModNone, cursorDown); err != nil {
return err
}
if err := g.SetKeybinding("tl", gocui.KeyArrowLeft, gocui.ModNone, backtoSearch); err != nil {
return err
}
if err := g.SetKeybinding("tl", gocui.KeyEnter, gocui.ModNone, selectit); err != nil {
return err
}
if err := g.SetKeybinding("tl", gocui.KeyArrowRight, gocui.ModNone, selectit); err != nil {
return err
}
if err := g.SetKeybinding("tl", 'q', gocui.ModNone, quit); err != nil {
return err
}
if err := g.SetKeybinding("tl", 'a', gocui.ModNone, sortAsc); err != nil {
return err
}
if err := g.SetKeybinding("tl", 'd', gocui.ModNone, sortDec); err != nil {
return err
}
var cv *gocui.View
if cv, err = g.SetCurrentView("tl"); err != nil {
return err
}
if len(torrents) != 0 {
maxlentl := int(float64(maxX) / 1.35)
tw := tabwriter.NewWriter(tllist, 0, 0, 1, ' ', 0)
t := tabby.NewCustom(tw)
t.AddLine(term_res+"#", term_res+"Name", term_res+"Size", term_res+"Seeds", term_res+"Leeches"+term_res)
for idno, eachtorrent := range torrents {
t.AddLine(term_yell+strconv.Itoa(idno+1),
term_cyan+maxstring(eachtorrent.Text, maxlentl),
term_purp+fmt.Sprintf("%f", 0.000000001*eachtorrent.Length)+" GB",
term_green+"S:"+strconv.Itoa(eachtorrent.Seeds),
term_red+"L:"+strconv.Itoa(eachtorrent.Leechs)+term_res)
}
t.Print()
}
for mc := 0; mc <= selid+1; mc++ {
if ccerr := cv.SetCursor(0, mc); ccerr != nil {
ox, oy := cv.Origin()
if soerr := cv.SetOrigin(ox, oy+1); soerr != nil {
return soerr
}
}
}
}
if tlhelp, err := g.SetView("tlhelp", -1, maxY-2, maxX, maxY); err != nil {
if err != gocui.ErrUnknownView {
return err
}
tlhelp.Frame = false
tlhelp.Wrap = true
fmt.Fprintf(tlhelp, "use ↑ ↓ to navigate, <enter> or → to select , ← to go back to search , (a) to Ascending-Sort , (d) to Descending-Sort and (q) or Ctrl-c to quit")
}
return nil
}