Skip to content

Commit

Permalink
Pulled all the cli stuff out into a single function to make using cli…
Browse files Browse the repository at this point in the history
… messaging outside of formods easier.
  • Loading branch information
john-harrold committed Nov 26, 2023
1 parent ca4214b commit 619f47e
Show file tree
Hide file tree
Showing 40 changed files with 391 additions and 165 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export(FM_fetch_user_files_path)
export(FM_generate_report)
export(FM_init_state)
export(FM_le)
export(FM_message)
export(FM_mk_error_fig)
export(FM_notify)
export(FM_pause_screen)
Expand Down
117 changes: 82 additions & 35 deletions R/formods.R
Original file line number Diff line number Diff line change
Expand Up @@ -600,34 +600,37 @@ FM_le = function(state, entry, escape_braces=TRUE, entry_type="alert"){
# Writing messages to the console
if(state[["yaml"]][["FM"]][["logging"]][["console"]]){
for(line in entry){
FM_message(line=line,
escape_braces=escape_braces,
entry_type=entry_type)
# This will conditionally show the entry if the cli packages is present:
if(system.file(package="cli") != ""){
if(escape_braces){
if(entry_type=="alert"){
cli::cli_alert("{line}") }
if(entry_type=="danger"){
cli::cli_alert_danger("{line}") }
if(entry_type=="warning"){
cli::cli_alert_warning("{line}") }
if(entry_type=="info"){
cli::cli_alert_info("{line}") }
if(entry_type=="success"){
cli::cli_alert_success("{line}") }
} else {
if(entry_type=="alert"){
cli::cli_alert(line)}
if(entry_type=="danger"){
cli::cli_alert_danger(line)}
if(entry_type=="warning"){
cli::cli_alert_warning(line)}
if(entry_type=="info"){
cli::cli_alert_info(line)}
if(entry_type=="success"){
cli::cli_alert_success(line)}
}
} else {
message(line)
}
#if(system.file(package="cli") != ""){
# if(escape_braces){
# if(entry_type=="alert"){
# cli::cli_alert("{line}") }
# if(entry_type=="danger"){
# cli::cli_alert_danger("{line}") }
# if(entry_type=="warning"){
# cli::cli_alert_warning("{line}") }
# if(entry_type=="info"){
# cli::cli_alert_info("{line}") }
# if(entry_type=="success"){
# cli::cli_alert_success("{line}") }
# } else {
# if(entry_type=="alert"){
# cli::cli_alert(line)}
# if(entry_type=="danger"){
# cli::cli_alert_danger(line)}
# if(entry_type=="warning"){
# cli::cli_alert_warning(line)}
# if(entry_type=="info"){
# cli::cli_alert_info(line)}
# if(entry_type=="success"){
# cli::cli_alert_success(line)}
# }
#} else {
# message(line)
#}
}
}

Expand All @@ -644,7 +647,49 @@ FM_le = function(state, entry, escape_braces=TRUE, entry_type="alert"){

isgood}


#'@export
#'@title Show Message to User
#'@description Writes a message to the console depending on whether cli is
#'installed or not.
#'@param line Text to display
#'@param escape_braces Set to \code{TRUE} (default) to escape curly braces in the entry, set to \code{FALSE} to have the values interpreted.
#'@param entry_type Set to either "alert"(default), "danger", "info", "success", or "warning"
#'@return Returns NULL
#'@examples
#' mr = FM_message("This is a normal message")
#' mr = FM_message("This is a danger message", entry_type="danger")
#' mr = FM_message("This is a info message", entry_type="info")
#' mr = FM_message("This is a success message", entry_type="success")
#' mr = FM_message("This is a warning message", entry_type="warning")
FM_message = function(line, escape_braces=TRUE, entry_type="alert"){
if(system.file(package="cli") != ""){
if(escape_braces){
if(entry_type=="alert"){
cli::cli_alert("{line}") }
if(entry_type=="danger"){
cli::cli_alert_danger("{line}") }
if(entry_type=="warning"){
cli::cli_alert_warning("{line}") }
if(entry_type=="info"){
cli::cli_alert_info("{line}") }
if(entry_type=="success"){
cli::cli_alert_success("{line}") }
} else {
if(entry_type=="alert"){
cli::cli_alert(line)}
if(entry_type=="danger"){
cli::cli_alert_danger(line)}
if(entry_type=="warning"){
cli::cli_alert_warning(line)}
if(entry_type=="info"){
cli::cli_alert_info(line)}
if(entry_type=="success"){
cli::cli_alert_success(line)}
}
} else {
message(line)
}
NULL}

#'@export
#'@title Run Try/Catch and Process Results
Expand Down Expand Up @@ -864,13 +909,15 @@ FM_set_app_state <- function(session, app_state, set_holds = TRUE){
}
}
} else {
if(system.file(package="cli") != ""){
cli::cli_alert("FM_set_app_state()")
cli::cli_alert("Unable to find ASM state.")
} else {
message("FM_set_app_state()")
message("Unable to find ASM state.")
}
FM_message(line="FM_set_app_state()")
FM_message(line="Unable to find ASM state.")
#if(system.file(package="cli") != ""){
# cli::cli_alert("FM_set_app_state()")
# cli::cli_alert("Unable to find ASM state.")
#} else {
# message("FM_set_app_state()")
# message("Unable to find ASM state.")
#}
}


Expand Down
Loading

0 comments on commit 619f47e

Please sign in to comment.