R/utils.R

Defines functions cat_tw

Documented in cat_tw

#' Typewriter-style Concatenate and Print
#'
#' Input text-string and output one character at a time given a delay. Aims to
#' mimic the Pokemon Blue progressive text reveal, like typewriter output.
#'
#' @param x A character string of length 1.
#' @param sleep Numeric, length 1. System delay in seconds.
#'
#' @return Output to console.
#'
#' @examples \dontrun{ cat_typewriter("Example text.", 0.2) }
cat_tw <- function(x, sleep = 0.02) {

  x_paste <- paste0(x, collapse = "")

  x_chars <- strsplit(x_paste, "\\b")[[1]]

  for (char in x_chars) {
    cat(char)
    Sys.sleep(sleep)
  }

}
matt-dray/safar6 documentation built on Feb. 6, 2021, 1:03 p.m.