File: pb_plan9.go

package info (click to toggle)
golang-github-cheggaaa-pb.v3 3.1.5-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 344 kB
  • sloc: makefile: 2
file content (70 lines) | stat: -rw-r--r-- 1,270 bytes parent folder | download | duplicates (3)
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()
	}
}