-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
162 lines (136 loc) · 3.14 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
package main
import (
"errors"
"fmt"
"os"
"strconv"
"strings"
tea "github.com/charmbracelet/bubbletea"
)
var (
currUser *User
appDir string
csvName string
cli bool
criticalError error
sep = ','
prompt = false
helpText = strings.TrimSpace(`
https://github.com/abeleinin/goki
Usage:
goki - tui mode
goki list - view deck index
goki review <deck index> - review deck from cli
Import:
opt: - optional flags
-n "deck name" - assigned deck name to imported cards
-t - assigns tab sep (default sep=',')
goki opt < deck.txt - import deck in using stdin
Generate:
goki --gpt "my prompt" - generate a deck from a text prompt
goki --gpt < my_notes.txt - generate a deck from text or markdown files`)
)
func main() {
runCLI(os.Args[1:])
}
func runCLI(args []string) {
currUser = NewUser()
loadDecks()
initTable()
initInput()
updateTableColumns()
err := parseArgs(args)
if err != nil {
return
}
var response string
if prompt {
response = createDeckStdin()
} else {
response = readDeckStdin(sep)
}
if response != "" {
fmt.Println(response)
return
}
p := tea.NewProgram(currUser, tea.WithAltScreen())
if _, err := p.Run(); err != nil || criticalError != nil {
if criticalError != nil {
err = criticalError
}
fmt.Println("Error running goki:", err)
os.Exit(1)
}
}
func parseArgs(args []string) error {
for i := 0; i < len(args); i++ {
switch args[i] {
case "list":
PrintDecks()
// TODO: Not an error. Temp fix.
return errors.New("")
case "-h", "--help", "help":
fmt.Println(gokiLogo)
fmt.Println(helpText)
return errors.New("")
case "review":
if i <= len(args)-2 {
ReviewCLI(args[i+1])
i++
} else {
fmt.Println("Not enough args to run 'goki review <deck index>.'")
fmt.Println("Use 'goki list' to view deck index.")
return errors.New("Input Error")
}
case "-n":
if i <= len(args)-2 {
csvName = args[i+1]
i++
} else {
fmt.Println("Please provide a deck name.")
fmt.Println("Use 'goki help' for more info.")
return errors.New("Input Error")
}
case "-t":
sep = '\t'
case "--gpt":
prompt = true
if i <= len(args)-2 {
response := generateDeck(args[i+1])
i++
return errors.New(response)
}
default:
fmt.Print(args[i], " is not a valid command. Use 'goki -h' for more information.")
return errors.New("Input Error")
}
}
if sep == 0 {
for _, arg := range args[1:] {
if arg == "-t" {
sep = '\t'
break
}
}
}
return nil
}
func ReviewCLI(s string) {
i, _ := strconv.Atoi(s)
if i < 0 || i >= len(currUser.decks) {
fmt.Println("Invalid deck index when running 'goki review <deck index>'.")
fmt.Println("Use 'goki list' to view deck index.")
return
}
cli = true
currUser.decks[i].StartReview()
if len(currUser.decks[i].reviewData.reviewCards) > 0 {
p := tea.NewProgram(currUser.decks[i])
if _, err := p.Run(); err != nil {
fmt.Println(err)
os.Exit(1)
}
} else {
fmt.Println("No cards to review in the deck: " + currUser.decks[i].Name + ".")
}
}