-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatus.go
76 lines (65 loc) · 1.44 KB
/
status.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
package main
import (
"fmt"
"time"
"github.com/jroimartin/gocui"
)
type statusInfo struct {
message string
duration int
gui *gocui.Gui
}
const (
statusView = "error_view"
)
var currentStatus *statusInfo = nil
func statusLayout(g *gocui.Gui) error {
status := currentStatus
if currentStatus != nil {
maxX, _ := g.Size()
if v, err := g.SetView(statusView, maxX/3+2, 5, maxX-maxX/3-2, 10); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.FgColor = gocui.ColorYellow
fmt.Fprintln(v, "\u001b[31m"+status.message)
v.Title = "Status"
v.Editable = true
}
}
return nil
}
// duration is in seconds
func createStatus(status statusInfo) error {
g := status.gui
v, err := g.View(statusView)
if err == nil {
fmt.Fprint(v, "\u001b[31m"+status.message)
return nil
}
maxX, _ := g.Size()
if v, err := g.SetView(statusView, maxX/2-15, 5, maxX/2+15, 7); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.FgColor = gocui.ColorYellow
fmt.Fprintln(v, "\u001b[31m"+status.message)
v.Title = "Status"
v.Editable = true
}
// scheduale the error to be removed
go func(g *gocui.Gui, duration int) {
time.Sleep(time.Duration(duration) * time.Second)
g.Update(func(gui *gocui.Gui) error {
// wait before removing
v, err := gui.View(statusView)
if err != nil {
return err
}
gui.DeleteView(v.Name())
currentStatus = nil
return nil
})
}(g, status.duration)
return nil
}