Skip to content

Commit

Permalink
ft: show starting/restarting msg on console
Browse files Browse the repository at this point in the history
  • Loading branch information
kennedymwavu committed Oct 24, 2024
1 parent 3b6884b commit d609787
Showing 1 changed file with 35 additions and 19 deletions.
54 changes: 35 additions & 19 deletions R/monitor.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,7 @@ monitor <- function(

patterns <- paste0(ext, "$", collapse = "|")


dashes <- function() {
example_line <- sprintf("%s Files changed. Restarting...", current_time())
strrep(x = "-", times = nchar(example_line))
}

message(
dashes(),
"\n",
sprintf("%s Starting rmon...\n", current_time())
)
dash_and_msg(type = "starting")

get_file_info <- function() {
files <- list.files(
Expand Down Expand Up @@ -120,7 +110,6 @@ monitor <- function(
file.info(files)$mtime
}


start_new_process <- function() {
processx::process$new(
command = "Rscript",
Expand All @@ -139,14 +128,8 @@ monitor <- function(
changed <- !identical(file_info, new_file_info)
if (changed) {
file_info <- new_file_info

message(
dashes(),
"\n",
sprintf("%s Files changed. Restarting...\n", current_time())
)
dash_and_msg()
p$kill()

p <- start_new_process()
}

Expand All @@ -159,10 +142,43 @@ monitor <- function(
#' @details Retrieves current system date and time, formatted in
#' a human-readable way.
#' @examples
#' \dontrun{
#' current_time()
#' }
#' @return String in the format "YYYY-MM-DD H:M:S" with the
#' timezone appended at the end.
#' @noRd
current_time <- function() {
format(x = Sys.time(), format = "%F %T", usetz = TRUE)
}

#' Show starting/restarting message on console
#'
#' @param type String. Type of message to show. Either "restarting"(default) or "starting".
#' @examples
#' \dontrun{
#' dash_and_msg()
#' }
#' @return `NULL`
#' @noRd
dash_and_msg <- function(type = c("restarting", "starting")) {
type <- match.arg(arg = type)

now <- current_time()
restart_msg <- sprintf("%s Files changed. Restarting...", now)
start_msg <- sprintf("%s Starting rmon...", now)
dashes <- strrep(x = "_", times = nchar(restart_msg))

msg <- switch(
EXPR = type,
restarting = restart_msg,
starting = start_msg
)

message(
dashes,
"\n",
msg,
"\n"
)
}

0 comments on commit d609787

Please sign in to comment.