-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
81 lines (68 loc) · 1.68 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
package main
import (
"fmt"
"os"
"github.com/rodpadev/gopuby/commander"
"github.com/rodpadev/gopuby/db"
"github.com/rodpadev/gopuby/epubManager"
"github.com/rodpadev/gopuby/renderer"
"github.com/rodpadev/gopuby/stateMachine"
"github.com/nsf/termbox-go"
// "github.com/nsf/termbox-go"
)
type Gopuby struct {
Book *epubManager.Book
Commander *commander.Commander
Renderer *renderer.Renderer
StateMachine *stateMachine.StateMachine
}
func New() *Gopuby {
initialPage := 1
currentChapterIndex := 0
b := &epubManager.Book{
CurrentTextPage: &initialPage,
CurrentChapterIndex: ¤tChapterIndex,
}
sm := stateMachine.New()
r := renderer.New(&initialPage)
c := commander.New(r, sm, b)
return &Gopuby{
Commander: c,
Renderer: r,
StateMachine: sm,
Book: b,
}
}
func run() error {
db.New("gopuby.db")
g := New()
defer termbox.Close()
termbox.SetInputMode(termbox.InputEsc)
termbox.SetOutputMode(termbox.OutputNormal)
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
termbox.Flush()
args := os.Args[1:]
if len(args) > 1 {
return fmt.Errorf("\nUsage: gopuby [path]")
} else if len(args) == 0 {
// transition state to commander
g.StateMachine.Transition(stateMachine.EnterCommandMode)
g.Commander.DrawCommandBar()
g.Commander.DrawHelpScreen()
// the user then enters command to either list books or open a book from a path
} else if len(args) == 1 {
path := args[0]
if err := g.Book.LoadBook(path); err != nil {
return err
}
g.Renderer.Render(&g.Book.CurrentText)
}
g.Commander.Run()
return nil
}
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}