1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
|
# Copyright (C) 2021 Brodie Gaslam
#
# This file is part of "diffobj - Diffs for R Objects"
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# Go to <https://www.r-project.org/Licenses/GPL-2> for a copy of the license.
is.less_flags <-
function(x) is.chr.1L(x) && isTRUE(grepl("^[[:alpha:]]*$", x))
# for checking the limits, if successful returns an integer(2L) vector,
# otherwise a character vector to sprintf as an error
#' @include pager.R
check_limit <- function(limit) {
if(
!is.numeric(limit) || any(is.na(limit)) ||
!length(limit) %in% 1:2 ||
!all(is.finite(limit)) ||
any(round(limit) != limit) ||
(length(limit) == 2L && diff(limit) > 0)
) {
return(
paste0(
"Argument `%s` must be an integer vector of length 1 or 2 ",
"and if length 2, with the first value larger than or equal to ",
"the second%s"
) ) }
limit <- as.integer(limit)
if(length(limit) == 1L) limit <- rep(limit, 2L)
limit
}
# requires a value to be a scalar character and match one of the provided
# options
string_in <- function(x, valid.x) is.chr.1L(x) && x %in% valid.x
# Simple validation functions
is.int.1L <- function(x)
is.numeric(x) && length(x) == 1L && !is.na(x) && all(x == round(x)) &&
is.finite(x)
is.int.2L <- function(x)
is.numeric(x) && length(x) == 2L && !anyNA(x) && all(x == round(x)) &&
all(is.finite(x))
is.TF <- function(x) isTRUE(x) || identical(x, FALSE)
is.chr.1L <- function(x) is.character(x) && length(x) == 1L && !is.na(x)
is.valid.palette.param <- function(x, param, palette) {
stopifnot(is(palette, "PaletteOfStyles"))
stopifnot(isTRUE(param %in% c("brightness", "color.mode")))
valid.formats <- dimnames(palette@data)$format
valid.params <- dimnames(palette@data)[[param]]
if(!is.character(x) || anyNA(x))
paste0("Argument `", param, "` must be character and not contain NAs")
else if(!all(x %in% valid.params))
paste0(
"Argument `", param, "` may only contain values in `", dep(valid.params),
"`"
)
else if(
(length(x) > 1L && is.null(names(x))) ||
!all(names(x) %in% c("", valid.formats))
)
paste0(
"Argument `", param, "` must have names if it has length > 1, and those ",
"names must include at least an empty name `\"\"` as well as names only ",
"from `", dep(valid.formats), "`."
)
else if ((!is.null(names(x)) && !"" %in% names(x)))
paste0(
"Argument `", param, "` must include at least one empty name `\"\"` if ",
"it has names."
)
else TRUE
}
is.one.arg.fun <- function(x) {
if(!is.function(x)) {
"is not a function"
} else if(length(formals(x)) < 1L) {
"does not have at least one arguments"
} else if("..." %in% names(formals(x))[1]) {
"cannot have `...` as the first argument"
} else {
nm.forms <- vapply(formals(x), is.name, logical(1L))
forms.chr <- character(length(nm.forms))
forms.chr[nm.forms] <- as.character(formals(x)[nm.forms])
forms.names <- names(formals(x))
if(any(tail(!nzchar(forms.chr) & nm.forms & forms.names != "...", -1L)))
"cannot have any non-optional arguments other than first one" else TRUE
}
}
is.valid.guide.fun <- is.two.arg.fun <- function(x) {
if(!is.function(x)) {
"is not a function"
} else if(length(formals(x)) < 2L) {
"does not have at least two arguments"
} else if("..." %in% names(formals(x))[1:2]) {
"cannot have `...` as one of the first two arguments"
} else {
nm.forms <- vapply(formals(x), is.name, logical(1L))
forms.chr <- character(length(nm.forms))
forms.chr[nm.forms] <- as.character(formals(x)[nm.forms])
if(any(tail(!nzchar(forms.chr) & nm.forms, -2L)))
"cannot have any non-optional arguments other than first two" else TRUE
} }
is.valid.width <- function(x)
if(!is.int.1L(x) || (x != 0L && (x < 10L || x > 10000))) {
"must be integer(1L) and 0, or between 10 and 10000"
} else TRUE
is.one.file.name <- function(x) {
if(!is.chr.1L(x)) {
"must be character(1L) and not NA"
} else if(!file_test("-f", x)) {
sprintf("(\"%s\") is not a file", x)
} else TRUE
}
is.non.obj.style <- function(x)
string_in(x, "auto") || (is.list(x) && !is.object(x))
# Things that could possibly be output by substitute
is.possibly.substituted <- function(x)
(is.atomic(x) && length(x) == 1L) || is.null(x) || is.name(x) || is.call(x)
# Checks common arguments across functions
check_args <- function(
call, tar.exp, cur.exp, mode, context, line.limit, format, brightness,
color.mode, pager, ignore.white.space, max.diffs, align, disp.width,
hunk.limit, convert.hz.white.space, tab.stops, style, palette.of.styles,
frame, tar.banner, cur.banner, guides, rds, trim, word.diff, unwrap.atomic,
extra, interactive, term.colors, strip.sgr, sgr.supported, call.match
) {
err <- make_err_fun(call)
warn <- make_warn_fun(call)
# Check for conflicting arguments
formals <- tail(names(call.match), -1L)
style.overrideable <- c("format", "brightness", "color.mode")
if(
"style" %in% formals && !is.non.obj.style(style) &&
any(s.ov <- style.overrideable %in% formals)
)
warn(
"Provided `style` argument will override the provided ",
if(sum(s.ov) < 2L) {
sprintf("`%s` argument", style.overrideable[s.ov])
} else {
paste0(
paste0(
sprintf("`%s`", head(style.overrideable[s.ov], -1L)),
collapse=", "
),
" and `", tail(style.overrideable[s.ov], 1L), "` arguments."
) } )
# Check extra
if(!is.list(extra)) err("Argument `extra` must be a list.")
# Check context
msg.base <- paste0(
"Argument `%s` must be integer(1L) and not NA, an object produced ",
"by `auto_context`, or \"auto\"."
)
if(
!is.int.1L(context) && !is(context,"AutoContext") &&
!identical(context, "auto")
)
err(sprintf(msg.base, "context"))
if(!is(context, "AutoContext")) {
context <- if(identical(context, "auto")) auto_context() else {
if(is.int.1L(context) && context < 0) {
min.cont <- 0
max.cont <- -1
} else if (is.int.1L(context)) {
min.cont <- max.cont <- as.integer(context)
} else {
err("Argument `context` must be integer(1L) and not NA.")
}
cont <- try(auto_context(min.cont, max.cont))
if(inherits(cont, "try-error"))
# nocov start
# should not be possible to get here given prior checks
err(
"Unable to instantiate an `AutoContext` object from provided ",
"`context` argument. Value should be integer(1L) and not NA, or ",
"an `AutoContext` object as generated by `auto_context()`."
)
# nocov end
cont
}
}
# any 'substr' of them otherwise these checks fail
val.modes <- c("auto", "unified", "context", "sidebyside")
fail.mode <- FALSE
if(!is.character(mode) || length(mode) != 1L || is.na(mode) || !nzchar(mode))
fail.mode <- TRUE
if(!fail.mode && !any(mode.eq <- substr(val.modes, 1, nchar(mode)) == mode))
fail.mode <- TRUE
if(fail.mode)
err(
"Argument `mode` must be character(1L) and in `", deparse(val.modes), "`."
)
# Tab stops
tab.stops <- as.integer(tab.stops)
if(
!is.integer(tab.stops) || !length(tab.stops) >= 1L || anyNA(tab.stops) ||
!all(tab.stops > 0L)
)
err(
"Argument `tab.stops` must be integer containing at least one value and ",
"with all values strictly positive"
)
# Limit vars
hunk.limit <- check_limit(hunk.limit)
if(!is.integer(hunk.limit)) err(sprintf(hunk.limit, "hunk.limit", "."))
if(!is.integer(line.limit <- check_limit(line.limit)))
err(
sprintf(
line.limit, "line.limit",
", or \"auto\" or the result of calling `auto_line_limit`"
) )
# guides
if(!is.TF(guides) && !is.function(guides))
err("Argument `guides` must be TRUE, FALSE, or a function")
if(is.function(guides) && !isTRUE(g.f.err <- is.two.arg.fun(guides)))
err("Argument `guides` ", g.f.err)
if(!is.function(guides) && !guides)
guides <- function(obj, obj.as.chr) integer(0L)
if(!is.TF(trim) && !is.function(trim))
err("Argument `trim` must be TRUE, FALSE, or a function")
if(is.function(trim) && !isTRUE(t.f.err <- is.two.arg.fun(trim)))
err("Argument `trim` ", t.f.err)
if(!is.function(trim) && !trim) trim <- trim_identity
# check T F args
if(is.null(interactive)) interactive <- interactive()
TF.vars <- c(
"ignore.white.space", "convert.hz.white.space", "rds", "word.diff",
"unwrap.atomic", "interactive"
)
msg.base <- "Argument `%s` must be TRUE or FALSE."
for(x in TF.vars) if(!is.TF(get(x, inherits=FALSE))) err(sprintf(msg.base, x))
# int 1L vars
if(is.null(term.colors)) term.colors <- crayon::num_colors()
msg.base <- "Argument `%s` must be integer(1L) and not NA."
int.1L.vars <- c("max.diffs", "term.colors")
for(x in int.1L.vars) {
if(!is.int.1L(int.val <- get(x, inherits=FALSE)))
err(sprintf(msg.base, "max.diffs"))
assign(x, as.integer(int.val))
}
# Banners; convolution here is to accomodate `diffObj` and have it be able
# to pass captured target/current expressions
chr1LorNULLorLanguage.vars <- c("tar.banner", "cur.banner")
msg.base <-
"Argument `%s` must be atomic and length(1L), NULL, a symbol, or a call"
for(x in chr1LorNULLorLanguage.vars ) {
y <- get(x, inherits=FALSE)
if(!is.possibly.substituted(y)) err(sprintf(msg.base, x))
}
if(!is.chr.1L(tar.banner) && !is.null(tar.banner)) {
tar.exp <- tar.banner
tar.banner <- NULL
}
if(!is.chr.1L(cur.banner) && !is.null(cur.banner)) {
cur.exp <- cur.banner
cur.banner <- NULL
}
# Align threshold
if(!is(align, "AlignThreshold")) {
align <- if(
is.numeric(align) && length(align) == 1L &&
!is.na(align) && align %bw% c(0, 1)
) {
AlignThreshold(threshold=align)
} else if(is.null(align)) {
AlignThreshold()
} else err(
"Argument `align` must be an \"AlignThreshold\" object or numeric(1L) ",
"and between 0 and 1."
)
}
# style
valid_object(style, "style", err)
if(
!is(style, "Style") && !string_in(style, "auto") &&
!(is.list(style) && !is.object(style))
)
err("Argument `style` must be \"auto\", a `Style` object, or a list.")
# pager; 'on' just means use pager already associated with style
valid_object(pager, "pager", err)
valid.pagers <- c("auto", "off", "on")
if(
!is(pager, "Pager") && !string_in(pager, valid.pagers) &&
!(is.list(pager) && !is.object(pager))
)
err(
"Argument `pager` must be one of `", dep(valid.pagers),
"`, a `Pager` object, or a list."
)
pager.args <- list()
if(!is(pager, "Pager")) {
if(string_in(pager, "off")) {
pager <- PagerOff()
} else if (is.list(pager)) {
pager.args <- pager
pager <- "on"
}
}
# palette and arguments that reference palette dimensions
if(is.null(palette.of.styles)) palette.of.styles <- PaletteOfStyles()
if(!is(palette.of.styles, "PaletteOfStyles"))
err("Argument `palette.of.styles` must be a `PaletteOfStyles` object.")
palette.params <- c("brightness", "color.mode")
for(x in palette.params)
if(
!isTRUE(
msg <- is.valid.palette.param(
get(x, inherits=FALSE), x, palette.of.styles
) )
) err(msg)
# Figure out whether pager is allowable or not; note that "auto" pager just
# means let the pager that comes built into the style be the pager
if(is.character(pager))
pager <- if(
(pager == "auto" && interactive) || pager == "on"
) {
"on"
} else PagerOff()
# format; decide what format to use
if(
!is(style, "Style") &&
(
string_in(style, "auto") || (is.list(style) && !is.object(style))
)
) {
if(is.list(style)) {
style.args <- style
style <- "auto"
} else style.args <- list()
# We only want to allow ansi styles if the pager supports them too;
# unfortuantely we cannot have different styles depending on whether the
# output is paged or not, at least not at this time
pager.could.be.ansi <- if(is(pager, "Pager")) pager@ansi else FALSE
if(!is.chr.1L(format))
err("Argument `format` must be character(1L) and not NA")
valid.formats <- c("auto", dimnames(palette.of.styles@data)$format)
if(!format %in% valid.formats)
err("Argument `format` must be one of `", dep(valid.formats) , "`.")
if(format == "auto") {
if(!is.int.1L(term.colors))
# nocov start
err(
"Logic Error: unexpected return from `crayon::num_colors()`; ",
"contact maintainer."
)
# nocov end
# No recognized color alternatives, try to use HTML if we can
format <- if(
nzchar(Sys.getenv('RSTUDIO')) && !nzchar(Sys.getenv('RSTUDIO_TERM')) &&
interactive
) {
"html"
} else if(
term.colors < 8
) {
if(!pager.could.be.ansi) {
if(
(interactive && identical(pager, "on")) || is(pager, "PagerBrowser")
) "html" else "raw"
} else {
if(!pager@threshold) "ansi8" else "raw"
}
} else if (term.colors < 256) {
"ansi8"
} else if (term.colors >= 256) {
"ansi256"
} else stop("Logic error: unhandled format; contact maintainer.") # nocov
}
style <- palette.of.styles[[
format, get_pal_par(format, brightness), get_pal_par(format, color.mode)
]]
if(is(style, "classRepresentation")) {
style <- try(do.call("new", c(list(style), style.args)), silent=TRUE)
if(inherits(style, "try-error")) {
msg <- conditionMessage(attr(style, "condition"))
err("Unable to instantiate `Style` object: ", msg)
}
} else {
if(length(style.args)) {
warn(
"Extra `style` arguments cannot be applied because selected object ",
"`palette.of.styles` is a `Style` instance rather than a `Style` ",
"\"classRepresentation\". See documentation for the `style` ",
"parameter for details."
) }
valid_object(
style, "palette.of.styles", err,
paste0(
"Argument `%s` is an invalid `%s` because it contains and invalid ",
"`Style` object:"
) ) }
} else if(!is(style, "Style"))
stop("Logic Error: unexpected style state; contact maintainer.") # nocov
# Attach specific pager if it was requested generated; if "on" just let the
# existing pager on the style be, which is done by not modifying @pager
if(is(pager, "Pager")) {
style@pager <- pager
} else if (length(pager.args)) {
## this is a bit gnarly, and
pager.s <- style@pager
old.slots <-
sapply(slotNames(pager.s), slot, object=pager.s, simplify=FALSE)
pager.args <-
c(pager.args, old.slots[setdiff(names(old.slots), names(pager.args))])
style@pager <- do.call("new", c(list(class(pager.s)), pager.args))
} else if(!identical(pager, "on"))
stop("Logic Error: Unexpected pager state; contact maintainer.") # nocov
# Check display width
if(!isTRUE(d.w.err <- is.valid.width(disp.width)))
err("Arugment `disp.width` ", d.w.err)
disp.width <- as.integer(disp.width)
if(disp.width) {
style@disp.width <- disp.width
} else if(!style@disp.width) {
d.w <- getOption("width")
if(!is.valid.width(d.w)) {
# nocov start this should never happen
warn("`getOption(\"width\") returned an invalid width, using 80L")
d.w <- 80L
# nocov end
}
style@disp.width <- d.w
}
disp.width <- style@disp.width
# check strip.sgr
if(!is.TF(strip.sgr) && !is.null(strip.sgr))
err("Argument `strip.sgr` must be TRUE, FALSE, or NULL")
if(is.null(strip.sgr)) strip.sgr <- is(style, "Ansi")
# check strip.sgr
if(!is.TF(sgr.supported) && !is.null(sgr.supported))
err("Argument `sgr.supported` must be TRUE, FALSE, or NULL")
if(is.null(sgr.supported))
sgr.supported <- is(style, "Ansi") || crayon::has_color()
# instantiate settings object
etc <- new(
"Settings", mode=val.modes[[which(mode.eq)]], context=context,
line.limit=line.limit, ignore.white.space=ignore.white.space,
max.diffs=max.diffs, align=align, disp.width=disp.width,
hunk.limit=hunk.limit, convert.hz.white.space=convert.hz.white.space,
tab.stops=tab.stops, style=style, frame=frame,
tar.exp=tar.exp, cur.exp=cur.exp, guides=guides, tar.banner=tar.banner,
cur.banner=cur.banner, trim=trim, word.diff=word.diff,
unwrap.atomic=unwrap.atomic, strip.sgr=strip.sgr,
sgr.supported=sgr.supported, err=err, warn=warn
)
etc
}
|