-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
92 lines (77 loc) · 1.81 KB
/
logger.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
package eugor
import (
"eugor/termboxext"
"fmt"
"github.com/nsf/termbox-go"
)
var nilEvent Event = Event{}
type Logger struct {
events []Event
Render bool
}
var GlobalLog = &Logger{}
func (l *Logger) Append(e termbox.Event) {
var message string
switch e.Key {
default:
message = string(e.Ch)
case termbox.KeyArrowUp:
message = string('↑')
case termbox.KeyArrowDown:
message = string('↓')
case termbox.KeyArrowLeft:
message = string('←')
case termbox.KeyArrowRight:
message = string('→')
}
event := Event{LogLevel: Info, Message: fmt.Sprintf("Received event %s", message)}
l.AppendEvent(event)
}
func (l *Logger) AppendEvent(event Event) {
l.events = append(l.events, nilEvent)
copy(l.events[1:], l.events[0:])
l.events[0] = event
}
func (l *Logger) ToggleRender() {
l.Render = !l.Render
}
func (l *Logger) Draw() {
if l.Render == false {
return
}
l.clear()
l.drawLogEvents()
l.drawBorder()
}
func (l *Logger) StartingY() int {
_, height := termbox.Size()
return height - (height / 4)
}
func (l *Logger) clear() {
width, height := termbox.Size()
termboxext.Fill(0, l.StartingY(), width, height/4, ' ', termbox.ColorCyan, termbox.ColorBlack)
}
func (l *Logger) drawBorder() {
width, height := termbox.Size()
termboxext.DrawSimpleBox(0, l.StartingY(), width, height/4, termbox.ColorCyan, termbox.ColorBlack)
}
func (l *Logger) drawLogEvents() {
startingY := l.StartingY() + 1
if len(l.events) == 0 {
termboxext.DrawString(1, startingY, "There is nothing to log", termbox.ColorWhite, termbox.ColorBlack)
}
for index, event := range l.events {
termboxext.DrawString(1, startingY+index, event.Message, termbox.ColorWhite, termbox.ColorBlack)
}
}
type LogLevel uint8
const (
Debug LogLevel = 0xFF - iota
Info
Warn
Error
)
type Event struct {
LogLevel LogLevel
Message string
}