diff --git a/R/utils.R b/R/utils.R index 85b4cfa..23aa992 100644 --- a/R/utils.R +++ b/R/utils.R @@ -8,6 +8,11 @@ check_pacman_dir <- function() { return(invisible(FALSE)) } } + if (!dir.exists("./.pacman/library")) { + if (!dir.create("./.pacman/library")) { + return(invisible(FALSE)) + } + } invisible(TRUE) } @@ -21,4 +26,35 @@ get_cran_repo <- function() { repo <- getOption("repos")[[1]] } repo +} + +#' Displays a menu for the user to select elements from. +#' +#' @param choices A character vector of choices the user must choose from +#' @param title An optional string containing text for a title line above the +#' menu +#' @param msg An optional string containung the text directing the user what to +#' do, displayed below the menu +#' @param default An integer containing the index of the default choice to return +#' +#' @return string Selected menu choice +select_menu <- function(choices, title = NULL, msg = "Enter a number from the menu above, or an empty line to default: ", default = 1) { + if (!is.null(title)) { + cat(title, "\n", sep = "") + } + + nc <- length(choices) + op <- paste0(format(seq_len(nc)), ": ", choices) + fop <- format(op) + cat("", fop, "", sep = "\n") + repeat { + answer <- readline(msg) + answer <- strsplit(answer, "[ ,]+")[[1]] + if (length(answer) == 0) { + return(choices[default]) + } + if (all(answer %in% seq_along(choices))) { + return(choices[as.integer(answer)]) + } + } } \ No newline at end of file diff --git a/man/select_menu.Rd b/man/select_menu.Rd new file mode 100644 index 0000000..76410d6 --- /dev/null +++ b/man/select_menu.Rd @@ -0,0 +1,30 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{select_menu} +\alias{select_menu} +\title{Displays a menu for the user to select elements from.} +\usage{ +select_menu( + choices, + title = NULL, + msg = "Enter a number from the menu above, or an empty line to default: ", + default = 1 +) +} +\arguments{ +\item{choices}{A character vector of choices the user must choose from} + +\item{title}{An optional string containing text for a title line above the +menu} + +\item{msg}{An optional string containung the text directing the user what to +do, displayed below the menu} + +\item{default}{An integer containing the index of the default choice to return} +} +\value{ +string Selected menu choice +} +\description{ +Displays a menu for the user to select elements from. +}