-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.go
168 lines (151 loc) · 4.9 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
163
164
165
166
167
168
package main
import (
"fmt"
"log"
"os"
"os/user"
"github.com/jroimartin/gocui"
"github.com/nanohard/gotime/models"
)
const (
// The following 3 boxes will allow for 21 viewable characters.
// They will not adjust horizontally, only vertically,
// so only the output box will readjust both horizontally and vertically.
//
// Projects box width.
pwidth = 22
// Tasks box width.
twidth = 44
// Entries box width.
ewidth = 62
// Input box height.
sheight = 3
// P is string for projects.
P = "projects"
// T is string for tasks.
T = "tasks"
// E is string for entries.
E = "entries"
// O is string for output.
O = "output"
)
func main() {
// Debug log
usr, _ := user.Current()
dir := usr.HomeDir
f, err := os.OpenFile(dir+"/.gotime.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
fmt.Println("file no exists")
}
defer f.Close()
log.SetOutput(f)
g, err := gocui.NewGui(gocui.OutputNormal)
if err != nil {
log.Println("Failed to create a GUI:", err)
return
}
defer g.Close()
// Highlight active view.
g.Highlight = true
g.SelFgColor = gocui.ColorBlue
g.BgColor = gocui.ColorBlack
g.FgColor = gocui.ColorWhite
// The GUI object wants to know how to manage the layout.
// Unlike termui, gocui does not use a grid layout.
// Instead, it relies on a custom layout handler function to manage the layout.
//
// Here we set the layout manager to a function named layout that is defined further down.
g.SetManagerFunc(layout)
// Bind the quit handler function (also defined further down) to Ctrl-C,
// so that we can leave the application at any time.
err = g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit)
if err != nil {
log.Println("Could not set key binding:", err)
return
}
// View definitions *******************************************************************
// The terminal’s width and height are needed for layout calculations.
terminalWidth, terminalHeight := g.Size()
// Projects view.
projectView, err := g.SetView(P, 0, 0, pwidth, terminalHeight-4)
// ErrUnknownView is not a real error condition.
// It just says that the view did not exist before and needs initialization.
if err != nil && err != gocui.ErrUnknownView {
log.Println("Failed to create projects view:", err)
return
}
projectView.Title = "Projects"
projectView.FgColor = gocui.ColorCyan
projectView.Highlight = true
projectView.SelBgColor = gocui.ColorBlue
projectView.SelFgColor = gocui.ColorBlack
// Tasks view.
tasksView, err := g.SetView(T, pwidth+1, 0, twidth, terminalHeight-4)
// ErrUnknownView is not a real error condition.
// It just says that the view did not exist before and needs initialization.
if err != nil && err != gocui.ErrUnknownView {
log.Println("Failed to create tasks view:", err)
return
}
tasksView.Title = "Tasks"
tasksView.FgColor = gocui.ColorCyan
tasksView.SelBgColor = gocui.ColorBlue
tasksView.SelFgColor = gocui.ColorBlack
// // Entries view.
entriesView, err := g.SetView(E, twidth+1, 0, ewidth, terminalHeight-4)
// ErrUnknownView is not a real error condition.
// It just says that the view did not exist before and needs initialization.
if err != nil && err != gocui.ErrUnknownView {
log.Println("Failed to create main view:", err)
return
}
entriesView.Title = "Entries"
entriesView.FgColor = gocui.ColorCyan
entriesView.SelBgColor = gocui.ColorBlue
entriesView.SelFgColor = gocui.ColorBlack
// Output view.
outputView, err := g.SetView("output", ewidth+1, 0, terminalWidth-1, terminalHeight-4)
if err != nil && err != gocui.ErrUnknownView {
log.Println("Failed to create output view (AAAGGHHH!!!):", err)
return
}
outputView.FgColor = gocui.ColorWhite
// Let the view scroll if the output exceeds the visible area.
outputView.Autoscroll = true
outputView.Wrap = true
// Status view.
// Not used right now.
// statusView, err := g.SetView("status", 0, terminalHeight-sheight, terminalWidth-1, terminalHeight-1)
// if err != nil && err != gocui.ErrUnknownView {
// log.Println("Failed to create input view:", err)
// return
// }
// statusView.Title = "Status"
// statusView.FgColor = gocui.ColorYellow
// Database ***************************************************
models.InitDB()
defer models.DB.Close()
// Projects
projectItems := models.AllProjects()
if len(projectItems) > 0 {
models.CurrentProject = projectItems[0]
redrawProjects(g, projectView)
}
// Main loop stuff *********************************************
// Apply keybindings to program.
if err = keybindings(g); err != nil {
log.Panicln(err)
}
// Must set initial view here, right before program start!!!
v, _ := g.SetCurrentView(P)
// Move the cursor to update the output view with the description.
// (workaround)
cursorUp(g, v)
// If no projects on start then prompt the user to add a project.
if len(projectItems) == 0 {
inputView(g, projectView)
}
// Start the main loop.
err = g.MainLoop()
log.Println("Main loop has finished:", err)
}