R/program_complete.R

Defines functions program_complete

program_complete <- function(
  msg          = 'Program complete!',
  emoji_string = "thumbsup",
  start_time   = NULL,
  end_time     = NULL,
  notify       = FALSE,
  notification = list(
    title         = '',
    subtitle      = NA,
    message       = '',
    app_icon      = NA,
    content_image = NA,
    command       = NA,
    open_iterm    = FALSE
  )
  ) {

  # Print to STDOUT indicating program was completed. Optionally include the
  # elapsed program time. Optionally send a macOS notification or a notification
  # email indicating program completion.
  #
  # Keyword Arguments:
  #   msg {char} -- custom message to print instead of default (default: {'Program complete!'})
  #   emoji_string {char} -- name of emoji to print if any (default: {'thumbsup'})
  #   start_time {POSIXt} -- start time of program, output of Sys.time() (default: {NULL})
  #   end_time {POSIXt} -- end time of program, output of Sys.time() (default: {NULL})
  #   notify    {logical} -- if TRUE, send notification to macOS using macos_notify()
  #   notification {list} -- list containing notification properties, elements:
  #     title         {char} -- title of notification
  #     subtitle      {char} -- subtitle of notification
  #     app_icon      {char} -- path to icon image to display in notification
  #     content_image {char} -- path to content image to display in notification
  #     command       {char} -- shell command to execute upon clicking on notification
  #     open_iterm {logical} -- if TRUE, open "iTerm.app" when notification is clicked
  #
  # Returns:
  #   nothing

  # Save original message before any editing is done if the message will be used in a
  # macOS notification
  if (notify) { msg_raw = msg }

  # Color text green
  msg = crayon::green(msg)
  
  # Add emoji if specified
  if (!is.null(emoji_string)) {
    
    # Add colons surrounding emoji string if not already present
    emoji_res = search_emoji(emoji_string)
    if (length(emoji_res)) {
      emoji_res = emoji_res[emoji_res == emoji_string]
      emoji = emoji(emoji_res)
      msg = paste(msg, emoji)
    } else {
      rdoni::echo("Emoji '%s' not found!", emoji_string, warn = TRUE)
    }
  }
        
  # Get elapsed time if specified
  if (!is.null(start_time) && !is.null(end_time)) {
    diff = end_time - start_time
    msg = paste0(
      msg,
      sprintf(" Elapsed time: %s %s",
        crayon::yellow(as.numeric(diff)),
        crayon::yellow(units(diff))
      )
    )

    # Add to msg_raw to include in notification
    if (notify) {
      msg_raw = crayon::strip_style(msg)
    }
  }

  # Print message and notify if specified
  if (notify) {
    notification$message = msg_raw
    rdoni::echo(msg, notify=True, notification=notification)
  } else {
    rdoni::echo(msg)
  }
}
tsouchlarakis/rdoni documentation built on Sept. 16, 2019, 8:53 p.m.