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 77 78 79 80 81 82 83
|
//go:build aix || darwin || dragonfly || freebsd || (linux && !appengine) || netbsd || openbsd || os400 || solaris || zos
package platform
import (
"context"
"os"
"os/signal"
"sync"
"syscall"
"github.com/ergochat/readline/internal/term"
)
const (
IsWindows = false
)
// SuspendProcess suspends the process with SIGTSTP,
// then blocks until it is resumed.
func SuspendProcess() {
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGCONT)
defer stop()
p, err := os.FindProcess(os.Getpid())
if err != nil {
panic(err)
}
p.Signal(syscall.SIGTSTP)
// wait for SIGCONT
<-ctx.Done()
}
// getWidthHeight of the terminal using given file descriptor
func getWidthHeight(stdoutFd int) (width int, height int) {
width, height, err := term.GetSize(stdoutFd)
if err != nil {
return -1, -1
}
return
}
// GetScreenSize returns the width/height of the terminal or -1,-1 or error
func GetScreenSize() (width int, height int) {
width, height = getWidthHeight(syscall.Stdout)
if width < 0 {
width, height = getWidthHeight(syscall.Stderr)
}
return
}
func DefaultIsTerminal() bool {
return term.IsTerminal(syscall.Stdin) && (term.IsTerminal(syscall.Stdout) || term.IsTerminal(syscall.Stderr))
}
// -----------------------------------------------------------------------------
var (
sizeChange sync.Once
sizeChangeCallback func()
)
func DefaultOnWidthChanged(f func()) {
DefaultOnSizeChanged(f)
}
func DefaultOnSizeChanged(f func()) {
sizeChangeCallback = f
sizeChange.Do(func() {
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGWINCH)
go func() {
for {
_, ok := <-ch
if !ok {
break
}
sizeChangeCallback()
}
}()
})
}
|