R/debug.R

Defines functions show_line debug get_debug_level

debug_levels <- c(
  fatal = 0,
  error = 1,
  warn = 2,
  info = 3,
  debug = 4,
  trace = 5
)

get_debug_level <- function() {
  lvl <- tolower(Sys.getenv("ASCIICAST_DEBUG_LEVEL", "fatal"))
  if (! lvl %in% names(debug_levels)) {
    stop(cli::format_error(c(
      "Invalid asciicast debug level in {.env ASCIICAST_DEBUG_LEVEL env
       var: {.code lvl}.",
      i = "It must be one of {.code {names(debug_levels)}}."
    )))
  }
}

debug <- function(level, ..., .envir = parent.frame()) {
  current <- get_debug_level()
  if (debug_levels[current] >= debug_levels[level]) {
    cli::cli_alert(..., .envir = .envir)
  }
}

show_line <- function(line) {
  pl <- parse_line(line)
  if (pl$type == "o") {
    cat(pl$value)
  }
}

Try the asciicast package in your browser

Any scripts or data that you put into this service are public.

asciicast documentation built on Dec. 5, 2022, 5:22 p.m.