Skip to content

Commit

Permalink
- Added local_timer().
Browse files Browse the repository at this point in the history
Closes #12
  • Loading branch information
joethorley committed Aug 5, 2024
1 parent 79f5ac0 commit 05cc42f
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 9 deletions.
6 changes: 4 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(local_timer)
export(tmr_ceiling)
export(tmr_elapsed)
export(tmr_floor)
Expand Down
7 changes: 7 additions & 0 deletions R/chk.R
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
28 changes: 28 additions & 0 deletions R/local-timer.R
Original file line number Diff line number Diff line change
@@ -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
}
8 changes: 5 additions & 3 deletions R/params.R
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions R/test-helpers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
test_local_timer <- function() {
local_timer()
Sys.sleep(0.1)
NULL
}
20 changes: 20 additions & 0 deletions man/local_timer.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions man/params.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions tests/testthat/test-local-timer.R
Original file line number Diff line number Diff line change
@@ -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()
})

0 comments on commit 05cc42f

Please sign in to comment.