Removes Control Sequences from strings. By default it will
strip all known Control Sequences, including CSI/OSC sequences, two
character sequences starting with ESC, and all C0 control characters,
including newlines. You can fine tune this behavior with the ctl
parameter.
Usage
strip_ctl(x, ctl = "all", warn = getOption("fansi.warn", TRUE), strip)Arguments
- x
a character vector or object that can be coerced to such.
- ctl
character, any combination of the following values (see details):
"nl": strip newlines.
"c0": strip all other "C0" control characters (i.e. x01-x1f, x7F), except for newlines and the actual ESC character.
"sgr": strip ANSI CSI SGR sequences.
"csi": strip all non-SGR csi sequences.
"esc": strip 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" (see details).
- warn
TRUE (default) or FALSE, whether to warn when potentially problematic Control Sequences are encountered. These could cause the assumptions
fansimakes about how strings are rendered on your display to be incorrect, for example by moving the cursor (see?fansi). At most one warning will be issued per element in each input vector. Will also warn about some badly encoded UTF-8 strings, but a lack of UTF-8 warnings is not a guarantee of correct encoding (usevalidUTF8for that).- strip
character, deprecated in favor of
ctl.
Details
The ctl value contains the names of non-overlapping subsets of the
known Control Sequences (e.g. "csi" does not contain "sgr", and "c0" does
not contain newlines). The one exception is "all" which means strip every
known sequence. If you combine "all" with any other options then everything
but those options will be stripped.
See also
?fansi for details on how Control Sequences are
interpreted, particularly if you are getting unexpected results,
unhandled_ctl for detecting bad control sequences.
Examples
string <- "hello\033k\033[45p world\n\033[31mgoodbye\a moon"
strip_ctl(string)
#> [1] "hello worldgoodbye moon"
strip_ctl(string, c("nl", "c0", "sgr", "csi", "esc")) # equivalently
#> [1] "hello worldgoodbye moon"
strip_ctl(string, "sgr")
#> [1] "hello\033k\033[45p world\ngoodbye\a moon"
strip_ctl(string, c("c0", "esc"))
#> [1] "hello45p world\n31mgoodbye moon"
## everything but C0 controls, we need to specify "nl"
## in addition to "c0" since "nl" is not part of "c0"
## as far as the `strip` argument is concerned
strip_ctl(string, c("all", "nl", "c0"))
#> [1] "hello world\ngoodbye\a moon"