Skip to content

Commit

Permalink
Fix #73: Always specify fallbacks for 'getOption'
Browse files Browse the repository at this point in the history
  • Loading branch information
brodieG committed Oct 24, 2021
2 parents 300a7f9 + f655a2e commit 3f85ece
Show file tree
Hide file tree
Showing 40 changed files with 183 additions and 164 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Package: fansi
Title: ANSI Control Sequence Aware String Functions
Description: Counterparts to R string manipulation functions that account for
the effects of ANSI text formatting control sequences.
Version: 0.5.0.9001
Version: 0.5.0.9002
Authors@R: c(
person("Brodie", "Gaslam", email="[email protected]",
role=c("aut", "cre")),
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
export("substr2_ctl<-")
export("substr_ctl<-")
export(close_state)
export(dflt_css)
export(dflt_term_cap)
export(fansi_lines)
export(fwl)
export(has_ctl)
Expand Down
5 changes: 4 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

### Features

* [#26](https://github.com/brodieG/fansi/issues/26) Replacement forms of
* [#26](https://github.com/brodieG/fansi/issues/26) Add replacement forms of
`substr_cl` (i.e `substr_ctl<-`).
* [#58](https://github.com/brodieG/fansi/issues/58) Add support for OSC-anchored
URLs.
Expand All @@ -26,6 +26,9 @@
SGR state.
* [#71](https://github.com/brodieG/fansi/issues/71) Functions that write SGR are
now more parsimonious (see "Behavior Changes" below).
* [#73](https://github.com/brodieG/fansi/issues/73) Default parameter values
retrieved with `getOption` now always have explicit fallback values defined
(h/t @gadenbui).
* More granular error messages for `unhandled_ctl` for adjacent _Control
Sequences_.
* `term.cap` parameter now accepts "all" as value, like the `ctl` parameter.
Expand Down
2 changes: 1 addition & 1 deletion R/internal.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ ctl_as_int <- function(x) .Call(FANSI_ctl_as_int, as.integer(x))
## testing interface for bridging

bridge <- function(
end, restart, term.cap=getOption("fansi.term.cap"),
end, restart, term.cap=getOption("fansi.term.cap", dflt_term_cap()),
normalize=getOption('fansi.normalize', FALSE)
) {
VAL_IN_ENV(term.cap=term.cap)
Expand Down
26 changes: 0 additions & 26 deletions R/load.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,6 @@
# Scheme defaults are fairly complex...

check_assumptions()

# Default options; beware of defining default options that may have different
# values during package install, which is when this list is contructed, and
# function runtime

.default.opts <- list(
fansi.tabs.as.spaces=FALSE,
fansi.tab.stops=8L,
fansi.warn=TRUE,
fansi.ctrl="all",
fansi.term.cap=c(
if(isTRUE(Sys.getenv('COLORTERM') %in% c('truecolor', '24bit')))
'truecolor',
'bright', '256'
),
# This is not a particularly good default setting as it may exceed or fail
# to cover the interline distance when two lines have background colors. To
# ensure lines are exactly touching use inline-block, although that has it's
# own issues. Otherwise specify your own values.

fansi.css="PRE.fansi SPAN {padding-top: .25em; padding-bottom: .25em};"
)
# Scheme defaults are fairly complex...

existing.opts <- options()
options(.default.opts[setdiff(names(.default.opts), names(existing.opts))])
R.ver.gte.3.2.2 <<- getRversion() >= "3.2.2"
}
.onAttach <- function(libname, pkgname) {
Expand Down
47 changes: 38 additions & 9 deletions R/misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
#' ) )

tabs_as_spaces <- function(
x, tab.stops=getOption('fansi.tab.stops'), warn=getOption('fansi.warn'),
ctl='all'
x, tab.stops=getOption('fansi.tab.stops', 8L),
warn=getOption('fansi.warn', TRUE), ctl='all'
) {

if(!is.character(x)) x <- as.character(x)
Expand Down Expand Up @@ -99,7 +99,8 @@ tabs_as_spaces <- function(
#' CSI SGR sequences are displayed as well.
#'
#' You should compare the screen output from this function to
#' `getOption('fansi.term.cap')` to ensure that they are self consistent.
#' `getOption('fansi.term.cap', dflt_term_cap)` to ensure that they are self
#' consistent.
#'
#' By default `fansi` assumes terminals support bright and 256 color
#' modes, and also tests for truecolor support via the $COLORTERM system
Expand All @@ -108,10 +109,10 @@ tabs_as_spaces <- function(
#' Functions with the `term.cap` parameter like `substr_ctl` will warn if they
#' encounter 256 or true color SGR sequences and `term.cap` indicates they are
#' unsupported as such a terminal may misinterpret those sequences. Bright
#' codes in terminals that do not support them are more likely to be silently
#' ignored, so `fansi` functions do not warn about those.
#' codes and OSC encoded URLs in terminals that do not support will likely be
#' silently ignored, so `fansi` functions do not warn about those.
#'
#' @inherit has_ctl seealso
#' @seealso [`dflt_term_cap`], [`has_ctl`].
#' @export
#' @return character the test vector, invisibly
#' @examples
Expand Down Expand Up @@ -228,7 +229,7 @@ html_code_block <- function(x, class='fansi-output') {
#'
#' This is a convenience function designed for use within an `rmarkdown`
#' document. It overrides the `knitr` output hooks by using
#' `knitr::knit_hooks$set`. It replaces the hooks with ones that convert
#' `knitr::knit_hooks$set`. It replaces the hooks with ones that convert
#' _Control Sequences_ into HTML. In addition to replacing the hook functions,
#' this will output a &lt;STYLE&gt; HTML block to stdout. These two actions are
#' side effects as a result of which R chunks in the `rmarkdown` document that
Expand Down Expand Up @@ -298,7 +299,7 @@ html_code_block <- function(x, class='fansi-output') {
#' ## them (alternatively we could have done output as part of this call too)
#'
#' styles <- c(
#' getOption('fansi.style'), # default style
#' getOption('fansi.style', dflt_css()), # default style
#' "PRE.fansi CODE {background-color: transparent;}",
#' "PRE.fansi-error {background-color: #DD5555;}",
#' "PRE.fansi-warning {background-color: #DDDD55;}",
Expand All @@ -325,7 +326,7 @@ set_knit_hooks <- function(
proc.fun=function(x, class)
html_code_block(to_html(html_esc(x)), class=class),
class=sprintf("fansi fansi-%s", which),
style=getOption("fansi.css"),
style=getOption("fansi.css", dflt_css()),
split.nl=FALSE,
.test=FALSE
) {
Expand Down Expand Up @@ -509,3 +510,31 @@ fwl <- function(..., end='<END>\033[0m') {
writeLines(c(..., end))
}

#' Default Arg Helper Funs
#'
#' Terminal capabilities are assumed to include bright and 256 color SGR codes,
#' and will detect 24 bit color support based on the `COLORTERM` environment
#' variable.
#'
#' Default CSS may exceed or fail to cover the interline distance when two lines
#' have background colors. To ensure lines are exactly touching use
#' inline-block, although that has its own issues. Otherwise specify your own
#' values.
#'
#' @seealso [`term_cap_test`].
#' @export
#' @return character to use as default value for `fansi` parameter.

dflt_term_cap <- function() {
c(
if(isTRUE(Sys.getenv('COLORTERM') %in% c('truecolor', '24bit')))
'truecolor',
'bright', '256'
)
}
#' @rdname dflt_term_cap
#' @export

dflt_css <- function() {
"PRE.fansi SPAN {padding-top: .25em; padding-bottom: .25em};"
}
10 changes: 6 additions & 4 deletions R/nchar.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@

nchar_ctl <- function(
x, type='chars', allowNA=FALSE, keepNA=NA, ctl='all',
warn=getOption('fansi.warn'), strip
warn=getOption('fansi.warn', TRUE), strip
) {
if(!is.logical(allowNA)) allowNA <- as.logical(allowNA)
if(length(allowNA) != 1L)
Expand Down Expand Up @@ -92,7 +92,9 @@ nchar_ctl <- function(
#' @export
#' @rdname nchar_ctl

nzchar_ctl <- function(x, keepNA=NA, ctl='all', warn=getOption('fansi.warn')) {
nzchar_ctl <- function(
x, keepNA=NA, ctl='all', warn=getOption('fansi.warn', TRUE)
) {
## modifies / creates NEW VARS in fun env
VAL_IN_ENV(x=x, ctl=ctl, warn=warn)
if(!is.logical(keepNA)) keepNA <- as.logical(keepNA)
Expand All @@ -112,7 +114,7 @@ nzchar_ctl <- function(x, keepNA=NA, ctl='all', warn=getOption('fansi.warn')) {
#' @export

nchar_sgr <- function(
x, type='chars', allowNA=FALSE, keepNA=NA, warn=getOption('fansi.warn')
x, type='chars', allowNA=FALSE, keepNA=NA, warn=getOption('fansi.warn', TRUE)
)
nchar_ctl(
x=x, type=type, allowNA=allowNA, keepNA=keepNA, warn=warn, ctl='sgr'
Expand All @@ -121,6 +123,6 @@ nchar_sgr <- function(
#' @export
#' @rdname nchar_sgr

nzchar_sgr <- function(x, keepNA=NA, warn=getOption('fansi.warn'))
nzchar_sgr <- function(x, keepNA=NA, warn=getOption('fansi.warn', TRUE))
nzchar_ctl(x=x, keepNA=keepNA, warn=warn, ctl='sgr')

3 changes: 2 additions & 1 deletion R/normalize.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@
#' ) )

normalize_state <- function(
x, warn=getOption('fansi.warn'), term.cap=getOption('fansi.term.cap'),
x, warn=getOption('fansi.warn', TRUE),
term.cap=getOption('fansi.term.cap', dflt_term_cap()),
carry=getOption('fansi.carry', FALSE)
) {
## modifies / creates NEW VARS in fun env
Expand Down
14 changes: 7 additions & 7 deletions R/sgr.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
#' ## as far as the `strip` argument is concerned
#' strip_ctl(string, c("all", "nl", "c0"))

strip_ctl <- function(x, ctl='all', warn=getOption('fansi.warn'), strip) {
strip_ctl <- function(x, ctl='all', warn=getOption('fansi.warn', TRUE), strip) {
if(!missing(strip)) {
message("Parameter `strip` has been deprecated; use `ctl` instead.")
ctl <- strip
Expand All @@ -80,7 +80,7 @@ strip_ctl <- function(x, ctl='all', warn=getOption('fansi.warn'), strip) {
#' ## convenience function, same as `strip_ctl(ctl=c('sgr', 'url'))`
#' strip_sgr(string)

strip_sgr <- function(x, warn=getOption('fansi.warn')) {
strip_sgr <- function(x, warn=getOption('fansi.warn', TRUE)) {
## modifies / creates NEW VARS in fun env
VAL_IN_ENV(x=x, warn=warn)
ctl.int <- match(c("sgr", "url"), VALID.CTL)
Expand All @@ -106,7 +106,7 @@ strip_sgr <- function(x, warn=getOption('fansi.warn')) {
#' has_ctl("hello\nworld", "sgr")
#' has_ctl("hello\033[31mworld\033[m", "sgr")

has_ctl <- function(x, ctl='all', warn=getOption('fansi.warn'), which) {
has_ctl <- function(x, ctl='all', warn=getOption('fansi.warn', TRUE), which) {
if(!missing(which)) {
message("Parameter `which` has been deprecated; use `ctl` instead.")
ctl <- which
Expand All @@ -127,7 +127,7 @@ has_ctl <- function(x, ctl='all', warn=getOption('fansi.warn'), which) {
#' @keywords internal
#' @export

has_sgr <- function(x, warn=getOption('fansi.warn'))
has_sgr <- function(x, warn=getOption('fansi.warn', TRUE))
has_ctl(x, ctl=c("sgr", "url"), warn=warn)

#' Utilities for Managing CSI and OSC State In Strings
Expand Down Expand Up @@ -155,8 +155,8 @@ has_sgr <- function(x, warn=getOption('fansi.warn'))

state_at_end <- function(
x,
warn=getOption('fansi.warn'),
term.cap=getOption('fansi.term.cap'),
warn=getOption('fansi.warn', TRUE),
term.cap=getOption('fansi.term.cap', dflt_term_cap()),
normalize=getOption('fansi.normalize', FALSE),
carry=getOption('fansi.carry', FALSE)
) {
Expand All @@ -179,7 +179,7 @@ state_at_end <- function(

close_state <- function(
x,
warn=getOption('fansi.warn'),
warn=getOption('fansi.warn', TRUE),
normalize=getOption('fansi.normalize', FALSE)
) {
## modifies / creates NEW VARS in fun env
Expand Down
6 changes: 4 additions & 2 deletions R/strsplit.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@

strsplit_ctl <- function(
x, split, fixed=FALSE, perl=FALSE, useBytes=FALSE,
warn=getOption('fansi.warn'), term.cap=getOption('fansi.term.cap'),
warn=getOption('fansi.warn', TRUE),
term.cap=getOption('fansi.term.cap', dflt_term_cap()),
ctl='all', normalize=getOption('fansi.normalize', FALSE),
carry=getOption('fansi.carry', FALSE),
terminate=getOption('fansi.terminate', TRUE)
Expand Down Expand Up @@ -153,7 +154,8 @@ strsplit_ctl <- function(

strsplit_sgr <- function(
x, split, fixed=FALSE, perl=FALSE, useBytes=FALSE,
warn=getOption('fansi.warn'), term.cap=getOption('fansi.term.cap'),
warn=getOption('fansi.warn', TRUE),
term.cap=getOption('fansi.term.cap', dflt_term_cap()),
normalize=getOption('fansi.normalize', FALSE),
carry=getOption('fansi.carry', FALSE),
terminate=getOption('fansi.terminate', TRUE)
Expand Down
16 changes: 8 additions & 8 deletions R/strtrim.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#' strtrim_ctl("\033[42mHello world\033[m", 6)

strtrim_ctl <- function(
x, width, warn=getOption('fansi.warn'), ctl='all',
x, width, warn=getOption('fansi.warn', TRUE), ctl='all',
normalize=getOption('fansi.normalize', FALSE),
carry=getOption('fansi.carry', FALSE),
terminate=getOption('fansi.terminate', TRUE)
Expand Down Expand Up @@ -76,9 +76,9 @@ strtrim_ctl <- function(
#' @rdname strtrim_ctl

strtrim2_ctl <- function(
x, width, warn=getOption('fansi.warn'),
tabs.as.spaces=getOption('fansi.tabs.as.spaces'),
tab.stops=getOption('fansi.tab.stops'),
x, width, warn=getOption('fansi.warn', TRUE),
tabs.as.spaces=getOption('fansi.tabs.as.spaces', FALSE),
tab.stops=getOption('fansi.tab.stops', 8L),
ctl='all', normalize=getOption('fansi.normalize', FALSE),
carry=getOption('fansi.carry', FALSE),
terminate=getOption('fansi.terminate', TRUE)
Expand Down Expand Up @@ -128,7 +128,7 @@ strtrim2_ctl <- function(
#' @export

strtrim_sgr <- function(
x, width, warn=getOption('fansi.warn'),
x, width, warn=getOption('fansi.warn', TRUE),
normalize=getOption('fansi.normalize', FALSE),
carry=getOption('fansi.carry', FALSE),
terminate=getOption('fansi.terminate', TRUE)
Expand All @@ -141,9 +141,9 @@ strtrim_sgr <- function(
#' @export
#' @rdname strtrim_sgr

strtrim2_sgr <- function(x, width, warn=getOption('fansi.warn'),
tabs.as.spaces=getOption('fansi.tabs.as.spaces'),
tab.stops=getOption('fansi.tab.stops'),
strtrim2_sgr <- function(x, width, warn=getOption('fansi.warn', TRUE),
tabs.as.spaces=getOption('fansi.tabs.as.spaces', FALSE),
tab.stops=getOption('fansi.tab.stops', 8L),
normalize=getOption('fansi.normalize', FALSE),
carry=getOption('fansi.carry', FALSE),
terminate=getOption('fansi.terminate', TRUE)
Expand Down
20 changes: 12 additions & 8 deletions R/strwrap.R
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@
strwrap_ctl <- function(
x, width = 0.9 * getOption("width"), indent = 0,
exdent = 0, prefix = "", simplify = TRUE, initial = prefix,
warn=getOption('fansi.warn'), term.cap=getOption('fansi.term.cap'),
warn=getOption('fansi.warn', TRUE),
term.cap=getOption('fansi.term.cap', dflt_term_cap()),
ctl='all', normalize=getOption('fansi.normalize', FALSE),
carry=getOption('fansi.carry', FALSE),
terminate=getOption('fansi.terminate', TRUE)
Expand Down Expand Up @@ -139,9 +140,10 @@ strwrap2_ctl <- function(
exdent = 0, prefix = "", simplify = TRUE, initial = prefix,
wrap.always=FALSE, pad.end="",
strip.spaces=!tabs.as.spaces,
tabs.as.spaces=getOption('fansi.tabs.as.spaces'),
tab.stops=getOption('fansi.tab.stops'),
warn=getOption('fansi.warn'), term.cap=getOption('fansi.term.cap'),
tabs.as.spaces=getOption('fansi.tabs.as.spaces', FALSE),
tab.stops=getOption('fansi.tab.stops', 8L),
warn=getOption('fansi.warn', TRUE),
term.cap=getOption('fansi.term.cap', dflt_term_cap()),
ctl='all', normalize=getOption('fansi.normalize', FALSE),
carry=getOption('fansi.carry', FALSE),
terminate=getOption('fansi.terminate', TRUE)
Expand Down Expand Up @@ -197,7 +199,8 @@ strwrap2_ctl <- function(
strwrap_sgr <- function(
x, width = 0.9 * getOption("width"), indent = 0,
exdent = 0, prefix = "", simplify = TRUE, initial = prefix,
warn=getOption('fansi.warn'), term.cap=getOption('fansi.term.cap'),
warn=getOption('fansi.warn', TRUE),
term.cap=getOption('fansi.term.cap', dflt_term_cap()),
normalize=getOption('fansi.normalize', FALSE),
carry=getOption('fansi.carry', FALSE),
terminate=getOption('fansi.terminate', TRUE)
Expand All @@ -216,9 +219,10 @@ strwrap2_sgr <- function(
exdent = 0, prefix = "", simplify = TRUE, initial = prefix,
wrap.always=FALSE, pad.end="",
strip.spaces=!tabs.as.spaces,
tabs.as.spaces=getOption('fansi.tabs.as.spaces'),
tab.stops=getOption('fansi.tab.stops'),
warn=getOption('fansi.warn'), term.cap=getOption('fansi.term.cap'),
tabs.as.spaces=getOption('fansi.tabs.as.spaces', FALSE),
tab.stops=getOption('fansi.tab.stops', 8L),
warn=getOption('fansi.warn', TRUE),
term.cap=getOption('fansi.term.cap', dflt_term_cap()),
normalize=getOption('fansi.normalize', FALSE),
carry=getOption('fansi.carry', FALSE),
terminate=getOption('fansi.terminate', TRUE)
Expand Down
Loading

0 comments on commit 3f85ece

Please sign in to comment.