diff --git a/DESCRIPTION b/DESCRIPTION index b680545..32e9685 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -19,10 +19,12 @@ License: MIT + file LICENSE Depends: R (>= 3.6) Imports: - hms + hms, + rlang Suggests: covr, - testthat (>= 3.0.0) + testthat (>= 3.0.0), + withr Config/testthat/edition: 3 Encoding: UTF-8 Language: en-US diff --git a/NAMESPACE b/NAMESPACE index 16cfdc8..1ec090a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,6 @@ # Generated by roxygen2: do not edit by hand +export(local_timer) export(tmr_ceiling) export(tmr_elapsed) export(tmr_floor) diff --git a/R/chk.R b/R/chk.R index 4bc909a..f122108 100644 --- a/R/chk.R +++ b/R/chk.R @@ -9,6 +9,13 @@ chk_digits <- function(digits) { err("`digits` must be a whole number.") } +chk_env <- function(env) { + if(is.environment(env)) { + return(invisible(env)) + } + err("`env` must be an environment.") +} + chk_seconds <- function(seconds) { if (is.numeric(seconds) && length(seconds) == 1L && !is.na(seconds)) { return(invisible()) diff --git a/R/local-timer.R b/R/local-timer.R new file mode 100644 index 0000000..d9b5533 --- /dev/null +++ b/R/local-timer.R @@ -0,0 +1,28 @@ +#' Local Timer +#' +#' @inheritParams params +#' +#' @return xx +#' @export +#' +#' @examples +#' local_timer() +local_timer <- function(.local_envir = rlang::caller_env()) { + chk_env(.local_envir) + + if(!requireNamespace("withr", quietly = TRUE)) { + err("Package 'withr' must be installed to create a local_timer().") + } + + tmr <- tmr_(seconds = 0, start = TRUE, caller = sys.call(-1)) + withr::defer(message(tmr_format(tmr)), envir = .local_envir) + invisible(tmr) +} + +tmr_ <- function(seconds, start, caller = sys.call(-1)) { + seconds <- as.double(seconds) + + x <- as_hms(seconds) + if (start) x <- tmr_start(x) + x +} diff --git a/R/params.R b/R/params.R index 4dbee28..5de4a49 100644 --- a/R/params.R +++ b/R/params.R @@ -1,8 +1,10 @@ -#' #' Parameter Descriptions for hmstimer Functions -#' @param x A [hms_timer()]. +#' Parameter Descriptions for hmstimer Functions +#' +#' @param .local_envir The environment to use for scoping +#' @param digits A whole number of the number of decimal places. #' @param seconds A non-negative numeric scalar of the initial number of seconds. #' @param start A flag indicating whether to start the timer. -#' @param digits A whole number of the number of decimal places. +#' @param x A [hms_timer()]. #' @keywords internal #' @name params NULL diff --git a/R/test-helpers.R b/R/test-helpers.R new file mode 100644 index 0000000..0740054 --- /dev/null +++ b/R/test-helpers.R @@ -0,0 +1,5 @@ +test_local_timer <- function() { + local_timer() + Sys.sleep(0.1) + NULL +} diff --git a/man/local_timer.Rd b/man/local_timer.Rd new file mode 100644 index 0000000..4ceff45 --- /dev/null +++ b/man/local_timer.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/local-timer.R +\name{local_timer} +\alias{local_timer} +\title{Local Timer} +\usage{ +local_timer(.local_envir = rlang::caller_env()) +} +\arguments{ +\item{.local_envir}{The environment to use for scoping} +} +\value{ +xx +} +\description{ +Local Timer +} +\examples{ +local_timer() +} diff --git a/man/params.Rd b/man/params.Rd index 90238c5..35e1b1d 100644 --- a/man/params.Rd +++ b/man/params.Rd @@ -2,17 +2,19 @@ % Please edit documentation in R/params.R \name{params} \alias{params} -\title{#' Parameter Descriptions for hmstimer Functions} +\title{Parameter Descriptions for hmstimer Functions} \arguments{ -\item{x}{A \code{\link[=hms_timer]{hms_timer()}}.} +\item{.local_envir}{The environment to use for scoping} + +\item{digits}{A whole number of the number of decimal places.} \item{seconds}{A non-negative numeric scalar of the initial number of seconds.} \item{start}{A flag indicating whether to start the timer.} -\item{digits}{A whole number of the number of decimal places.} +\item{x}{A \code{\link[=hms_timer]{hms_timer()}}.} } \description{ -#' Parameter Descriptions for hmstimer Functions +Parameter Descriptions for hmstimer Functions } \keyword{internal} diff --git a/tests/testthat/test-local-timer.R b/tests/testthat/test-local-timer.R new file mode 100644 index 0000000..1b01ce2 --- /dev/null +++ b/tests/testthat/test-local-timer.R @@ -0,0 +1,12 @@ +test_that("local_timer", { + fun <- function(x) { + tmr <- local_timer() + Sys.sleep(0.1) + NULL + } + expect_message(fun(), "^00:00:00.1\\d{2,2}\\s$") +}) + +test_that("test_local_timer()", { + test_local_timer() +})