-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
299 lines (246 loc) · 5.97 KB
/
main.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package main
import (
"bufio"
"encoding/json"
"fmt"
"os"
"os/exec"
"os/user"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/officialkris/notr/config"
)
var DevMode = false
type InternalData struct {
Version string
Week int
Classes []Class
LastDate time.Time
ClassDirectory string
}
type Class struct {
Code string
Description string
}
func main() {
clearOutput()
internalData := readInternalData()
checkData(&internalData)
for {
internalData.Version = config.PROGRAM_VERSION
internalData.LastDate = time.Now()
year, month, day := internalData.LastDate.Date()
writeInternalData(internalData)
if config.DEV_MODE {
fmt.Println("DEV_MODE ENABLED: USE WITH CARE")
}
fmt.Printf("NOTR Version: %v\n\n", internalData.Version)
fmt.Printf("Current Date: %v %v, %v\n", month, day, year)
fmt.Printf("Current Week: %v\n", internalData.Week)
fmt.Printf("Current Config Directory: ~%v\n", config.CONFIG_LOCATION+config.CONFIG_FILE_NAME)
fmt.Printf("Current Directory: %v\n", internalData.ClassDirectory)
fmt.Println()
fmt.Println("a: Start a new NOTR")
fmt.Println("b: Modify classes")
fmt.Println("c: Change week number")
fmt.Println("d: Reload date")
fmt.Println("e: Set class directory")
fmt.Println()
fmt.Print("Enter menu option (a to e, or q to quit): ")
var first string
fmt.Scan(&first)
first = strings.ToLower(first)
if first[0] == 'q' {
return
}
switch first {
case "a":
createNOTR(internalData)
case "b":
//modifyClasses
notImplemented()
case "c":
setWeek(&internalData)
case "d":
//reloadDate
notImplemented()
case "e":
setClassDirectory(&internalData)
}
}
}
func setWeek(internalData *InternalData) {
for {
fmt.Println()
fmt.Print("Enter new week number (or q to quit): ")
var first string
fmt.Scan(&first)
if first[0] == 'q' {
return
}
num, err := strconv.Atoi(first)
if err != nil {
fmt.Println("Please enter a valid number.")
} else {
internalData.Week = num
fmt.Println()
break
}
}
}
func setClassDirectory(internalData *InternalData) {
for {
fmt.Println()
fmt.Print("Enter fully resolved path name for new directory (or q for quit): ")
reader := bufio.NewReader(os.Stdin)
line, err := reader.ReadString('\n')
if err != nil {
fmt.Println(err.Error())
}
line = strings.TrimRight(line, "\n")
if line[0] == 'q' {
return
}
if _, err := os.Stat(line); err != nil {
if os.IsNotExist(err) {
fmt.Println("Folder does not exist.")
fmt.Println(line)
} else {
panic(err)
}
} else {
line, _ = filepath.Abs(line)
fmt.Println("Folder DOES exist!")
internalData.ClassDirectory = line
break
}
}
}
func createNOTR(internalData InternalData) {
fmt.Println()
for i, s := range internalData.Classes {
fmt.Printf("%v: %v - %v\n", i+1, s.Code, s.Description)
}
fmt.Println("q: Return to base menu")
fmt.Println()
fmt.Print("Enter class (from range above): ")
var first string
fmt.Scan(&first)
if first[0] == 'q' {
return
}
parsedInput, err := strconv.Atoi(first)
if err != nil {
panic(err)
}
class := internalData.Classes[parsedInput-1]
year, month, day := internalData.LastDate.Date()
name := generateNoteName()
stub := generateStub(class.Code, internalData.Week, internalData.LastDate)
filepath := internalData.ClassDirectory + "/" + name
fmt.Println()
fmt.Printf("Class: %v\n", class)
fmt.Printf("Current Date: %v-%v-%v\n", year, month, day)
fmt.Printf("Current Week %v\n", internalData.Week)
fmt.Printf("Started TextEdit with file name: %v\n", name)
createFile(filepath)
populateFile(filepath, stub)
openFile(filepath)
fmt.Println()
}
func generateNoteName() string {
return "NOTES" + ".txt" // TODO: Add number
}
func check(e error) {
if e != nil {
panic(e)
}
}
func generateStub(class string, week int, time time.Time) (buf string) {
day := fmt.Sprintf("%s", time.Weekday())
day = day[0:3]
buf = fmt.Sprintf("%s\nWEEK %d\n%s %d/%d/%d\n\n", class, week, day, time.Month(), time.Day(), time.Year())
return
}
func populateFile(name string, stub string) {
fmt.Println(name)
f, err := os.OpenFile(name, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
check(err)
defer f.Close()
n2, err := f.Write([]byte(stub))
check(err)
fmt.Printf("Wrote bytes: %v", n2)
}
func createFile(name string) {
app := "touch"
cmd := exec.Command(app, name)
stdout, err := cmd.Output()
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(string(stdout))
}
func openFile(name string) {
app := "open"
arg1 := "-a"
arg2 := "TextEdit"
arg3 := name
cmd := exec.Command(app, arg1, arg2, arg3)
stdout, err := cmd.Output()
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(string(stdout))
}
func writeInternalData(data InternalData) {
jsonData, err := json.MarshalIndent(data, "", "\t")
if err != nil {
fmt.Printf("Could not marshal data: %s\n", err)
return
}
usr, _ := user.Current()
path := usr.HomeDir + config.CONFIG_LOCATION
if _, err := os.Stat(path); os.IsNotExist(err) {
os.MkdirAll(path, 0700)
}
err1 := os.WriteFile(path+config.CONFIG_FILE_NAME, []byte(jsonData), 0644)
check(err1)
}
func readInternalData() (returnData InternalData) {
usr, _ := user.Current()
path := usr.HomeDir + config.CONFIG_LOCATION
if _, err := os.Stat(path); os.IsNotExist(err) {
os.MkdirAll(path, 0700)
}
data, err := os.ReadFile(path + config.CONFIG_FILE_NAME)
if err != nil {
fmt.Println(err.Error())
return
}
json.Unmarshal(data, &returnData)
return
}
func clearOutput() {
cmd := exec.Command("clear")
stdout, err := cmd.Output()
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(string(stdout))
}
func notImplemented() {
println()
println("Function is not implemented yet")
println()
}
func checkData(internalData *InternalData) {
if internalData.ClassDirectory == "" {
println("Unfortunately, the config file under ~/.config/notr does not have a value for the class directory.")
setClassDirectory(internalData)
}
}