okami.mom/ultraviolet

git clone https://git.nkp.pet/okami.mom/ultraviolet

Ayman Bagabasfeat: add cross-platform console I/O interface (#78)6ea2266

upstream
947 B39 linesraw
1//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris || aix
2// +build darwin dragonfly freebsd linux netbsd openbsd solaris aix
3
4package uv
5
6import (
7	"context"
8	"os"
9	"os/signal"
10	"syscall"
11
12	"golang.org/x/sys/unix"
13)
14
15func openTTY() (inTty, outTty *os.File, err error) {
16	f, err := os.OpenFile("/dev/tty", os.O_RDWR, 0)
17	if err != nil {
18		return nil, nil, err //nolint:wrapcheck
19	}
20	return f, f, nil
21}
22
23func suspend() (err error) {
24	// Send SIGTSTP to the entire process group.
25	c := make(chan os.Signal, 1)
26	signal.Notify(c, syscall.SIGCONT)
27	err = syscall.Kill(0, syscall.SIGTSTP)
28	// blocks until a CONT happens...
29	<-c
30	return
31}
32
33func notifyWinch(c chan os.Signal, sigs ...os.Signal) {
34	signal.Notify(c, append(sigs, unix.SIGWINCH)...)
35}
36
37func notifyWinchContext(ctx context.Context, sigs ...os.Signal) (context.Context, context.CancelFunc) {
38	return signal.NotifyContext(ctx, append(sigs, unix.SIGWINCH)...)
39}