R/development/printfuns.R

Defines functions printstamp printmsg printstamp printmsg

<<<<<<< HEAD:R/printfuns.R
#' Print Message or Stamp
#'
#' Print Stamp or Message with optional timestamp
#'
#' @param msg printmsg to display
#' @param sym A character symbol used to create the banner
#' @param slim boolean. Default TRUE to make banner compact
#' @param content An optional object to print below the banner
#' @param addtime boolean. Set TRUE to add time (default = FALSE)
#'
#' @importFrom stringr str_c str_split str_count str_length str_trunc
#'
#' @name print_funs
NULL


c("!", "@", "#", "$", "%")
letters[1:5]
rmarkdown::pandoc_template

rmarkdown::rmarkdown_format()



outformat <- rmarkdown::powerpoint_presentation(
  toc = TRUE,
  toc_depth = 2,
  df_print = "kable",slide_level = 2,
  keep_md = FALSE
)

rmarkdown::render(
  input = "R/Untitled.Rmd",
  output_format = outformat,
  output_file = "testdelete.ppt",
  output_dir = "~/",
  output_options = NULL,
  intermediates_dir = NULL,
  knit_root_dir = NULL,
  runtime = c("auto", "static", "shiny","shiny_prerendered"),
  clean = TRUE,
  params = NULL,
  knit_meta = NULL,
  envir = parent.frame(),
  run_pandoc = TRUE,
  quiet = FALSE,
  encoding = "UTF-8"
)



# toc = FALSE,
# toc_depth = 2,
# fig_width = 5,
# fig_height = 4,
# fig_caption = TRUE,
# df_print = "default",
# smart = TRUE,
# keep_md = FALSE,
# md_extensions = NULL,
# slide_level = NULL,
# reference_doc = "default",
# pandoc_args = NULL




rmarkdown::pandoc_toc_args(toc = TRUE, toc_depth = 1)

#' @describeIn print_funs TBD
#' @export
printstamp <- function(msg="hello\nworld", sym="#"){
  msglines <- stringr::str_split(msg, "\\n")[[1]]
  msgmain <- msglines[which.max(sapply(msglines, stringr::str_length, simplify = FALSE)[[1]])]

  # make message length an even number for centering purposes
  if(stringr::str_length(msgmain) %% 2 == 1)
    msgmain <- stringr::str_c(msgmain, " ")

  msg <- stringr::str_c(" ", msglines, " ")
  scount <- stringr::str_length(msgmain)
  cushion <- ceiling(scount*1.3) - scount

  cushion <- cushion + cushion %% 2
  topcount  <- scount + cushion - 1 + 2
  sidecount <- sum(length(msglines), 2)

  # hdft   <- stringr::str_c(rep(sym, topcount), collapse = "")
  spaces <- stringr::str_c(rep(" ", topcount - 1), collapse = "")
  sides.left   <- rep(paste0(sym, ">"), sidecount)
  sides.right  <- rep(sym, sidecount)


  grid_col <- topcount + 1
  grid_row <- sidecount + 2

  tmp <- stringr::str_c(stringr::str_c(sides.left, spaces), collapse = "\n")

  # tmp <- stringr::str_c(c(hdft, stringr::str_c(sides, spaces), hdft), sym, collapse = "\n")

  txt <- stringr::str_split(stringr::str_split(tmp, "\n")[[1]], "")

  pad.l <- c(paste0(sym, "> "), rep("", cushion/2-1))
  pad.r <- " "#c(rep(" ", cushion/2-1), sym)
  txt[2:(1+length(msglines))] <- lapply(stringr::str_split(msglines, ""), function(i) c(pad.l, i, pad.r))

  cat("\n\n")
  cat(paste0(sapply(txt, function(itxt) paste0(c(itxt, "\n"), collapse = "")), collapse = ""))
  cat("\n")
}


#' @describeIn print_funs TBD
#' @export
printmsg <- function(msg, sym="#", slim = TRUE, content=NULL, addtime=TRUE){
  ## can redo this code later: first construct the middle, then just repeat sym
  ## and cutoff at length of middle.. which can vary with slim.

  # Will be messing with console width so grab global setting to reset later
  globscipen <- options()$width
  on.exit(options(width = globscipen))

  ##
  ## Get parameters for placement
  ##
  # Calibrate position by requiring length to be closest even integer to true length
  numchars  <- ceiling(stringr::str_count(msg)/2)*2

  # border should by some factor of twice the length of the message for centering aesthetics
  lenAdj <- ifelse(slim, 1.25, 2)
  blen <- round(numchars*lenAdj)

  # construct topbottom first
  topbottom <- paste0(c("\n", rep(sym, blen), "\n"), collapse="")

  # construct middle
  ind <- paste0(rep(" ", ceiling((blen - numchars)/2-1)), collapse="")
  middle <- paste0(sym, ind, msg, ind, sym)

  # if middle is shorter (likely only by 1), then adjust one side's spacing
  # not sure when this would be negative, but too tired to think so will include 'max'
  adjby <- max(0, blen - stringr::str_length(middle))
  if(adjby > 0)
    middle <- paste0(sym, ind, msg, ind, " ", sym)

  # final message
  trunc_topbot <- stringr::str_trunc(topbottom, stringr::str_length(middle)+2, ellipsis = "\n")
  finalmsg <- paste0(trunc_topbot, middle, trunc_topbot, collapse="")

  # Display - temporarily set the console width then print
  options(width=stringr::str_count(topbottom))
  cat(crayon::bgWhite(crayon::bold(crayon::magenta(finalmsg))))

  # add time if applicable
  if(addtime){
    stamp <- paste0("\n", ind, "Timestamp: ", Sys.time(), "\n")
    cat(crayon::bgMagenta(crayon::white(stamp)))
  }

  # if content was provided, display that now
  if(!is.null(content)){
    if(class(content) %in% c("matrix", "data.frame")){
      print(content, print.gap = TRUE, quote = FALSE)
    }else{
      cat(paste0("\n", content,"\n"))
    }
  }else{
    cat("\n")
  }
}


=======
#' Print Message or Stamp
#'
#' Print Stamp or Message with optional timestamp
#'
#' @param msg printmsg to display
#' @param sym A character symbol used to create the banner
#' @param slim boolean. Default TRUE to make banner compact
#' @param content An optional object to print below the banner
#' @param addtime boolean. Set TRUE to add time (default = FALSE)
#'
#' @importFrom stringr str_c str_split str_count str_length str_trunc
#'
#' @name print_funs
NULL

title = "[T]"
author = "[A]"
tsCreated = "[%D!]"
tsModified = "[%M!]"
fileID = "[=FID!]"
projID = "[=PID!]"
section_symbols <- paste0("#~", c("!", "@", "#", "$", "%", "^", "&", "*"))
section_numbers <- 1:8
section_letters <- letters[1:8]
section_LETTERS <- LETTERS[1:8]


printstamp()


if(!dir.exists("~/.R/rstudio/snippets"))
  dir.create("~/.R/rstudio/snippets")

if()

tools::file_path_as_absolute("~/.R/rstudio/keybindings/editor_bindings.json")
tools::file_path_as_absolute("~/.R/rstudio/keybindings/addins.json")
tools::file_path_as_absolute("~/.R/rstudio/keybindings/rstudio_bindings.json")
tools::file_path_as_absolute("~/.R/rstudio/snippets")



#' @describeIn print_funs TBD
#' @export
printstamp <- function(msg="hello\nworld", sym="#"){
  msglines <- stringr::str_split(msg, "\\n")[[1]]
  msgmain <- msglines[which.max(sapply(msglines, stringr::str_length, simplify = FALSE)[[1]])]

  # make message length an even number for centering purposes
  if(stringr::str_length(msgmain) %% 2 == 1)
    msgmain <- stringr::str_c(msgmain, " ")

  msg <- stringr::str_c(" ", msglines, " ")
  scount <- stringr::str_length(msgmain)
  cushion <- ceiling(scount*1.3) - scount

  cushion <- cushion + cushion %% 2
  topcount  <- scount + cushion - 1 + 2
  sidecount <- sum(length(msglines), 2)

  # hdft   <- stringr::str_c(rep(sym, topcount), collapse = "")
  spaces <- stringr::str_c(rep(" ", topcount - 1), collapse = "")
  sides.left   <- rep(paste0(sym, ">"), sidecount)
  sides.right  <- rep(sym, sidecount)


  grid_col <- topcount + 1
  grid_row <- sidecount + 2

  tmp <- stringr::str_c(stringr::str_c(sides.left, spaces), collapse = "\n")

  # tmp <- stringr::str_c(c(hdft, stringr::str_c(sides, spaces), hdft), sym, collapse = "\n")

  txt <- stringr::str_split(stringr::str_split(tmp, "\n")[[1]], "")

  pad.l <- c(paste0(sym, "> "), rep("", cushion/2-1))
  pad.r <- " "#c(rep(" ", cushion/2-1), sym)
  txt[2:(1+length(msglines))] <- lapply(stringr::str_split(msglines, ""), function(i) c(pad.l, i, pad.r))

  cat("\n\n")
  cat(paste0(sapply(txt, function(itxt) paste0(c(itxt, "\n"), collapse = "")), collapse = ""))
  cat("\n")
}


#' @describeIn print_funs TBD
#' @export
printmsg <- function(msg, sym="#", slim = TRUE, content=NULL, addtime=TRUE){
  ## can redo this code later: first construct the middle, then just repeat sym
  ## and cutoff at length of middle.. which can vary with slim.

  # Will be messing with console width so grab global setting to reset later
  globscipen <- options()$width
  on.exit(options(width = globscipen))

  ##
  ## Get parameters for placement
  ##
  # Calibrate position by requiring length to be closest even integer to true length
  numchars  <- ceiling(stringr::str_count(msg)/2)*2

  # border should by some factor of twice the length of the message for centering aesthetics
  lenAdj <- ifelse(slim, 1.25, 2)
  blen <- round(numchars*lenAdj)

  # construct topbottom first
  topbottom <- paste0(c("\n", rep(sym, blen), "\n"), collapse="")

  # construct middle
  ind <- paste0(rep(" ", ceiling((blen - numchars)/2-1)), collapse="")
  middle <- paste0(sym, ind, msg, ind, sym)

  # if middle is shorter (likely only by 1), then adjust one side's spacing
  # not sure when this would be negative, but too tired to think so will include 'max'
  adjby <- max(0, blen - stringr::str_length(middle))
  if(adjby > 0)
    middle <- paste0(sym, ind, msg, ind, " ", sym)

  # final message
  trunc_topbot <- stringr::str_trunc(topbottom, stringr::str_length(middle)+2, ellipsis = "\n")
  finalmsg <- paste0(trunc_topbot, middle, trunc_topbot, collapse="")

  # Display - temporarily set the console width then print
  options(width=stringr::str_count(topbottom))
  cat(crayon::bgWhite(crayon::bold(crayon::magenta(finalmsg))))

  # add time if applicable
  if(addtime){
    stamp <- paste0("\n", ind, "Timestamp: ", Sys.time(), "\n")
    cat(crayon::bgMagenta(crayon::white(stamp)))
  }

  # if content was provided, display that now
  if(!is.null(content)){
    if(class(content) %in% c("matrix", "data.frame")){
      print(content, print.gap = TRUE, quote = FALSE)
    }else{
      cat(paste0("\n", content,"\n"))
    }
  }else{
    cat("\n")
  }
}


>>>>>>> 681c87557e94fcfc1a30a0185aca4c8d5737a0a8:R/development/printfuns.R
bfatemi/ninjar documentation built on Sept. 8, 2019, 7:37 p.m.