-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsignal_test.go
54 lines (44 loc) · 1.26 KB
/
signal_test.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
package anansi_test
import (
"errors"
"log"
"os"
"syscall"
"github.com/jcorbin/anansi"
)
// Handling some of the usual terminal lifecycle signals.
func ExampleSignal() {
var (
halt = anansi.Notify(syscall.SIGTERM, syscall.SIGHUP)
stop = anansi.Notify(syscall.SIGINT)
resize = anansi.Notify(syscall.SIGWINCH)
)
term := anansi.NewTerm(
os.Stdin, os.Stdout,
&halt,
&resize,
)
anansi.MustRun(term.RunWith(func(term *anansi.Term) error {
for {
select {
case sig := <-halt.C:
// Terminate program asap on a halting signal; wrapping it in
// anansi.SigErr provides transparency both internally, when
// anansi.MustRun logs, and externally by setting the normative
// exit code "killed by signal X" status code.
return anansi.SigErr(sig)
case <-stop.C:
// Interrupt, on the other hand, may not always result in
// immediate halt, it may only mean "stop / cancel whatever
// operation is being currently run, but don't halt". Here we
// just return a regular error, which will cause a normal "exit
// code 1", externally hiding the fact that we stopped due to
// SIGINT.
return errors.New("stop")
case <-resize.C:
sz, _ := term.Size()
log.Printf("Terminal resized to %v", sz)
}
}
}))
}