-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbulletin.go
87 lines (78 loc) · 2.2 KB
/
bulletin.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
package main
import (
"fmt"
ui "github.com/gizak/termui/v3"
"github.com/gizak/termui/v3/widgets"
"time"
)
type bulletin struct {
message string
title string
color ui.Color
speaker func(*bulletin) // tipo, wat the fuck... no me esperaba que iba a funcionar. Brain implosion
border bool
*fitting
}
func InitBulletin(myBulletin *bulletin) {
width, height := ui.TerminalDimensions()
speaks := widgets.NewParagraph()
//speaks.SetRect(2*width/3, 0, width+1, height/2+1)
speaks.SetRect(myBulletin.fitting.getRect())
speaks.Border = myBulletin.border
speaks.Title = myBulletin.title
speaks.TextStyle = ui.NewStyle(colorWheel[selectedTheme])
speaks.Text = welcomeText
ui.Render(speaks)
handle := func(input *bulletin) { // Closure is so cool
widthRefresh, heightRefresh := ui.TerminalDimensions()
if widthRefresh != width || heightRefresh != height {
width = widthRefresh
height = heightRefresh
speaks.SetRect(input.fitting.getRect())
}
speaks.Text = input.message
if input.color == ui.ColorRed {
speaks.TextStyle = ui.NewStyle(input.color)
} else {
speaks.TextStyle = ui.NewStyle(theme())
}
if input.title != "" {
speaks.Title = input.title
}
speaks.Border = input.border
ui.Render(speaks)
}
myBulletin.speaker = handle
}
func (myBulletin *bulletin) Refresh() {
myBulletin.speaker(myBulletin)
}
func (myBulletin *bulletin) Post(str string) {
myBulletin.message = str
myBulletin.color = theme()
myBulletin.speaker(myBulletin)
}
func (myBulletin *bulletin) Error(message string, err error) {
if err!=nil {
message = fmt.Sprintf("%s\n%s",message, err)
myBulletin.message = message
} else {
myBulletin.message = message
}
myBulletin.color = ui.ColorRed
myBulletin.speaker(myBulletin)
}
func NewBulletin() bulletin {
fit := CreateFitting([3]int{5, 6, 0}, [3]int{0, 0, 0}, [3]int{1, 1, 1}, [3]int{2, 3, 0})
return bulletin{title: "Status", color: theme(), fitting: fit, message: ""}
}
func displayMessage(input bulletin, keyPress <-chan string, timeToDisplay time.Duration) {
for i := 0; i < 1000; i++ {
pressed := AskForKeyPress()
if pressed == "<Space>" || pressed == "<Enter>" {
break
}
input.Post(pressed)
time.Sleep(timeToDisplay * time.Millisecond)
}
}