-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.go
294 lines (256 loc) · 7.75 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
Terminews is a terminal based (TUI) RSS feed manager.
Copyright (C) 2017 Alexandros Ntavelos, a[dot]ntavelos[at]gmail[dot]com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"fmt"
"log"
"os"
"os/user"
"path"
"github.com/antavelos/terminews/db"
"github.com/fatih/color"
c "github.com/jroimartin/gocui"
)
const (
SITES_VIEW = "rssreaders"
NEWS_VIEW = "news"
SUMMARY_VIEW = "summary"
PROMPT_VIEW = "prompt"
CONTENT_VIEW = "content"
HELP_VIEW = "help"
appVersion = "1.2.1"
)
var (
tdb *db.TDB
SitesList *List
NewsList *List
ContentList *List
Summary *c.View
CurrentContent []string
CurrentBookmarks []db.Event
curW int
curH int
Bold *color.Color
)
// relSize calculates the sizes of the sites view width
// and the news view height in relation to the current terminal size
func relSize(g *c.Gui) (int, int) {
tw, th := g.Size()
return (tw * 3) / 10, (th * 70) / 100
}
// The layout handler calculates all sizes depending
// on the current terminal size.
func layout(g *c.Gui) error {
// Get the current terminal size.
tw, th := g.Size()
// Get the relative size of the views
rw, rh := relSize(g)
_, err := g.SetView(SITES_VIEW, 0, 0, rw, th-1)
if err != nil {
log.Fatal("Cannot update sites view", err)
}
_, err = g.SetView(NEWS_VIEW, rw+1, 0, tw-1, rh)
if err != nil {
log.Fatal("Cannot update news view", err)
}
_, err = g.SetView(SUMMARY_VIEW, rw+1, rh+1, tw-1, th-1)
if err != nil {
log.Fatal("Cannot update Summary view.", err)
}
UpdateSummary()
if _, err = g.View(PROMPT_VIEW); err == nil {
_, err = g.SetView(PROMPT_VIEW, tw/6, (th/2)-1, (tw*5)/6, (th/2)+1)
if err != nil && err != c.ErrUnknownView {
return err
}
}
if _, err = g.View(CONTENT_VIEW); err == nil {
_, err = g.SetView(CONTENT_VIEW, tw/8, th/8, (tw*7)/8, (th*7)/8)
if err != nil && err != c.ErrUnknownView {
return err
}
}
if _, err = g.View(HELP_VIEW); err == nil {
_, err = g.SetView(HELP_VIEW, tw/6, th/5, (tw*5)/6, (th*4)/5)
if err != nil && err != c.ErrUnknownView {
return err
}
}
if curW != tw || curH != th {
SitesList.ResetPages()
SitesList.Draw()
NewsList.ResetPages()
NewsList.Draw()
if ContentList != nil {
ContentList.Reset()
UpdateContent(g, CurrentContent)
}
curW = tw
curH = th
}
return nil
}
// getappDir creates if not exists the app directory where the sqlite db file
// as well as the log file will be stored. In case of failure the current dir
// will be used.
func getAppDir() (string, error) {
usr, _ := user.Current()
dir := path.Join(usr.HomeDir, ".terminews")
if _, err := os.Stat(dir); err != nil {
if os.IsNotExist(err) {
if oserr := os.Mkdir(dir, 0700); oserr != nil {
return "", oserr
}
} else {
return "", err
}
}
return dir, nil
}
func main() {
// If app is called with --version then display version flag & exit
if len(os.Args) == 2 {
if os.Args[1] == "--version" {
fmt.Println(appVersion)
return
}
}
var v *c.View
var err error
Bold = color.New(color.Bold)
appDir, err := getAppDir()
if err != nil {
panic("Could not set up app directory.")
}
// Setup logging
logfile := path.Join(appDir, "terminews.log")
f, err := os.OpenFile(logfile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0700)
if err != nil {
log.Fatal("Failed to open logfile", err)
}
defer f.Close()
log.SetOutput(f)
// Init DB
if tdb, err = db.InitDB(appDir); err != nil {
log.Fatal("Failed to initialize DB", err)
}
defer tdb.Close()
// Create a new GUI.
g, err := c.NewGui(c.OutputNormal)
if err != nil {
log.Fatal("Failed to initialize GUI", err)
}
defer g.Close()
// some basic configuration
g.SelFgColor = c.ColorGreen | c.AttrBold
g.BgColor = c.ColorDefault
g.Highlight = true
// setup the layout
g.SetManagerFunc(layout)
// the current actual size of the terminal
curW, curH = g.Size()
// rw is the relative width of the sites view
// rh is the relative height of the news view
rw, rh := relSize(g)
// Setup the initial layout
// Sites List
v, err = g.SetView(SITES_VIEW, 0, 0, rw, curH-1)
if err != nil && err != c.ErrUnknownView {
log.Fatal("Failed to create sites list:", err)
}
SitesList = CreateList(v, true)
SitesList.Focus(g)
// it loads the existing sites if any at the beginning
g.Update(func(g *c.Gui) error {
if err := LoadSites(); err != nil {
log.Fatal("Error while loading sites", err)
}
log.Print("Loaded initial sites")
return nil
})
// News list
v, err = g.SetView(NEWS_VIEW, rw+1, 0, curW-1, rh)
if err != nil && err != c.ErrUnknownView {
log.Fatal(" Failed to create news list:", err)
}
NewsList = CreateList(v, true)
NewsList.SetTitle("No news yet...")
// Summary view
Summary, err = g.SetView(SUMMARY_VIEW, rw+1, rh+1, curW-1, curH-1)
if err != nil && err != c.ErrUnknownView {
log.Fatal("Failed to create Summary view:", err)
}
Summary.Title = " Summary "
Summary.Wrap = true
// preload thebookmarks
CurrentBookmarks, err = tdb.GetEvents()
if err != nil {
log.Println("Error on load bookmarks", err)
}
// setup the keybindings of the app
if err = g.SetKeybinding("", c.KeyCtrlN, c.ModNone, AddSite); err != nil {
log.Fatal("Failed to set keybindings")
}
if err = g.SetKeybinding("", c.KeyDelete, c.ModNone, DeleteEntry); err != nil {
log.Fatal("Failed to set keybindings")
}
if err = g.SetKeybinding(NEWS_VIEW, c.KeyCtrlB, c.ModNone, AddBookmark); err != nil {
log.Fatal("Failed to set keybindings")
}
if err = g.SetKeybinding("", c.KeyCtrlC, c.ModNone, Quit); err != nil {
log.Fatal("Failed to set keybindings")
}
if err = g.SetKeybinding("", c.KeyCtrlB, c.ModAlt, LoadBookmarks); err != nil {
log.Fatal("Failed to set keybindings")
}
if err = g.SetKeybinding("", c.KeyTab, c.ModNone, SwitchView); err != nil {
log.Fatal("Failed to set keybindings")
}
if err = g.SetKeybinding("", c.KeyArrowUp, c.ModNone, ListUp); err != nil {
log.Fatal("Failed to set keybindings")
}
if err = g.SetKeybinding("", c.KeyArrowDown, c.ModNone, ListDown); err != nil {
log.Fatal("Failed to set keybindings")
}
if err = g.SetKeybinding("", c.KeyPgup, c.ModNone, ListPgUp); err != nil {
log.Fatal("Failed to set keybindings")
}
if err = g.SetKeybinding("", c.KeyPgdn, c.ModNone, ListPgDown); err != nil {
log.Fatal("Failed to set keybindings")
}
if err = g.SetKeybinding("", c.KeyEnter, c.ModNone, OnEnter); err != nil {
log.Fatal("Failed to set keybindings")
}
if err = g.SetKeybinding("", c.KeyCtrlQ, c.ModNone, RemoveTopView); err != nil {
log.Fatal("Failed to set keybindings")
}
if err = g.SetKeybinding("", c.KeyCtrlF, c.ModNone, Find); err != nil {
log.Fatal("Failed to set keybindings")
}
if err = g.SetKeybinding(NEWS_VIEW, c.KeyCtrlO, c.ModNone, LoadContent); err != nil {
log.Fatal("Failed to set keybindings")
}
if err = g.SetKeybinding(NEWS_VIEW, c.KeyCtrlO, c.ModAlt, OpenBrowser); err != nil {
log.Fatal("Failed to set keybindings")
}
if err = g.SetKeybinding("", c.KeyCtrlH, c.ModNone, Help); err != nil {
log.Fatal("Failed to set keybindings")
}
// run the mainloop
if err = g.MainLoop(); err != nil && err != c.ErrQuit {
log.Println("terminews exited unexpectedly: ", err)
}
log.Println("Exiting")
}