// SPDX-FileCopyrightText: (C) Copyright 2026 Pim van Pelt // SPDX-License-Identifier: Apache-2.0 //go:build openbsd || freebsd || netbsd || dragonfly || darwin package keypress import "golang.org/x/sys/unix" // cbreak puts the terminal into cbreak mode (no canonical input/echo) so a // single keystroke is available, leaving output post-processing intact. The // BSDs use the TIOCGETA/TIOCSETA termios ioctls (Linux uses TCGETS/TCSETS). It // returns the previous termios for restore, or an error if fd is not a tty. func cbreak(fd int) (*unix.Termios, error) { old, err := unix.IoctlGetTermios(fd, unix.TIOCGETA) if err != nil { return nil, err } t := *old t.Lflag &^= unix.ICANON | unix.ECHO | unix.ECHOE | unix.ECHOK | unix.ECHONL t.Cc[unix.VMIN] = 1 t.Cc[unix.VTIME] = 0 if err := unix.IoctlSetTermios(fd, unix.TIOCSETA, &t); err != nil { return nil, err } return old, nil } // restore reverts the terminal to the settings captured by cbreak. func restore(fd int, old *unix.Termios) error { return unix.IoctlSetTermios(fd, unix.TIOCSETAF, old) }