-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
158 lines (133 loc) · 3.64 KB
/
utils.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
package gol
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"os"
"strings"
)
// What list are currently valid names to use in this program
func isValidListName(listName string) bool {
for name := range RegisteredTypes {
if name == listName {
return true
}
}
return false
}
func PrintAnime(object AnimeListElement) {
fmt.Printf("\"%s\" [%.2f] (%s)\n", object.Base.Name, object.Base.HeuristicRating, object.Base.URL)
fmt.Printf("Episodes: %d | Rating: %.2f \n", object.NumEpisodes, object.Base.SourceRating)
maxChars := 80
length := len(object.Base.Description)
for i := 0; i < length; i += maxChars {
var extent int = i + maxChars
if extent > length { //Slicing beyond length causes an exception. Careful with this
extent = length
}
fmt.Printf("\t%s\n", object.Base.Description[i:extent])
}
fmt.Println("")
}
func getListFilename(name string) string {
return strings.ToUpper(name[0:1]) + name[1:] + ".txt"
}
//Reads the file contents and extracts useful information, line by line
func readFile(filename string) []string {
//Read all data
data, err := ioutil.ReadFile(filename)
check(err)
plaintext := string(data)
lines := strings.Split(plaintext, "\r\n")
// Filter out comments
validLines := make([]string, 0)
for _, line := range lines {
if !strings.Contains(line, "#") {
validLines = append(validLines, strings.TrimSpace(line))
}
}
return validLines
}
//Makes sure the file is only left with comments
func rewriteFile(filename string) {
//Read all data
data, err := ioutil.ReadFile(filename)
check(err)
plaintext := string(data)
lines := strings.Split(plaintext, "\r\n")
// Filter out comments
commentedLines := make([]string, 0)
for _, line := range lines {
if strings.Contains(line, "#") {
commentedLines = append(commentedLines, strings.TrimSpace(line))
}
}
//Now, writeback
_ = os.Remove(filename)
newFile, _ := os.Create(filename)
defer newFile.Close()
for _, line := range commentedLines {
newFile.WriteString(line)
//Unfortunately, Windows line endings (just in case)
newFile.WriteString("\r\n")
}
}
func ExtractDomainName(URL string) string {
startIdx := strings.Index(URL, "//") + 2
endIdx := strings.Index(URL[startIdx:], "/") + startIdx
//Check if this was invalid
if startIdx < 2 || endIdx < 2 {
return ""
}
return URL[startIdx:endIdx]
}
func PrintSetWidth(text, linePrefix, newlineSeq string, columnWidth int) {
if columnWidth < 1 {
columnWidth = 65535
}
length := len(text)
charsInLine, nextWordIndex := 0, 0
for idx := 0; idx < length; {
nextWordIndex = strings.IndexRune(text[charsInLine+idx:], ' ')
if nextWordIndex < 0 {
//TODO: implement slicing here
fmt.Print(linePrefix, text[idx:], newlineSeq)
return
}
if charsInLine+nextWordIndex > columnWidth {
fmt.Print(linePrefix, text[idx:idx+charsInLine], newlineSeq)
idx += charsInLine
charsInLine = 0
} else {
charsInLine += nextWordIndex + 1
}
}
}
func RequestInput(message string) string {
fmt.Printf("\n%s", message)
reader := bufio.NewReader(os.Stdin)
input, _ := reader.ReadString('\n')
return input
}
func PrintKnownLists() {
fmt.Print("\tLists: [ ")
for namedList := range RegisteredTypes {
fmt.Print(namedList, " ")
}
fmt.Println("]")
}
func PrintKnownActions() {
var buffer bytes.Buffer
fmt.Print("\tActions: [ ")
for namedList := range Actions {
buffer.WriteString(namedList)
buffer.WriteString(" ")
}
buffer.WriteString("]")
PrintSetWidth(buffer.String(), "", "\n\t", 80)
fmt.Println("")
}
func Cleanup() {
closeDatabase()
}