-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.td
127 lines (112 loc) · 2.54 KB
/
main.td
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
import "bufio"
import "os"
import "strings"
import "fs"
import "json"
import "http"
import "crypto"
var raw_data = embed("db.json")
sysout "Loading Anime Database...\n".yellow
var obj = json.decode(raw_data)
sysout "Anime Database has loaded! ".blue, string(obj.data.length).iblue, " animes total!\n".blue
XX := "%." + string(obj.data.length).length + "x "
fn name_query(a, b) {
a = strings.to_upper(a)
b = strings.to_upper(b)
var out = ""
aa := strings.split(a, b)
for i, s in aa {
out += s.iwhite
if i < aa.length - 1 {
out += b.on_green
}
}
return out
}
fn parse_arg(str){
ret := []
arr := strings.split(str, " ")
for a in arr {
a = strings.trim_space(a)
if a != "" {
ret.push(a)
}
}
return ret
}
fn search_anime(input) {
arr := []
for index, anime in obj.data {
if strings.contains(strings.to_lower(anime.title), strings.to_lower(input)) {
arr.push(index)
}
}
limit := 20
sysout "\nTotal search result: ", arr.length, "\n"
for i := 0; i < (arr.length >= limit ? limit : arr.length); i++ {
index := arr[i]
sysout format(XX, index).igreen, name_query(obj.data[index].title, input), "\n"
}
if arr.length > limit {
sysout string(arr.length - limit).iyellow, " more anime found!\n".yellow
}
}
fn view_anime(id) {
w := 600
h := 400
k := strings.parse_int(id, 16, 0)
if is_error(k) {
return
}
anime := obj.data[k]
if anime == null {
sysout "Undefined Anime ID!\n".ired
return
}
if !fs.exists("cache/image") {
fs.mkdir_all("cache/image")
}
imgname := format("cache/image/%s.png", crypto.md5(anime.picture))
if !fs.exists(imgname) {
img := http.get(anime.picture)
fs.writefile(imgname, img)
}
cmd := os.exec("tender", "view.td", json.encode(anime), imgname)
cmd.stdout(os.stdout)
cmd.start()
}
fn print_help() {
println()
println("Commands: ")
sysout "search <name> -> To search anime\n"
sysout "view -> To view anime details in GUI\n"
sysout "help -> To print help\n"
sysout "exit -> To exit\n"
println()
}
sysout "Welcome to AnimeDB!\n".red
sysout "Last Updated, ".red, obj.lastUpdate.ired, "\n"
print_help()
for {
print("$ ")
var input = strings.trim_space(bufio.readline())
args := parse_arg(input)
if args.length > 0 {
cmd := args[0]
if cmd == "search" {
search_anime(strings.join(splice(args, 1), " "))
}
else if cmd == "view" {
view_anime(args[1])
}
else if cmd == "help" {
print_help()
}
else if cmd == "exit" {
os.exit(0)
}
else {
sysout format("Invalid command, \"%s\"!\n", cmd).red
}
}
}