module TermBuf::SizeDetector

Overview

Works out how big the terminal is, trying each source in turn and taking the first that answers.

The ioctl comes first because it is exact, free, and needs no cooperation from the terminal. Everything after it is a fallback for the cases where there is no controlling terminal to ask — a pipe, a pty without a size set, a terminal reached across a connection that did not forward one.

Extended Modules

Defined in:

termbuf/caps/screen_size.cr

Constant Summary

TIOCGWINSZ = 21523_u64

Instance Method Summary

Instance Method Detail

def detect(fd : Int32 | Nil = nil, env : Hash(String, String) = ENV.to_h) : ScreenSize #

In order: the kernel, the environment, then the shell utilities.


[View source]
def from_commands : ScreenSize | Nil #

stty and tput, which reach the same ioctl by another route. Worth trying because they may find a controlling terminal this process cannot see through its own descriptors.


[View source]
def from_env(env : Hash(String, String)) : ScreenSize | Nil #

COLUMNS and LINES, which a shell exports and which can be set by hand when nothing else knows.


[View source]
def from_ioctl(fd : Int32 | Nil = nil) : ScreenSize | Nil #

TIOCGWINSZ on the given descriptor, or on stdout, stdin, and stderr in turn when none is named. A process with its output redirected often still has a terminal on one of the others.


[View source]