From 91cd6222542770ecb66362614bbf16898f1a9598 Mon Sep 17 00:00:00 2001 From: hrbrmstr Date: Tue, 16 Jun 2020 08:57:31 -0400 Subject: [PATCH] initial commit --- .Rbuildignore | 1 + DESCRIPTION | 17 +++++++++------ LICENSE | 2 ++ LICENSE.md | 21 +++++++++++++++++++ NAMESPACE | 8 +++++-- R/add-entry.R | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ R/bitbar-package.R | 11 ++++++---- R/use-bitbar.R | 43 ++++++++++++++++++++++++++++++++++++++ R/utils-pipe.R | 11 ++++++++++ R/zzz.R | 3 +++ inst/templates/bitbar.R | 44 +++++++++++++++++++++++++++++++++++++++ man/add_entry.Rd | 31 +++++++++++++++++++++++++++ man/bitbar.Rd | 6 ++++-- man/new_bitbar_script.Rd | 26 +++++++++++++++++++++++ man/pipe.Rd | 12 +++++++++++ 15 files changed, 276 insertions(+), 14 deletions(-) create mode 100644 LICENSE create mode 100644 LICENSE.md create mode 100644 R/add-entry.R create mode 100644 R/use-bitbar.R create mode 100644 R/utils-pipe.R create mode 100644 R/zzz.R create mode 100644 inst/templates/bitbar.R create mode 100644 man/add_entry.Rd create mode 100644 man/new_bitbar_script.Rd create mode 100644 man/pipe.Rd diff --git a/.Rbuildignore b/.Rbuildignore index c9a5c92..19cdbd8 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -19,3 +19,4 @@ ^CRAN-RELEASE$ ^appveyor\.yml$ ^tools$ +^LICENSE\.md$ diff --git a/DESCRIPTION b/DESCRIPTION index ccf79bc..b0ca764 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: bitbar Type: Package -Title: bitbar title goes here otherwise CRAN checks fail +Title: Craft macOS Menu Bar Applications with Rscript Version: 0.1.0 Date: 2020-06-16 Authors@R: c( @@ -8,17 +8,22 @@ Authors@R: c( comment = c(ORCID = "0000-0001-5670-2640")) ) Maintainer: Bob Rudis -Description: A good description goes here otherwise CRAN checks fail. +Description: Mat Ryer's macOS utility enables any + script to become a menu bar application. Tools are provided to make it + easier to craft these scripts. URL: https://git.rud.is/hrbrmstr/bitbar BugReports: https://git.rud.is/hrbrmstr/bitbar/issues Encoding: UTF-8 -License: AGPL +License: MIT + file LICENSE Suggests: covr, tinytest +Imports: + usethis, + magrittr, + clipr, + knitr, + sys Depends: R (>= 3.5.0) -Imports: - httr, - jsonlite Roxygen: list(markdown = TRUE) RoxygenNote: 7.1.0 diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..49d554e --- /dev/null +++ b/LICENSE @@ -0,0 +1,2 @@ +YEAR: 2020 +COPYRIGHT HOLDER: Bob Rudis diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..524f6aa --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +# MIT License + +Copyright (c) 2020 Bob Rudis + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/NAMESPACE b/NAMESPACE index 5b4b9ae..6324d33 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,4 +1,8 @@ # Generated by roxygen2: do not edit by hand -import(httr) -importFrom(jsonlite,fromJSON) +export("%>%") +export(add_entry) +export(add_separator) +export(new_bitbar_script) +importFrom(magrittr,"%>%") +importFrom(usethis,use_template) diff --git a/R/add-entry.R b/R/add-entry.R new file mode 100644 index 0000000..d84c09a --- /dev/null +++ b/R/add-entry.R @@ -0,0 +1,54 @@ +#' Add an entry to the menu +#' +#' Use `add_separator()` to add a separator line and `add_entry()` to make new +#' menubar entries. +#' +#' @param text to display. See `type`. +#' @param type type of entry. +#' - `direct` has no dashes prefix and all output goes directly to the menubar. The first use of `direct` will set the menubar title. +#' - `submenu` uses `level` repeats of `--` to put these items in submenus +#' @param level used with `submenu` option of `type` +#' @param named BitBar entry parameters. See `References`. +#' @references [BitBar Plugin API Reference](https://github.com/matryer/bitbar#writing-plugins) +#' @export +add_entry <- function(text, type = c("direct", "submenu"), level=1, ...) { + + type <- match.arg(type, c("direct", "submenu")) + + type %>% + switch( + direct = text, + submenu = sprintf("%s%s", paste0(rep("--", level), collapse=""), text) + ) -> entry + + params <- "" + + p <- list(...) + + if (length(p) > 0) { + + sapply(names(p), function(name) { + value <- p[[name]] + if (is.logical(value)) { + value <- tolower(as.character(value)) + } else if (!is.character(value)) { + value <- toString(value) + } + sprintf("%s=%s", name, value) + }) -> params + + params <- paste0(params, collapse=" ") + + } + + if (nchar(params) > 0) params <- sprintf(" | %s", params) + + cat(entry, params, "\n", sep = "") + +} + +#' @rdname add_entry +#' @export +add_separator <- function() { + cat("---\n") +} \ No newline at end of file diff --git a/R/bitbar-package.R b/R/bitbar-package.R index f872496..6b3463d 100644 --- a/R/bitbar-package.R +++ b/R/bitbar-package.R @@ -1,9 +1,12 @@ -#' ... -#' +#' Craft macOS Menu Bar Applications with Rscript +#' +#' Mat Ryer's macOS utility enables any +#' script to become a menu bar application. Tools are provided to make it +#' easier to craft these scripts. +#' #' @md #' @name bitbar +#' @importFrom usethis use_template #' @keywords internal #' @author Bob Rudis (bob@@rud.is) -#' @import httr -#' @importFrom jsonlite fromJSON "_PACKAGE" diff --git a/R/use-bitbar.R b/R/use-bitbar.R new file mode 100644 index 0000000..7d45a5a --- /dev/null +++ b/R/use-bitbar.R @@ -0,0 +1,43 @@ +#' Helper to get started with a new BitBar {bitbar} script +#' +#' @param save_as full path (including scriptname.R) to where you want the file saved +#' @param title,version,author,github_user,description,dependencies,image_url,about_url +#' BitBar metadata passed on to the generator. +#' @export +new_bitbar_script <- function(save_as, + title = "Script Title", + version = "1.0", + author = Sys.info()[["user"]], + github_user = author, + description = "BitBar Plugin Using R", + dependencies = "R", + image_url = "", + about_url = "") { + + usethis:::render_template( + template = "bitbar.R", + package = "bitbar", + data = list( + title = title, + mb_title = title, + version = version, + author = author, + github_user = github_user, + description = description, + dependencies = dependencies, + image_url = image_url, + about_url = about_url + ) + ) -> rendered_template + + writeLines(rendered_template, con = path.expand(save_as)) + + Sys.chmod(path.expand(save_as), "0755") + + if (rstudioapi::isAvailable() && rstudioapi::hasFun("navigateToFile")) { + rstudioapi::navigateToFile(path.expand(save_as)) + } else { + utils::file.edit(path.expand(save_as)) + } + +} diff --git a/R/utils-pipe.R b/R/utils-pipe.R new file mode 100644 index 0000000..e79f3d8 --- /dev/null +++ b/R/utils-pipe.R @@ -0,0 +1,11 @@ +#' Pipe operator +#' +#' See \code{magrittr::\link[magrittr:pipe]{\%>\%}} for details. +#' +#' @name %>% +#' @rdname pipe +#' @keywords internal +#' @export +#' @importFrom magrittr %>% +#' @usage lhs \%>\% rhs +NULL diff --git a/R/zzz.R b/R/zzz.R new file mode 100644 index 0000000..fc49bf3 --- /dev/null +++ b/R/zzz.R @@ -0,0 +1,3 @@ +.onLoad <- function(libname, pkgname) { + Sys.setenv("CLIPR_ALLOW"=TRUE) +} \ No newline at end of file diff --git a/inst/templates/bitbar.R b/inst/templates/bitbar.R new file mode 100644 index 0000000..4bd845f --- /dev/null +++ b/inst/templates/bitbar.R @@ -0,0 +1,44 @@ +#!/Library/Frameworks/R.framework/Resources/bin/Rscript --default-packages=base,tools,utils,stats,methods +# +# {{{title}}} +# {{{version}}} +# {{{author}}} +# {{{github_user}}} +# {{{description}}} +# {{{dependencies}}} +# {{{image_url}}} +# {{{about_url}}} + +suppressPackageStartupMessages({ + library(magrittr, quietly = TRUE, verbose = FALSE, warn.conflicts = FALSE) + library(clipr, quietly = TRUE, verbose = FALSE, warn.conflicts = FALSE) + library(sys, quietly = TRUE, verbose = FALSE, warn.conflicts = FALSE) + library(bitbar, quietly = TRUE, verbose = FALSE, warn.conflicts = FALSE) + # other library() calls here +}) + +add_entry("{{{title}}}") # this will be the menubar title + +add_separator() + +add_entry(":mushroom: color!", color="#ff0000") # color! + +add_entry("R-project…", href="https://r-project.org/") # clickable URL + +add_separator() + +add_entry("Somewhere") # this will be the submenu entry point +add_entry("Beyond the sea", type = "submenu") +add_entry("Sea", type = "submenu") # this will be a submenu of the submenu +add_entry("Under the Sea", type = "submenu", level = 2) + +add_separator() + +add_entry("What's on your clipboard?") +add_entry(read_clip(), font="Monaco") + +# Keep at end of script --------------------------------------------------- + +add_separator() + +add_entry("Refresh", refresh=TRUE) diff --git a/man/add_entry.Rd b/man/add_entry.Rd new file mode 100644 index 0000000..fcb6f53 --- /dev/null +++ b/man/add_entry.Rd @@ -0,0 +1,31 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/add-entry.R +\name{add_entry} +\alias{add_entry} +\alias{add_separator} +\title{Add an entry to the menu} +\usage{ +add_entry(text, type = c("direct", "output", "menu", "submenu"), ...) + +add_separator() +} +\arguments{ +\item{text}{to display. See \code{type}.} + +\item{type}{type of entry. +\itemize{ +\item \code{direct} has no dashes prefix and all output goes directly to the menubar. The first use of \code{direct} will set the menubar title. +\item \code{output} uses \verb{---}, +\item \code{menu} uses \verb{--}, +\item \code{submenu} uses \verb{----} +}} + +\item{named}{BitBar entry parameters. See \code{References}.} +} +\description{ +Use \code{add_separator()} to add a separator line and \code{add_entry()} to make new +menubar entries. +} +\references{ +\href{https://github.com/matryer/bitbar#writing-plugins}{BitBar Plugin API Reference} +} diff --git a/man/bitbar.Rd b/man/bitbar.Rd index 57e64a2..b3614e4 100644 --- a/man/bitbar.Rd +++ b/man/bitbar.Rd @@ -4,9 +4,11 @@ \name{bitbar} \alias{bitbar} \alias{bitbar-package} -\title{...} +\title{Craft macOS Menu Bar Applications with Rscript} \description{ -A good description goes here otherwise CRAN checks fail. +Mat Ryer's macOS utility \url{https://getbitbar.com/} enables any +script to become a menu bar application. Tools are provided to make it +easier to craft these scripts. } \seealso{ Useful links: diff --git a/man/new_bitbar_script.Rd b/man/new_bitbar_script.Rd new file mode 100644 index 0000000..351dc18 --- /dev/null +++ b/man/new_bitbar_script.Rd @@ -0,0 +1,26 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/use-bitbar.R +\name{new_bitbar_script} +\alias{new_bitbar_script} +\title{Helper to get started with a new BitBar {bitbar} script} +\usage{ +new_bitbar_script( + save_as, + title = "Script Title", + version = "1.0", + author = Sys.info()[["user"]], + github_user = author, + description = "BitBar Plugin Using R", + dependencies = "R", + image_url = "", + about_url = "" +) +} +\arguments{ +\item{save_as}{full path (including scriptname.R) to where you want the file saved} + +\item{title, version, author, github_user, description, dependencies, image_url, about_url}{BitBar metadata passed on to the generator.} +} +\description{ +Helper to get started with a new BitBar {bitbar} script +} diff --git a/man/pipe.Rd b/man/pipe.Rd new file mode 100644 index 0000000..0eec752 --- /dev/null +++ b/man/pipe.Rd @@ -0,0 +1,12 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils-pipe.R +\name{\%>\%} +\alias{\%>\%} +\title{Pipe operator} +\usage{ +lhs \%>\% rhs +} +\description{ +See \code{magrittr::\link[magrittr:pipe]{\%>\%}} for details. +} +\keyword{internal}