-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbarely.go
113 lines (92 loc) · 2.13 KB
/
barely.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
// Copyright 2015 Lukas Weber. All rights reserved.
// Use of this source code is governed by the MIT-styled
// license that can be found in the LICENSE file.
// barely is a notmuch-frontend inspired by alot.
package main
import (
"bytes"
"flag"
"fmt"
"log"
"math/rand"
"os"
"runtime"
"time"
termbox "github.com/nsf/termbox-go"
)
const (
// Version is the version number of barely.
Version = "0.1"
// UserAgent is the User Agent string attached to mail messages.
UserAgent = "barely/" + Version
// StderrLogFile is a file in the temporary files directory
// where Xapians messy stderr output gets redirected to.
StderrLogFile = "barely.log"
)
var logbuf bytes.Buffer
// redirect panics to stdout
func recoverPanic() {
if r := recover(); r != nil {
termbox.Close()
fmt.Println(r)
buf := make([]byte, 2048)
l := runtime.Stack(buf, true)
fmt.Println(string(buf[:l]))
}
if len(logbuf.Bytes()) != 0 {
fmt.Println("Debug log:")
fmt.Print(logbuf.String())
}
}
func tmpDir() string {
return os.TempDir() + fmt.Sprintf("/barely-%d", os.Getpid())
}
func main() {
defer recoverPanic()
showcfg := flag.Bool("config", false, "Print example config file.")
flag.Parse()
if *showcfg {
fmt.Print(DefaultCfg)
return
}
var buffers BufferStack
var err error
stderrFile, err := os.Create(os.TempDir() + "/" + StderrLogFile)
if err == nil {
// os.Stderr = stderrFile
} else {
fmt.Println(err)
}
log.SetOutput(&logbuf)
rand.Seed(time.Now().Unix())
LoadConfig()
err = termbox.Init()
if err != nil {
log.Fatal(err)
}
termbox.SetOutputMode(termbox.Output256)
buffers.Init()
for len(buffers.buffers) > 0 {
err = termbox.Flush()
if err != nil {
log.Fatal(err)
}
event := termbox.PollEvent()
buffers.HandleEvent(&event)
}
termbox.Close()
// remove the stderrFile if it is empty
if stderrFile != nil {
stderrFile.Sync()
info, err := stderrFile.Stat()
stderrFile.Close()
if err == nil && info.Size() == 0 {
os.Remove(stderrFile.Name())
}
}
// remove the temporary directory
err = os.RemoveAll(tmpDir())
if err != nil {
log.Println("Could not remove temporary files: " + err.Error())
}
}