fansi (version 0.4.0)

has_ctl: Checks for Presence of Control Sequences

Description

has_ctl checks for any Control Sequence, whereas has_sgr checks only for ANSI CSI SGR sequences. You can check for different types of sequences with the ctl parameter.

Usage

has_ctl(x, ctl = "all", warn = getOption("fansi.warn"), which)

has_sgr(x, warn = getOption("fansi.warn"))

Arguments

x

a character vector or object that can be coerced to character.

ctl

character, which Control Sequences should be treated specially. See the "_ctl vs. _sgr" section for details.

  • "nl": newlines.

  • "c0": all other "C0" control characters (i.e. 0x01-0x1f, 0x7F), except for newlines and the actual ESC (0x1B) character.

  • "sgr": ANSI CSI SGR sequences.

  • "csi": all non-SGR ANSI CSI sequences.

  • "esc": all other escape sequences.

  • "all": all of the above, except when used in combination with any of the above, in which case it means "all but".

warn

TRUE (default) or FALSE, whether to warn when potentially problematic Control Sequences are encountered. These could cause the assumptions fansi makes about how strings are rendered on your display to be incorrect, for example by moving the cursor (see fansi).

which

character, deprecated in favor of ctl.

Value

logical of same length as x; NA values in x result in NA values in return

_ctl vs. _sgr

The *_ctl versions of the functions treat all Control Sequences specially by default. Special treatment is context dependent, and may include detecting them and/or computing their display/character width as zero. For the SGR subset of the ANSI CSI sequences, fansi will also parse, interpret, and reapply the text styles they encode if needed. You can modify whether a Control Sequence is treated specially with the ctl parameter. You can exclude a type of Control Sequence from special treatment by combining "all" with that type of sequence (e.g. ctl=c("all", "nl") for special treatment of all Control Sequences but newlines). The *_sgr versions only treat ANSI CSI SGR sequences specially, and are equivalent to the *_ctl versions with the ctl parameter set to "sgr".

See Also

fansi for details on how Control Sequences are interpreted, particularly if you are getting unexpected results.

Examples

Run this code
# NOT RUN {
has_ctl("hello world")
has_ctl("hello\nworld")
has_ctl("hello\nworld", "sgr")
has_ctl("hello\033[31mworld\033[m", "sgr")
has_sgr("hello\033[31mworld\033[m")
has_sgr("hello\nworld")
# }

Run the code above in your browser using DataCamp Workspace