Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
terminal.hh File Reference
#include <limits>
#include <string>
#include "nix/util/file-descriptor.hh"

Go to the source code of this file.

Enumerations

enum class  StandardOutputStream { Stdout = 1 , Stderr = 2 }

Functions

bool nix::isTTY (Descriptor fd)
bool nix::isOutputARealTerminal (StandardOutputStream fileno)
bool nix::shouldANSI (StandardOutputStream fileno=StandardOutputStream::Stderr)
std::string nix::filterANSIEscapes (std::string_view s, bool filterAll=false, unsigned int width=std::numeric_limits< unsigned int >::max())
std::string nix::stripANSIEscapes (std::string_view s)
void nix::updateWindowSize ()
std::pair< unsigned short, unsigned short > nix::getWindowSize ()
std::string nix::getPtsName (int fd)

Function Documentation

◆ filterANSIEscapes()

std::string nix::filterANSIEscapes ( std::string_view s,
bool filterAll = false,
unsigned int width = std::numeric_limits<unsigned int>::max() )

Truncate a string to 'width' printable characters. If 'filterAll' is true, all ANSI escape sequences are filtered out. Otherwise, some escape sequences (such as colour setting) are copied but not included in the character count. Also, tabs are expanded to spaces.

◆ getPtsName()

std::string nix::getPtsName ( int fd)

Get the slave name of a pseudoterminal in a thread-safe manner.

Parameters
fdThe file descriptor of the pseudoterminal master
Returns
The slave device name as a string

◆ getWindowSize()

std::pair< unsigned short, unsigned short > nix::getWindowSize ( )
Returns
the number of rows and columns of the terminal.

The value is cached so this is quick. The cached result is computed by updateWindowSize().

◆ isOutputARealTerminal()

bool nix::isOutputARealTerminal ( StandardOutputStream fileno)

Determine whether the output is a real terminal (i.e. not dumb, not a pipe).

This is probably not what you want, you may want shouldANSI() or something more specific. Think about how the output should work with a pager or entirely non-interactive scripting use.

The user may be redirecting the Nix output to a pager, but have stderr connected to a terminal. Think about where you are outputting the text when deciding whether to use STDERR_FILENO or STDOUT_FILENO.

Parameters
filenofile descriptor number to check if it is a tty

◆ isTTY()

bool nix::isTTY ( Descriptor fd)

Determine whether

Parameters
fdis a terminal.

◆ shouldANSI()

bool nix::shouldANSI ( StandardOutputStream fileno = StandardOutputStream::Stderr)

Determine whether ANSI escape sequences are appropriate for the present output.

This follows the rules described on https://bixense.com/clicolors/ with CLICOLOR defaulted to enabled (and thus ignored).

That is to say, the following procedure is followed in order:

  • NO_COLOR or NOCOLOR set -> always disable colour
  • CLICOLOR_FORCE or FORCE_COLOR set -> enable colour
  • The output is a tty; TERM != "dumb" -> enable colour
  • Otherwise -> disable colour
Parameters
filenowhich file descriptor number to consider. Use the one you are outputting to

◆ stripANSIEscapes()

std::string nix::stripANSIEscapes ( std::string_view s)

Remove all ANSI escape sequences, leaving the rest of the string, including tabs and carriage returns, unchanged.

◆ updateWindowSize()

void nix::updateWindowSize ( )

Recalculate the window size, updating a global variable.

Used in the SIGWINCH signal handler on Unix, for example.