-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
142 lines (126 loc) · 2.39 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
package main
import (
"github.com/gdamore/tcell/v2"
"log"
"os"
)
func initScreen() (tcell.Screen, error) {
s, err := tcell.NewScreen()
if err != nil {
return nil, err
}
if err := s.Init(); err != nil {
return nil, err
}
s.SetStyle(defStyle)
s.EnablePaste()
s.Clear()
return s, nil
}
func fatal(ui *UserInterface, v ...any) {
cleanup(ui)
log.Fatal(v...)
}
func handleEventKey(ui *UserInterface, ev *tcell.EventKey) {
switch ev.Key() {
case tcell.KeyEnter:
err := ui.withoutScreen(func() error {
mail := ui.SelectedMail()
return mail.Show()
})
if err != nil {
fatal(ui, err)
}
case tcell.KeyRune:
mail := ui.SelectedMail()
switch ev.Rune() {
case 'r':
err := ui.withoutScreen(func() error {
mail := ui.SelectedMail()
return mail.Reply()
})
if err != nil {
fatal(ui, err)
}
case 'd':
err := os.Remove(mail.Path)
if err != nil {
fatal(ui, err)
}
case 'f':
mail.Flag(Flagged)
case 'F':
mail.Flag(Unflagged)
case 's':
mail.Flag(Unseen)
case 'S':
mail.Flag(Seen)
case 't':
mail.Flag(Untrashed)
case 'T':
mail.Flag(Trashed)
default:
return
}
// TODO: Consider using `mflag -v` above to determine the
// new file names and thereby keep the sequence in tact.
err := ui.Refresh()
if err != nil {
fatal(ui, err)
}
ui.Screen.Clear()
ui.Draw()
case tcell.KeyDown:
ui.NextMail()
case tcell.KeyUp:
ui.PrevMail()
case tcell.KeyPgDn:
ui.NextPage()
case tcell.KeyPgUp:
ui.PrevPage()
case tcell.KeyCtrlL:
ui.Screen.Sync()
}
}
func eventLoop(ui *UserInterface) {
for {
ui.Screen.Show()
ev := ui.Screen.PollEvent()
switch ev := ev.(type) {
case *tcell.EventResize:
ui.Draw()
ui.Screen.Sync()
case *tcell.EventKey:
if ev.Key() == tcell.KeyEscape ||
ev.Key() == tcell.KeyCtrlC ||
(ev.Key() == tcell.KeyRune && ev.Rune() == 'q') {
return
}
handleEventKey(ui, ev)
}
}
}
func cleanup(ui *UserInterface) {
// You have to catch panics in a defer, clean up, and
// re-raise them - otherwise your application can
// die without leaving any diagnostic trace.
maybePanic := recover()
ui.Screen.Fini()
if maybePanic != nil {
panic(maybePanic)
}
}
func main() {
mails, err := mscan()
if err != nil {
log.Fatal(err)
}
screen, err := initScreen()
if err != nil {
log.Fatal(err)
}
ui := NewUI(mails, screen)
defer cleanup(ui)
ui.Draw()
eventLoop(ui)
}