keys

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 15, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package keys implements the portable, TTY-agnostic half of the "press q to quit" affordance shared by `puzzle dev` and `puzzle preview`. Listen is deliberately kept pure over an io.Reader (rather than reaching for os.Stdin directly) so it can be unit tested without a real terminal: a test feeds it a bytes.Reader and asserts on the returned channel. The termios/TTY plumbing that puts a real stdin into cbreak mode lives in the build-tagged keys_unix.go / keys_other.go files.

keys_termios_linux.go pins the Linux ioctl requests for reading and writing the terminal attributes: TCGETS/TCSETS (BSD/darwin use TIOCGETA/TIOCSETA — see keys_termios_bsd.go). This split mirrors how golang.org/x/term selects the constant per platform.

keys_unix.go puts stdin into "cbreak" mode so `puzzle dev` and `puzzle preview` can read a single keypress (the 'q' quit) without waiting for Enter, and without echoing the keystroke to the terminal.

We deliberately use CBREAK, not full raw mode:

  • ISIG is left ON, so Ctrl+C still generates SIGINT — the existing signal path keeps working exactly as before; 'q' is an addition, not a replacement.
  • The output flags (OPOST/ONLCR) are left untouched, so the rebuild/log lines the serve loop prints keep their normal "\n"→"\r\n" translation and don't stair-step across the terminal.

We only clear ICANON (line buffering) and ECHO (so the typed 'q' is invisible), and set VMIN=1/VTIME=0 for a blocking one-byte read.

The TTY gate is free: IoctlGetTermios fails when stdin is not a terminal (a pipe, file redirect, or CI), and we treat that failure as "not a TTY" and return ok=false — the feature silently switches off and nothing else changes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Listen

func Listen(ctx context.Context, r io.Reader) <-chan struct{}

Listen spawns a goroutine that reads r one byte at a time and, when it sees 'q' or 'Q', signals on the returned channel (a single non-blocking send) and returns. On read error/EOF or when ctx is cancelled the goroutine exits silently without signalling — so a piped/redirected stdin that hits EOF (CI, tests) never triggers a spurious quit.

The channel is buffered (size 1) so the send never blocks even if no one is selecting on it yet; callers select on it as one arm of their serve-loop select.

func StdinCbreak

func StdinCbreak() (restore func(), ok bool)

StdinCbreak switches os.Stdin into cbreak mode and returns a restore func that reverts it to the original settings. When stdin is not a TTY (get-termios fails) it returns (nil, false) and the caller skips the key listener.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL