Browse Source

super alpha but here goes

master
boB Rudis 4 years ago
parent
commit
fec41ea8b2
No known key found for this signature in database GPG Key ID: 1D7529BE14E2BBA9
  1. 10
      DESCRIPTION
  2. 7
      NAMESPACE
  3. 4
      R/add-entry.R
  4. 65
      R/addin.R
  5. 37
      R/base64-image.R
  6. 16
      R/bitbar-package.R
  7. 6
      R/imgs.R
  8. 42
      R/read-bitbar-prefs.R
  9. 8
      R/use-bitbar.R
  10. 24
      README.Rmd
  11. 99
      README.md
  12. BIN
      inst/img/r-logo-small.png
  13. BIN
      inst/rstudio/.addins.dcf.swp
  14. 4
      inst/rstudio/addins.dcf
  15. 16
      inst/templates/bitbar.R
  16. 8
      man/add_entry.Rd
  17. 14
      man/b64_image.Rd
  18. 13
      man/bitbar.Rd
  19. 11
      man/bitbar_plugins_dir.Rd
  20. 9
      man/new_bitbar_script.Rd
  21. 11
      man/r_logo_small.Rd

10
DESCRIPTION

@ -21,9 +21,13 @@ Imports:
usethis,
magrittr,
clipr,
knitr,
sys
XML,
httr,
sys,
miniUI,
shiny,
ulid
Depends:
R (>= 3.5.0)
R (>= 3.6.0)
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.0

7
NAMESPACE

@ -3,6 +3,13 @@
export("%>%")
export(add_entry)
export(add_separator)
export(b64_image)
export(bitbar_plugins_dir)
export(new_bitbar_script)
export(r_logo_small)
import(httr)
import(miniUI)
import(shiny)
importFrom(XML,readKeyValueDB)
importFrom(magrittr,"%>%")
importFrom(usethis,use_template)

4
R/add-entry.R

@ -33,6 +33,8 @@ add_entry <- function(text, type = c("direct", "submenu"), level=1, ...) {
value <- tolower(as.character(value))
} else if (!is.character(value)) {
value <- toString(value)
} else {
if (name == "bash") { value = sprintf("'%s'", value) }
}
sprintf("%s=%s", name, value)
}) -> params
@ -41,7 +43,7 @@ add_entry <- function(text, type = c("direct", "submenu"), level=1, ...) {
}
if (nchar(params) > 0) params <- sprintf(" | %s", params)
if (nchar(params) > 0) params <- sprintf("|%s", params)
cat(entry, params, "\n", sep = "")

65
R/addin.R

@ -0,0 +1,65 @@
create_new_bitbar_script <- function() {
miniPage(
gadgetTitleBar("New BitBar"),
miniContentPanel(
shiny::textInput(
inputId = "save_as", label = "Save as:", width="100%",
value = file.path(bitbar_plugins_dir(), sprintf("%s-new-plugin.R", ulid::generate())),
),
shiny::textInput(
inputId = "title", label = "Title:", value = "Script Title", width="100%",
),
shiny::textInput(
inputId = "version", label = "Version:", value = "1.0", width="100%",
),
shiny::textInput(
inputId = "author", label = "Author:", value = Sys.info()[["user"]], width="100%",
),
shiny::textInput(
inputId = "github_user", label = "GitHub User:", value = Sys.info()[["user"]], width="100%",
),
shiny::textInput(
inputId = "description", label = "Description:", value = "BitBar Plugin Using R", width="100%",
),
shiny::textInput(
inputId = "dependencies", label = "Dependencies:", value = "R", width="100%",
),
shiny::textInput(
inputId = "image_url", label = "Image URL:", value = "", width="100%",
),
shiny::textInput(
inputId = "about_url", label = "About URL:", value = "", width="100%",
)
)
) -> ui
server <- function(input, output, session) {
observeEvent(input$done, {
bitbar::new_bitbar_script(
save_as = input$save_as,
title = input$title,
version = input$version,
author = input$author,
github_user = input$github_user,
description = input$description,
dependencies = input$dependencies,
image_url = input$image_url,
about_url = input$about_url
)
stopApp(returnValue)
})
}
viewer <- dialogViewer("New BitBar", width = 400, height = 800)
runGadget(ui, server, viewer = viewer)
}

37
R/base64-image.R

@ -0,0 +1,37 @@
#' Return a base64 encoded string of an image (local filesystem or URL)
#'
#' @param path_or_url local path or URL for the image
#' @export
b64_image <- function(path_or_url) {
path_or_url <- path_or_url[1]
if (grepl("^http[s]*://", path_or_url)) {
httr::GET(
url = path_or_url,
httr::write_memory()
) -> res
httr::stop_for_status(res)
bytes <- httr::content(res, as = "raw", encoding = "UTF-8")
} else {
path_or_url <- path.expand(path_or_url)
file_size <- file.info(path_or_url)$size
stopifnot(
c(
"File not found." = file.exists(path_or_url),
"File is empty." = file.info(path_or_url)$size > 0
)
)
bytes <- readBin(path_or_url, "raw", n = file.info(path_or_url)$size)
}
openssl::base64_encode(bytes)
}

16
R/bitbar-package.R

@ -4,9 +4,25 @@
#' script to become a menu bar application. Tools are provided to make it
#' easier to craft these scripts.
#'
#' @section Installing BitBar:
#'
#' If you have [homebrew](https://brew.sh/) installed, you can do:
#'
#' ```shell
#' brew cask install bitbar
#' ```
#'
#' Otherwise [grab the latest release](https://github.com/matryer/bitbar/releases/latest) and install it the hard way.
#'
#' Open the application and choose the directory you want to be your plugins directory. This is where your menubar scripts will go.
#' (I use `~/Library/BitBar/Plugins` but you can pick any directory; just make sure it isn't one with
#' a gazillion files as that impacts BitBar's performance).
#'
#' @md
#' @name bitbar
#' @importFrom usethis use_template
#' @importFrom XML readKeyValueDB
#' @import httr shiny miniUI
#' @keywords internal
#' @author Bob Rudis (bob@@rud.is)
"_PACKAGE"

6
R/imgs.R

@ -0,0 +1,6 @@
#' Images that come with the package
#'
#' @export
r_logo_small <- function() {
system.file("img", "r-logo-small.png", package="bitbar")
}

42
R/read-bitbar-prefs.R

@ -0,0 +1,42 @@
read_bitbar_prefs <- function() {
bbprefs <- path.expand("~/Library/Preferences/com.matryer.BitBar.plist")
if (!file.exists(bbprefs)) {
stop(
paste0(strwrap(paste0(c(
"BitBar preferences file not found. ",
"This likely means BitBar is not installed. ",
"Please install BitBar before using the {bitbar} package."
), collapse = "")), collapse="\n"),
call.=FALSE
)
}
tf <- tempfile(fileext = ".xml")
on.exit(unlink(tf))
sys::exec_internal(
cmd = "plistutil",
args = c("--infile", bbprefs, "--outfile", tf)
) -> res
if (res$status == 0) {
res <- XML::readKeyValueDB(tf)
res
} else {
stop("Error reading BitBar preferences.", call.=FALSE)
}
}
#' BitBar plugins directory
#'
#' @export
bitbar_plugins_dir <- function() {
prefs <- read_bitbar_prefs()
prefs[["pluginsDirectory"]]
}

8
R/use-bitbar.R

@ -4,6 +4,14 @@
#' @param title,version,author,github_user,description,dependencies,image_url,about_url
#' BitBar metadata passed on to the generator.
#' @export
#' @examples
#' if (interactive()) {
#' new_bitbar_script(
#' save_as = file.path(bitbar_plugins_dir(), "my-awesome-bitbar-script.R"),
#' title = ":blue_heart:",
#' description = "Example BitBar Script")
#' )
#' }
new_bitbar_script <- function(save_as,
title = "Script Title",
version = "1.0",

24
README.Rmd

@ -15,6 +15,20 @@ hrbrpkghelpr::stinking_badges()
hrbrpkghelpr::yank_title_and_description()
```
## Installing BitBar
If you have [homebrew](https://brew.sh/) installed, you can do:
```shell
brew cask install bitbar
```
Otherwise [grab the latest release](https://github.com/matryer/bitbar/releases/latest) and install it the hard way.
Open the application and choose the directory you want to be your plugins directory. This is where your menubar scripts will go.
(I use `~/Library/BitBar/Plugins` but you can pick any directory; just make sure it isn't one with
a gazillion files as that impacts BitBar's performance).
## What's Inside The Tin
The following functions are implemented:
@ -39,6 +53,16 @@ packageVersion("bitbar")
```
This will open up a `my-awesome-bitbar-script.R` in RStudio (if available) or whatever editor `file.edit()` is configured to use.
```{r ex-01, eval=FALSE}
new_bitbar_script(
save_as = file.path(bitbar_plugins_dir(), "my-awesome-bitbar-script.R"),
title = ":blue_heart:",
description = "Example BitBar Script")
)
```
## bitbar Metrics
```{r cloc, echo=FALSE}

99
README.md

@ -0,0 +1,99 @@
[![Project Status: Active – The project has reached a stable, usable
state and is being actively
developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)
[![Signed
by](https://img.shields.io/badge/Keybase-Verified-brightgreen.svg)](https://keybase.io/hrbrmstr)
![Signed commit
%](https://img.shields.io/badge/Signed_Commits-100%25-lightgrey.svg)
[![Linux build
Status](https://travis-ci.org/hrbrmstr/bitbar.svg?branch=master)](https://travis-ci.org/hrbrmstr/bitbar)
![Minimal R
Version](https://img.shields.io/badge/R%3E%3D-3.5.0-blue.svg)
![License](https://img.shields.io/badge/License-MIT-blue.svg)
# bitbar
Craft macOS Menu Bar Applications with Rscript
## Description
Mat Ryer’s macOS utility <https://getbitbar.com/> enables any script to
become a menu bar application. Tools are provided to make it easier to
craft these scripts.
## Installing BitBar
If you have [homebrew](https://brew.sh/) installed, you can do:
``` shell
brew cask install bitbar
```
Otherwise [grab the latest
release](https://github.com/matryer/bitbar/releases/latest) and install
it the hard way.
Open the application and choose the directory you want to be your
plugins directory. This is where your menubar scripts will go. (I use
`~/Library/Application Support/BitBar/Plugins` but you can pick any
directory; just make sure it isn’t one with a gazillion files as that
impacts BitBar’s performance).
## What’s Inside The Tin
The following functions are implemented:
- `add_entry`: Add an entry to the menu
- `bitbar_plugins_dir`: BitBar plugins directory
- `new_bitbar_script`: Helper to get started with a new BitBar bitbar
script
## Installation
``` r
remotes::install_git("https://git.rud.is/hrbrmstr/bitbar.git")
# or
remotes::install_git("https://git.sr.ht/~hrbrmstr/bitbar")
# or
remotes::install_gitlab("hrbrmstr/bitbar")
# or
remotes::install_bitbucket("hrbrmstr/bitbar")
```
NOTE: To use the ‘remotes’ install options you will need to have the
[{remotes} package](https://github.com/r-lib/remotes) installed.
## Usage
``` r
library(bitbar)
# current version
packageVersion("bitbar")
## [1] '0.1.0'
```
This will open up a `my-awesome-bitbar-script.R` in RStudio (if
available) or whatever editor `file.edit()` is configured to use.
``` r
new_bitbar_script(
save_as = file.path(bitbar_plugins_dir(), "my-awesome-bitbar-script.R"),
title = ":blue_heart:",
description = "Example BitBar Script")
)
```
## bitbar Metrics
| Lang | \# Files | (%) | LoC | (%) | Blank lines | (%) | \# Lines | (%) |
| :--- | -------: | ---: | --: | ---: | ----------: | ---: | -------: | ---: |
| R | 7 | 0.88 | 96 | 0.88 | 29 | 0.57 | 68 | 0.63 |
| Rmd | 1 | 0.12 | 13 | 0.12 | 22 | 0.43 | 40 | 0.37 |
## Code of Conduct
Please note that this project is released with a Contributor Code of
Conduct. By participating in this project you agree to abide by its
terms.

BIN
inst/img/r-logo-small.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 674 B

BIN
inst/rstudio/.addins.dcf.swp

Binary file not shown.

4
inst/rstudio/addins.dcf

@ -0,0 +1,4 @@
Name: Create a new {bitbar} script
Description: Create a new {bitbar} script
Binding: create_new_bitbar_script
Interactive: true

16
inst/templates/bitbar.R

@ -1,4 +1,4 @@
#!/Library/Frameworks/R.framework/Resources/bin/Rscript --default-packages=base,tools,utils,stats,methods
#!/Library/Frameworks/R.framework/Resources/bin/Rscript
#
# <bitbar.title>{{{title}}}</bitbar.title>
# <bitbar.version>{{{version}}}</bitbar.version>
@ -17,13 +17,25 @@ suppressPackageStartupMessages({
# other library() calls here
})
this_script <- sub("--file=", "", grep("--file=", commandArgs(trailingOnly = FALSE), value=TRUE)) # assumes no spaces
args <- commandArgs(trailingOnly = TRUE) # holds param1, param2, etc.; change to TRUE if you want path to script
if (length(args) > 0) {
# handle self-invocation vs display menu things
}
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_entry("Open R Frameworks directory…", bash="/usr/bin/open", param1="file:///Library/Frameworks/R.framework/Versions/Current", terminal=FALSE)
add_entry("Visit R-project…", href="https://r-project.org/") # clickable URL
add_entry("Visit R for macOS…", href="https://mac.r-project.org/") # clickable URL
add_separator()

8
man/add_entry.Rd

@ -5,7 +5,7 @@
\alias{add_separator}
\title{Add an entry to the menu}
\usage{
add_entry(text, type = c("direct", "output", "menu", "submenu"), ...)
add_entry(text, type = c("direct", "submenu"), level = 1, ...)
add_separator()
}
@ -15,11 +15,11 @@ add_separator()
\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 \code{submenu} uses \code{level} repeats of \verb{--} to put these items in submenus
}}
\item{level}{used with \code{submenu} option of \code{type}}
\item{named}{BitBar entry parameters. See \code{References}.}
}
\description{

14
man/b64_image.Rd

@ -0,0 +1,14 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/base64-image.R
\name{b64_image}
\alias{b64_image}
\title{Return a base64 encoded string of an image (local filesystem or URL)}
\usage{
b64_image(path_or_url)
}
\arguments{
\item{path_or_url}{local path or URL for the image}
}
\description{
Return a base64 encoded string of an image (local filesystem or URL)
}

13
man/bitbar.Rd

@ -10,6 +10,19 @@ 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.
}
\section{Installing BitBar}{
If you have \href{https://brew.sh/}{homebrew} installed, you can do:\if{html}{\out{<div class="shell">}}\preformatted{brew cask install bitbar
}\if{html}{\out{</div>}}
Otherwise \href{https://github.com/matryer/bitbar/releases/latest}{grab the latest release} and install it the hard way.
Open the application and choose the directory you want to be your plugins directory. This is where your menubar scripts will go.
(I use \verb{~/Library/BitBar/Plugins} but you can pick any directory; just make sure it isn't one with
a gazillion files as that impacts BitBar's performance).
}
\seealso{
Useful links:
\itemize{

11
man/bitbar_plugins_dir.Rd

@ -0,0 +1,11 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/read-bitbar-prefs.R
\name{bitbar_plugins_dir}
\alias{bitbar_plugins_dir}
\title{BitBar plugins directory}
\usage{
bitbar_plugins_dir()
}
\description{
BitBar plugins directory
}

9
man/new_bitbar_script.Rd

@ -24,3 +24,12 @@ new_bitbar_script(
\description{
Helper to get started with a new BitBar {bitbar} script
}
\examples{
if (interactive()) {
new_bitbar_script(
save_as = file.path(bitbar_plugins_dir(), "my-awesome-bitbar-script.R"),
title = ":blue_heart:",
description = "Example BitBar Script")
)
}
}

11
man/r_logo_small.Rd

@ -0,0 +1,11 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/imgs.R
\name{r_logo_small}
\alias{r_logo_small}
\title{Images that come with the package}
\usage{
r_logo_small()
}
\description{
Images that come with the package
}
Loading…
Cancel
Save