Skip to content

Commit

Permalink
Merge pull request #28 from lewinfox/bug/curlies
Browse files Browse the repository at this point in the history
Fixes #26
  • Loading branch information
lewinfox authored Feb 17, 2024
2 parents f4c8d37 + a3a32de commit 16b3b3c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
19 changes: 10 additions & 9 deletions R/print.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,24 @@ ic_print <- function(loc, parent_ref, deparsed_expression = rlang::missing_arg()
if (!rlang::is_missing(deparsed_expression)) {
# We want to print a one-line summary for complex objects like lists and data frames.
str_res <- ic_peek(value)
expression_string <- glue::glue("{{.var {deparsed_expression}}}: {str_res}")
expression_string <- glue::glue("{deparsed_expression}: {str_res}")
}

# We need to check what options are set to decide what to print - whether to include the context
# or not.
#
# TODO: It's a bit messy having these multiple calls to `cli_alert_info`. These were introduced
# while fixing https://github.com/lewinfox/icecream/issues/26, when the multiple nested
# glue calls became too complex to unpick easily. It would be nice to construct a single
# perfectly-formatted string and then `cli_alert_info` that, but for now this will do.
prefix <- getOption("icecream.prefix", "ic|")
output <- if (!is.null(expression_string)) {
if (!is.null(expression_string)) {
if (getOption("icecream.always.include.context")) {
glue::glue("{context_string} | {expression_string}")
cli::cli_alert_info("{prefix} {.var {context_string}} | {expression_string}")
} else {
expression_string
cli::cli_alert_info("{prefix} {expression_string}")
}
} else {
context_string
cli::cli_alert_info("{prefix} {.var {context_string}}")
}
output <- paste(prefix, output)

cli::cli_alert_info(output) # TODO: This is where a custom print/display function would be used
invisible(output)
}
12 changes: 8 additions & 4 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -254,15 +254,19 @@ data(iris)
ic(iris) # we would like to see header of the data
options(icecream.peeking.function = head,
icecream.max.lines = 5)
options(
icecream.peeking.function = head,
icecream.max.lines = 5
)
ic(iris)
```

```{r, include=FALSE}
options(icecream.peeking.function = old.fun,
icecream.max.lines = old.lines)
options(
icecream.peeking.function = old.fun,
icecream.max.lines = old.lines
)
```

Note that if `icecream.max.lines` is greater than 1 and summary of an object is longer than 1, the
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test-icecream.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,12 @@ test_that("source file is correctly identified", {
source(temp, keep.source = TRUE)
expect_message(f(), regexp = "my_name_is_inigo_montoya")
})

test_that("invalid glue input is handled cleanly", {
expect_message(ic("{"), regexp = "\\{")
expect_message(ic("{}"), regexp = "\\{\\}")
})

test_that("JSON input is handled cleanly", {
expect_message(ic("{'a': 1}"), regexp = "\\{'a': 1\\}")
})

0 comments on commit 16b3b3c

Please sign in to comment.