Skip to content

Commit

Permalink
Add reprex_to_gist() function
Browse files Browse the repository at this point in the history
  • Loading branch information
francisbarton committed Apr 1, 2024
1 parent 724f882 commit 942cc95
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 1 deletion.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Imports:
crayon,
dplyr (>= 1.1.0),
gert,
gh,
glue,
gt,
here,
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export(rationalise_df)
export(read_rss)
export(register_pdf_font)
export(remove_nas)
export(reprex_to_gist)
export(save_it)
export(suggest_postcode_fixes)
export(transpose_tbl_wider)
Expand All @@ -47,6 +48,7 @@ importFrom(dplyr,desc)
importFrom(dplyr,if_any)
importFrom(dplyr,if_else)
importFrom(dplyr,join_by)
importFrom(rlang,.data)
importFrom(rlang,`:=`)
importFrom(tidyselect,all_of)
importFrom(tidyselect,any_of)
Expand Down
2 changes: 1 addition & 1 deletion R/register_pdf_font.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

register_pdf_font <- function(name) {

assertthat::assert_that(require(extrafontdb), msg = "It looks like the extrafontdb package is not installed.")
assertthat::assert_that(requireNamespace("extrafontdb"), msg = "It looks like the extrafontdb package is not installed.")

font_paths <- function(name, path) {
list.files(path, pattern = paste0("^", name, ".*.afm.gz"), ignore.case = TRUE)
Expand Down
48 changes: 48 additions & 0 deletions R/reprex_to_gist.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#' Easily post reprexes from selected text to a GitHub gist
#'
#' If no selected text is found for the reprex input then the currently visible
#' open file, and then the system clipboard, will be tried in that order.
#'
#' From https://github.com/tidyverse/reprex/issues/190#issuecomment-817313938
#' by Mickaël Canouil
#'
#' @export
reprex_to_gist <- function() {
input <- reprex:::rstudio_selection()
if (input == character(0)) input <- reprex:::rstudio_file()
if (input == character(0)) input <- reprex:::ingest_clipboard()
if (all(input == "")) cli::cli_abort("No input found for reprex")

output <- reprex::reprex(input = input, style = TRUE, html_preview = FALSE)
codename <- sub("-", "_", sample(reprex:::adjective_animal, 1L))
ts <- format(Sys.time(), "%Y%m%d_%H%M%S")
file_id <- glue::glue("{codename}_{ts}.R")
post_data <- list(list(content = paste(input, collapse = "\n"))) |>
rlang::set_names(file_id)


# Post the source code as a gist
gist_info <- gh::gh(
"POST /gists",
description = "reprex code",
files = post_data,
public = TRUE
)

# Post the reprex output as a comment on the gist
gist_comment <- gh::gh(
gist_info[["comments_url"]],
.method = "POST",
body = paste(output, collapse = "\n")
)

gist_base <- "https://gist.github.com"
my_gh <- gh::gh_whoami()[["login"]]
gist_id <- gist_info[["id"]]
gist_url <- glue::glue("{gist_base}/{my_gh}/{gist_id}")

cli::cli_alert_success("reprex posted to {gist_url}")
writeClipboard(gist_url)
utils::browseURL(gist_url)
invisible(TRUE)
}
Binary file added data/myr_colours.rda
Binary file not shown.
16 changes: 16 additions & 0 deletions man/reprex_to_gist.Rd

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

0 comments on commit 942cc95

Please sign in to comment.