-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
106 lines (86 loc) · 1.59 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
package main
import (
"flag"
"fmt"
"log"
"os/user"
"path"
"github.com/berto/flashcards/cmd"
"github.com/berto/flashcards/context"
"github.com/berto/flashcards/handlers"
"github.com/gizak/termui"
)
const (
VERSION = "v0.0.1"
USAGE = `Name:
flashcards - flashcards for your terminal
Usage:
flashcards [-options] [word]
Version:
%s
Global Options:
--help, -h
--config, -c
`
)
var (
configPath string
word *string
)
func init() {
// Get home dir for config file default
usr, err := user.Current()
if err != nil {
log.Fatal(err)
}
// Parse flags
flag.StringVar(
&configPath,
"config",
path.Join(usr.HomeDir, ".flashcards.json"),
"location of config file",
)
flag.StringVar(
&configPath,
"c",
path.Join(usr.HomeDir, ".flashcards.json"),
"shorthand for location of config file",
)
flag.Usage = func() {
fmt.Printf(USAGE, VERSION)
}
flag.Parse()
if len(flag.Args()) > 0 {
word = &flag.Args()[0]
}
}
func main() {
if word != nil {
commands.Define(&configPath, word)
}
// start terminal user interface
err := termui.Init()
if err != nil {
log.Fatal(err)
}
defer termui.Close()
// create context
ctx := context.CreateAppContext(&configPath)
// setup body
termui.Body.AddRows(
termui.NewRow(
termui.NewCol(2, 0, ctx.View.WordList),
termui.NewCol(10, 0, ctx.View.Flashcard),
),
termui.NewRow(
termui.NewCol(12, 0, ctx.View.Options),
),
)
termui.Body.Align()
termui.Render(termui.Body)
// set body in context
ctx.Body = termui.Body
// register handlers
handlers.RegisterEventHandlers(ctx)
termui.Loop()
}