-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
102 lines (83 loc) · 1.94 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
package main
import (
"fmt"
"math/rand"
"time"
"github.com/alanxoc3/concards/internal"
"github.com/alanxoc3/concards/internal/deck"
"github.com/alanxoc3/concards/internal/file"
"github.com/alanxoc3/concards/internal/termboxgui"
"github.com/spf13/cobra"
)
var version string = "snapshot"
func main() {
rootCmd := &cobra.Command{
Use: "concards [flags] [file | folder]...",
Short: "Concards is a simple CLI based SRS flashcard app.",
}
c := genConfig(rootCmd.Flags())
rootCmd.PreRun = func(cmd *cobra.Command, args []string) {
cleanConfig(c, args)
}
rootCmd.Run = func(cmd *cobra.Command, args []string) {
if c.IsVersion {
fmt.Println("concards " + version)
return
}
main_logic(c)
}
rootCmd.Execute()
}
func main_logic(c *internal.Config) {
d := deck.NewDeck(time.Now())
predicts := file.ReadPredictsFromFile(c.PredictFile)
d.AddPredicts(predicts...)
if len(c.Files) == 0 {
internal.AssertError("You didn't provide any files to parse.")
}
for _, f := range c.Files {
if cm, err := file.ReadCardsFromFile(f); err != nil {
internal.AssertError(fmt.Sprintf("File \"%s\" does not exist!", f))
} else {
for _, c := range cm {
d.AddCards(c)
}
}
}
if !c.IsMemorize {
d.RemoveMemorize()
}
if !c.IsReview {
d.RemoveReview()
}
if !c.IsDone {
d.RemoveDone()
}
if c.Number > 0 {
d.Truncate(c.Number)
}
if c.IsFileList {
files := map[string]bool{}
for _, c := range d.CardList() {
file := c.File()
if _, exist := files[file]; !exist {
files[file] = true
fmt.Printf("%s\n", file)
}
}
return
} else if c.IsPrint {
cards := d.CardList()
for _, c := range cards {
fmt.Printf("#: %s\n", c)
}
if len(cards) > 0 {
fmt.Printf(":#\n")
}
return
}
rand.Seed(time.Now().UTC().UnixNano()) // Used for algorithms.
termboxgui.TermBoxRun(d, c)
_ = file.WritePredictsToFile(d.PredictList(), c.PredictFile)
_ = file.WriteOutcomesToFile(d.OutcomeList(), c.OutcomeFile)
}