Skip to content

Commit e84718a

Browse files
authored
Add some basic print methods (#140)
1 parent bbc13af commit e84718a

File tree

6 files changed

+107
-1
lines changed

6 files changed

+107
-1
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# chromote (development version)
22

3+
* `Chromote` and `ChromoteSession` gain print methods to give you a snapshot of the most important values.
4+
35
* `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.
46

57
* 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).

R/chromote.R

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,35 @@ Chromote <- R6Class(
382382
invisible()
383383
},
384384

385+
#' @description Summarise the current state of the object.
386+
#' @param verbose The print method defaults to a brief summary
387+
#' of the most important debugging info; use `verbose = TRUE` tp
388+
#' see the complex R6 object.
389+
#' @param ... Passed on to `format()` when `verbose` = TRUE
390+
print = function(..., verbose = FALSE) {
391+
if (verbose) {
392+
cat(format(self, ...), sep = "\n")
393+
} else {
394+
if (self$is_active()) {
395+
state <- "active + alive"
396+
} else if (self$is_alive()) {
397+
state <- "alive"
398+
} else {
399+
state <- "closed"
400+
}
401+
402+
ps <- self$get_browser()$get_process()
403+
404+
cat_line("<Chromote> (", state, ")")
405+
if (self$is_alive()) {
406+
cat_line(" URL: ", self$url())
407+
cat_line(" PID: ", ps$get_pid())
408+
cat_line(" Path: ", ps$get_cmdline()[[1]])
409+
}
410+
}
411+
invisible(self)
412+
},
413+
385414
#' @field default_timeout Default timeout in seconds for \pkg{chromote} to
386415
#' wait for a Chrome DevTools Protocol response.
387416
default_timeout = 10,

R/chromote_session.R

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ ChromoteSession <- R6Class(
544544
#' active debugging session?
545545
mark_closed = function(target_closed) {
546546
private$session_is_active <- FALSE
547-
private$target_is_active <- target_closed
547+
private$target_is_active <- !target_closed
548548
},
549549

550550
#' @description Retrieve active status
@@ -577,6 +577,33 @@ ChromoteSession <- R6Class(
577577
private$init_promise_
578578
},
579579

580+
#' @description Summarise the current state of the object.
581+
#' @param verbose The print method defaults to a brief summary
582+
#' of the most important debugging info; use `verbose = TRUE` tp
583+
#' see the complex R6 object.
584+
#' @param ... Passed on to `format()` when `verbose` = TRUE
585+
print = function(..., verbose = FALSE) {
586+
if (verbose) {
587+
cat(format(self, ...), sep = "\n")
588+
} else {
589+
if (self$is_active()) {
590+
state <- "session + target active"
591+
} else if (private$target_is_active) {
592+
state <- "target active"
593+
} else {
594+
state <- "closed"
595+
}
596+
597+
cat_line("<ChromoteSession> (", state, ")")
598+
if (self$is_active())
599+
cat_line(" Session ID: ", self$get_session_id())
600+
if (private$target_is_active)
601+
cat_line(" Target ID: ", self$get_target_id())
602+
cat_line(" Parent PID: ", self$parent$get_browser()$get_process()$get_pid())
603+
}
604+
invisible(self)
605+
},
606+
580607
#' @field parent [`Chromote`] object
581608
parent = NULL,
582609
#' @field default_timeout Default timeout in seconds for \pkg{chromote} to

R/utils.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
cat_line <- function(...) {
2+
cat(paste0(..., "\n", collapse = ""))
3+
}
4+
15
# =============================================================================
26
# System
37
# =============================================================================

man/Chromote.Rd

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/ChromoteSession.Rd

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)