Skip to content

Commit

Permalink
Use a less confusing name for the flush console helpers (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley authored Jun 26, 2024
1 parent 23aa1f5 commit 405a4ca
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
2 changes: 1 addition & 1 deletion R/eval.R
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ evaluate_top_level_expression <- function(exprs,
if (!is.null(source))
watcher$push(source)

local_output_handler(watcher$capture_output)
local_console_flusher(watcher$capture_output)
local_plot_hooks(watcher$capture_plot_and_output)

# Handlers for warnings, errors and messages
Expand Down
23 changes: 11 additions & 12 deletions R/flush-console.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,22 @@
#' either a direct `evaluate()` call or in \pkg{knitr} code chunks).
#' @export
flush_console = function() {
if (!is.null(the$output_handler)) {
the$output_handler()
if (!is.null(the$console_flusher)) {
the$console_flusher()
}
invisible()
}

the$console_flusher <- NULL

the$output_handler <- NULL
local_console_flusher <- function(flusher, frame = parent.frame()) {
old <- set_console_flusher(flusher)
defer(set_console_flusher(old), frame)
invisible()
}

set_output_handler <- function(handler) {
old <- the$output_handler
the$output_handler <- handler
set_console_flusher <- function(flusher) {
old <- the$console_flusher
the$console_flusher <- flusher
invisible(old)
}

local_output_handler <- function(handler, frame = parent.frame()) {
old <- set_output_handler(handler)
defer(set_output_handler(old), frame)
invisible()
}
6 changes: 3 additions & 3 deletions tests/testthat/test-flush-console.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ test_that("flush_console() is a null op by default", {

test_that("can set and restore output handler", {
f <- function() message("Hi")
old <- set_output_handler(function() message("Hi"))
expect_equal(the$output_handler, f)
old <- set_console_flusher(function() message("Hi"))
expect_equal(the$console_flusher, f)
expect_equal(old, NULL)

expect_message(flush_console(), "Hi")
old2 <- set_output_handler(old)
old2 <- set_console_flusher(old)
expect_equal(old2, f)
})

Expand Down

0 comments on commit 405a4ca

Please sign in to comment.