Skip to content

Commit

Permalink
Escape cli syntax in "try" mode
Browse files Browse the repository at this point in the history
  • Loading branch information
lionel- committed May 12, 2021
1 parent 052918c commit 2a8ad2b
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 38 deletions.
44 changes: 29 additions & 15 deletions R/cnd-message.R
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,12 @@ format_message <- function(x, env = caller_env()) {
}
}

if (use_cli_format(env)) {
out <- cli::format_message(x, env)
} else {
out <- format_bullets(x)
}
out <- switch(
use_cli_format(env),
partial = cli::format_message(cli_escape(x)),
full = cli::format_message(x, env),
format_bullets(x)
)

str_restore(out, orig)
}
Expand All @@ -272,30 +273,35 @@ format_error_message <- function(x, env = caller_env()) {
if (inherits(x, "AsIs")) {
return(x)
}
if (use_cli_format(env)) {
str_restore(cli::format_error(x, env), x)
} else {
switch(
use_cli_format(env),
partial = str_restore(cli::format_error(cli_escape(x)), x),
full = str_restore(cli::format_error(x, env), x),
format_message(x, env)
}
)
}
#' @rdname format_message
#' @export
format_warning_message <- function(x, env = caller_env()) {
if (inherits(x, "AsIs")) {
return(x)
}
if (use_cli_format(env)) {
str_restore(cli::format_warning(x, env), x)
} else {
switch(
use_cli_format(env),
partial = str_restore(cli::format_warning(cli_escape(x)), x),
full = str_restore(cli::format_warning(x, env), x),
format_message(x, env)
}
)
}

str_restore <- function(x, to) {
to <- to[1]
to[[1]] <- x
to
}
cli_escape <- function(x) {
gsub("\\}", "}}", gsub("\\{", "{{", x))
}

use_cli_format <- function(env) {
# Internal option to disable cli in case of recursive errors
Expand All @@ -322,7 +328,11 @@ use_cli_format <- function(env) {
)

if (is_string(flag, "try")) {
return(has_cli_format)
if (has_cli_format) {
return("partial")
} else {
return("fallback")
}
}

if (!is_bool(flag)) {
Expand All @@ -339,5 +349,9 @@ use_cli_format <- function(env) {
)
}

flag
if (flag) {
"full"
} else {
"fallback"
}
}
10 changes: 2 additions & 8 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -222,16 +222,10 @@ pad_spaces <- function(x, left = TRUE) {

# Import symbols from cli if available
has_cli <- FALSE
has_cli_bullet <- FALSE
has_cli_format <- FALSE
on_load({
has_cli <- is_installed("cli")
has_cli_format <- is_installed("cli", version = "2.4.0.9000")
# TODO: detect new-style cli bullet

if (has_cli_format) {
.Call(ffi_cli_format_is_here)
}
has_cli_format <- is_installed("cli", version = "2.5.0")
})

info <- function() {
Expand All @@ -251,7 +245,7 @@ bullet <- function() {

# Use small bullet if cli is too old.
# See https://github.com/r-lib/cli/issues/241
if (!has_cli_bullet && !is_string(x, "*")) {
if (!has_cli_format && !is_string(x, "*")) {
x <- "\u2022"
}

Expand Down
6 changes: 1 addition & 5 deletions src/internal/dots.c
Original file line number Diff line number Diff line change
Expand Up @@ -500,11 +500,7 @@ r_obj* dots_unquote(r_obj* dots, struct dots_capture_info* capture_info) {
break;
}
case DOTS_OP_value_curly:
if (has_cli_format) {
r_abort("Can't use `{{{{` in a non-quoting function");
} else {
r_abort("Can't use `{{` in a non-quoting function");
}
r_abort("Can't use `{{` in a non-quoting function");
case DOTS_OP_expr_uqn:
case DOTS_OP_quo_uqn:
case DOTS_OP_value_uqn:
Expand Down
1 change: 0 additions & 1 deletion src/internal/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ static const R_CallMethodDef r_callables[] = {
{"ffi_call_has_precedence", (DL_FUNC) &ffi_call_has_precedence, 3},
{"ffi_chr_get", (DL_FUNC) &ffi_chr_get, 2},
{"ffi_chr_has_curly", (DL_FUNC) &ffi_chr_has_curly, 1},
{"ffi_cli_format_is_here", (DL_FUNC) &ffi_cli_format_is_here, 0},
{"ffi_cnd_signal", (DL_FUNC) &ffi_cnd_signal, 1},
{"ffi_cnd_type", (DL_FUNC) &ffi_cnd_type, 1},
{"ffi_data_mask_clean", (DL_FUNC) &ffi_data_mask_clean, 1},
Expand Down
7 changes: 0 additions & 7 deletions src/internal/internal.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
#include <rlang.h>
#include "internal.h"

bool has_cli_format = false;

r_obj* ffi_cli_format_is_here() {
has_cli_format = true;
return r_null;
}

#include "arg.c"
#include "attr.c"
#include "call.c"
Expand Down
2 changes: 0 additions & 2 deletions src/internal/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ struct rlang_globals_syms {
r_obj* c_null;
};

extern bool has_cli;

extern r_obj* rlang_zap;

extern r_obj* rlang_as_list_call;
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-cnd-message.R
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,9 @@ test_that("cli is not used when message is escaped with `I()`", {
"{x}"
)
})

test_that("cli syntax is escaped in 'try' mode", {
.rlang_use_cli_format <- "try"
x <- "{foo {{}}"
expect_equal(format_message(x), x)
})

0 comments on commit 2a8ad2b

Please sign in to comment.