Skip to content

Commit

Permalink
Add some basic print methods (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley authored Feb 5, 2024
1 parent bbc13af commit e84718a
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# chromote (development version)

* `Chromote` and `ChromoteSession` gain print methods to give you a snapshot of the most important values.

* `Chromote` gains a new `is_alive()` method equivalent to the old `is_active()` method; i.e. it reports on if there is an active chrome process running in the background.

* Breaking change: `Chromote$is_active()` method now reports if there is an active connection to the underlying chrome instance, rather than whether or not that instance is alive (#94).
Expand Down
29 changes: 29 additions & 0 deletions R/chromote.R
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,35 @@ Chromote <- R6Class(
invisible()
},

#' @description Summarise the current state of the object.
#' @param verbose The print method defaults to a brief summary
#' of the most important debugging info; use `verbose = TRUE` tp
#' see the complex R6 object.
#' @param ... Passed on to `format()` when `verbose` = TRUE
print = function(..., verbose = FALSE) {
if (verbose) {
cat(format(self, ...), sep = "\n")
} else {
if (self$is_active()) {
state <- "active + alive"
} else if (self$is_alive()) {
state <- "alive"
} else {
state <- "closed"
}

ps <- self$get_browser()$get_process()

cat_line("<Chromote> (", state, ")")
if (self$is_alive()) {
cat_line(" URL: ", self$url())
cat_line(" PID: ", ps$get_pid())
cat_line(" Path: ", ps$get_cmdline()[[1]])
}
}
invisible(self)
},

#' @field default_timeout Default timeout in seconds for \pkg{chromote} to
#' wait for a Chrome DevTools Protocol response.
default_timeout = 10,
Expand Down
29 changes: 28 additions & 1 deletion R/chromote_session.R
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ ChromoteSession <- R6Class(
#' active debugging session?
mark_closed = function(target_closed) {
private$session_is_active <- FALSE
private$target_is_active <- target_closed
private$target_is_active <- !target_closed
},

#' @description Retrieve active status
Expand Down Expand Up @@ -577,6 +577,33 @@ ChromoteSession <- R6Class(
private$init_promise_
},

#' @description Summarise the current state of the object.
#' @param verbose The print method defaults to a brief summary
#' of the most important debugging info; use `verbose = TRUE` tp
#' see the complex R6 object.
#' @param ... Passed on to `format()` when `verbose` = TRUE
print = function(..., verbose = FALSE) {
if (verbose) {
cat(format(self, ...), sep = "\n")
} else {
if (self$is_active()) {
state <- "session + target active"
} else if (private$target_is_active) {
state <- "target active"
} else {
state <- "closed"
}

cat_line("<ChromoteSession> (", state, ")")
if (self$is_active())
cat_line(" Session ID: ", self$get_session_id())
if (private$target_is_active)
cat_line(" Target ID: ", self$get_target_id())
cat_line(" Parent PID: ", self$parent$get_browser()$get_process()$get_pid())
}
invisible(self)
},

#' @field parent [`Chromote`] object
parent = NULL,
#' @field default_timeout Default timeout in seconds for \pkg{chromote} to
Expand Down
4 changes: 4 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
cat_line <- function(...) {
cat(paste0(..., "\n", collapse = ""))
}

# =============================================================================
# System
# =============================================================================
Expand Down
22 changes: 22 additions & 0 deletions man/Chromote.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions man/ChromoteSession.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e84718a

Please sign in to comment.