blob: 5b509ac4046a325b54a07bfa1386502c861edf5d (
plain)
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
|
package statusline
import (
"io"
"time"
)
type AsyncStatusLine struct {
lines chan string
end1 chan struct{}
end2 chan struct{}
}
func NewAsyncStatusLine(out io.Writer, d time.Duration) *AsyncStatusLine {
ret := &AsyncStatusLine{
lines: make(chan string),
end1: make(chan struct{}),
end2: make(chan struct{}),
}
go func() {
sl := NewStatusLine(out)
ticker := time.NewTicker(d)
var oldLine string
var newLine string
dirty := false
for {
select {
case <-ticker.C:
if dirty && newLine != oldLine {
sl.Put(newLine)
dirty = false
}
case l := <-ret.lines:
newLine = l
dirty = true
case <-ret.end1:
sl.End()
ticker.Stop()
end2 <- struct{}{}
close(end2)
return
}
}
}()
return ret
}
func (sl *AsyncStatusLine) Put(line string) {
sl.lines <- line
}
func (sl *AsyncStatusLine) End() {
sl.end1 <- struct{}{}
close(sl.lines)
close(sl.end)
<-sl.end2
}
|