forked from cheggaaa/pb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pb_plan9.go
70 lines (61 loc) · 1.24 KB
/
pb_plan9.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
package pb
import (
"errors"
"os"
"os/signal"
"sync"
"syscall"
)
var ErrPoolWasStarted = errors.New("Bar pool was started")
var (
echoLockMutex sync.Mutex
consctl *os.File
)
// terminalWidth returns width of the terminal.
func terminalWidth() (int, error) {
return 0, errors.New("Not Supported")
}
func lockEcho() (shutdownCh chan struct{}, err error) {
echoLockMutex.Lock()
defer echoLockMutex.Unlock()
if consctl != nil {
return nil, ErrPoolWasStarted
}
consctl, err = os.OpenFile("/dev/consctl", os.O_WRONLY, 0)
if err != nil {
return nil, err
}
_, err = consctl.WriteString("rawon")
if err != nil {
consctl.Close()
consctl = nil
return nil, err
}
shutdownCh = make(chan struct{})
go catchTerminate(shutdownCh)
return
}
func unlockEcho() error {
echoLockMutex.Lock()
defer echoLockMutex.Unlock()
if consctl == nil {
return nil
}
if err := consctl.Close(); err != nil {
return err
}
consctl = nil
return nil
}
// listen exit signals and restore terminal state
func catchTerminate(shutdownCh chan struct{}) {
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, syscall.SIGTERM, syscall.SIGKILL)
defer signal.Stop(sig)
select {
case <-shutdownCh:
unlockEcho()
case <-sig:
unlockEcho()
}
}