Skip to content

Commit

Permalink
+ bread crumbs
Browse files Browse the repository at this point in the history
  • Loading branch information
romainfrancois committed Feb 4, 2024
1 parent 119b9d3 commit 1db20d0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
49 changes: 32 additions & 17 deletions R/peek.R
Original file line number Diff line number Diff line change
@@ -1,37 +1,52 @@
#' @importFrom utils ls.str capture.output
informant <- function() {
hex_pink <- cli::make_ansi_style("pink")("\U2B22")

env <- parent.frame()
breadcrumbs <- function() {
calls <- sys.calls()
frames <- sys.frames()
snitched <- sapply(frames, function(f) exists(".__snitch", f, inherits = FALSE))

call <- deparse(calls[[length(calls) - 5L]])
crumbs <- sapply(calls[snitched], function(call) {
as.character(call[[1L]])
})

header <- paste(hex_pink, cli::col_silver("call:"), call)
content <- paste(hex_pink, cli::col_silver("env:"))
txt <- paste(" - ", capture.output(print(ls.str(env))))
# TODO: figure out why eval is in there in the first place
crumbs[crumbs != "eval"]
}

print(cli::boxx(
c(header, content, txt),
padding = 1, border_col = "pink"
))
colors <- rainbow(1000)

invisible()
#' @importFrom utils ls.str capture.output
informant <- function(col) {
function() {
assign(".__snitch", TRUE, parent.frame(n = 5))
colored <- cli::make_ansi_style(col)
hex <- "\U2B22"

env <- parent.frame()

crumbs <- paste(breadcrumbs(), collapse = paste0(" ", hex, " "))
writeLines(colored(cli::rule(crumbs)))
if (length(ls(env, all.names = FALSE))) {
txt <- paste(" ", capture.output(print(ls.str(env, all.names = FALSE))))
writeLines(colored(txt))
}

invisible()
}
}

#' Peek function inputs
#' Snitch on functions
#'
#' @param what Function to snitch on
#' @param col color
#'
#' @examples
#' fun(rnorm)
#'
#' @export
fun <- function(what) {
fun <- function(what, col = "pink") {
call <- sys.call()
call[[1L]] <- quote(trace)
call$where <- topenv(parent.frame())
call$tracer <- informant
call$tracer <- informant(col)
call$print <- FALSE
suppressMessages(eval.parent(call))

Expand Down
8 changes: 5 additions & 3 deletions man/fun.Rd

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

0 comments on commit 1db20d0

Please sign in to comment.