R/todolist.R

Defines functions todolist

Documented in todolist

#' Todolist
#'
#' @param path path to search through scripts
#' @param FLAGrecursive search subfolders?
#' @param FLAGremoveDone remove done bullet points
#' @param nchar Limit the number of characters in task
#'
#' @return markdown table printed to console
#' @export
#' @author Daniel Lill (daniel.lill@physik.uni-freiburg.de)
#' @md
#' @family structuring work
#' @importFrom data.table data.table rbindlist copy
#' @importFrom stringr str_extract str_remove str_detect fixed
todolist <- function(path = ".", FLAGrecursive = FALSE, FLAGremoveDone = TRUE, nchar = 80) {
  
  files <- list.files(path, "\\.(R|md|txt)$", recursive = FLAGrecursive, full.names = TRUE)
  d <- lapply(setNames(files, basename(files)), function(fl) {
    ft <- grep("# \\[[x ]*\\]", readLines(fl), value = TRUE)
    ft
    data.table::data.table(gout = ft)
  })
  
  d <- data.table::rbindlist(d, idcol = "script")
  
  d[,`:=`(scriptn = data.table::copy(script))]
  d[,`:=`(scriptn = stringr::str_extract(scriptn, "S\\d+(-\\d+)?"))]
  d[,`:=`(scriptn = stringr::str_remove(scriptn, "S"))]
  d[is.na(scriptn),`:=`(scriptn = script)]
    
  d[,`:=`(done = stringr::str_detect(gout, "\\[x\\]"))]
  d[,`:=`(check = ifelse(done, "[x]", "[ ]"))]
  
  d[,`:=`(task = data.table::copy(gout))]
  d[,`:=`(task = stringr::str_remove(task, ".*# ?"))]
  d[,`:=`(task = stringr::str_remove(task, stringr::fixed(check)))]
  
  # Truncate strings
  d[,`:=`(scriptn = stringr::str_trunc(scriptn, width = 40, side = "right"))]
  d[,`:=`(task = stringr::str_trunc(task, width = nchar, side = "right"))]
  
  d <- d[order(done, scriptn)]
  if (FLAGremoveDone) d <- d[done != TRUE]
  d <- d[,list(check, scriptn, task)]
  
  if (!nrow(d)) {cat("No todolist points left :) The following files were checked:\n", paste0(files, collapse = "\n"));return()}
  cfoutput_MdTable(dt = d, split_by = "check")
}
dlill/conveniencefunctions documentation built on Sept. 30, 2022, 4:40 a.m.