-
Notifications
You must be signed in to change notification settings - Fork 0
/
flashcards.go
133 lines (111 loc) · 2.54 KB
/
flashcards.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"os"
"os/exec"
"time"
"github.com/pkg/term"
)
// Returns either an ascii code, or (if input is an arrow) a Javascript key code.
// code taken from: https://github.com/paulrademacher/climenu/blob/a1afbb4e378bf580e7d6bddd826e44e8f64347a1/getchar.go#L6
func getChar() (ascii int, keyCode int, err error) {
t, _ := term.Open("/dev/tty")
term.RawMode(t)
bytes := make([]byte, 3)
var numRead int
numRead, err = t.Read(bytes)
if err != nil {
return
}
if numRead == 3 && bytes[0] == 27 && bytes[1] == 91 {
// Three-character control sequence, beginning with "ESC-[".
// Since there are no ASCII codes for arrow keys, we use
// Javascript key codes.
if bytes[2] == 65 {
// Up
keyCode = 38
} else if bytes[2] == 66 {
// Down
keyCode = 40
} else if bytes[2] == 67 {
// Right
keyCode = 39
} else if bytes[2] == 68 {
// Left
keyCode = 37
}
} else if numRead == 1 {
ascii = int(bytes[0])
} else {
// Two characters read??
}
t.Restore()
t.Close()
return
}
type definition struct {
Term string `json:"term"`
Definition string `json:"definition"`
}
func getDefinitions() []definition {
raw, err := ioutil.ReadFile("./terms.json")
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
var d []definition
json.Unmarshal(raw, &d)
return d
}
func getUserInput(valid []int) int {
for {
ascii, _, err := getChar()
checkForError(err)
for _, v := range valid {
if v == ascii {
return ascii
}
}
if ascii == 'q' {
os.Exit(0)
}
}
}
func checkForError(err error) {
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
}
func clearScreen() {
cmd := exec.Command("clear") //Linux example, its tested
cmd.Stdout = os.Stdout
cmd.Run()
}
func main() {
clearScreen()
def := getDefinitions()
length := len(def)
r := rand.New(rand.NewSource(time.Now().UnixNano()))
fmt.Println("*** Welcome to Flashcards ***")
fmt.Println("Press ? for instructions, or b to begin...")
var ascii int
ascii = getUserInput([]int{'?', 'b'})
if ascii == '?' {
fmt.Println("1. When only the term is displayed, press spacebar to view definition")
fmt.Println("2. After definition is displayed, press spacebar to go again")
fmt.Println("3. At any time, press q to quit")
fmt.Println("\nPress b to begin...")
ascii = getUserInput([]int{'b'})
}
for {
elem := def[r.Intn(length)]
fmt.Printf("%s\t-\t", elem.Term)
ascii = getUserInput([]int{32})
fmt.Printf("%s\n", elem.Definition)
ascii = getUserInput([]int{32})
}
}