-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmessages.go
69 lines (58 loc) · 1.57 KB
/
messages.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
package main
import (
"log"
"time"
"github.com/gdamore/tcell"
"github.com/rivo/tview"
)
// Defines a message
type message struct {
isError bool
message string
time time.Time
}
// messagesInit called when the app starts up, to perform any intialization
// required.
func messagesInit() {
addMessage(message{isError: false, message: "Message subsystem intialized"})
}
// messagesShutdown is called then the app is closed, prints any error messages
// in the stack to stderr.
func messagesShutdown() {
for _, value := range app.msgs {
if value.isError {
log.Println(value.message)
}
}
}
// addMessage adds a message to the stack.
func addMessage(message message) {
message.time = time.Now()
app.msgs = append(app.msgs, message)
}
// showMessagesUI called when we want to display the interface for view the
// message stack.
func showMessagesUI() {
app.ui.idx.Clear()
app.ui.idx.SetSelectable(false, false)
for key, value := range app.msgs {
cell := tview.NewTableCell("[" + value.time.Format("2006/01/02 15:04:05") + "]")
cell.SetAlign(tview.AlignLeft)
cell.SetBackgroundColor(tcell.ColorGray)
cell.SetTextColor(tcell.ColorDefault)
app.ui.idx.SetCell(key, 0, cell)
app.ui.idx.SetCell(key, 0, cell)
cell = tview.NewTableCell(value.message)
cell.SetAlign(tview.AlignLeft)
cell.SetBackgroundColor(tcell.ColorGray)
if value.isError {
cell.SetTextColor(tcell.ColorRed)
} else {
cell.SetTextColor(tcell.ColorDefault)
}
cell.SetExpansion(1)
app.ui.idx.SetCell(key, 1, cell)
}
app.ui.idx.SetSelectable(true, false)
app.ui.idx.Select(0, 0)
}