Skip to content

Instantly share code, notes, and snippets.

@tjmahr
Last active December 14, 2023 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjmahr/f63c21ad044311f7433999e97cbbfcd8 to your computer and use it in GitHub Desktop.
Save tjmahr/f63c21ad044311f7433999e97cbbfcd8 to your computer and use it in GitHub Desktop.
bib_glance()
bib_glance <- function(sort = TRUE) {
path <- "./notebook/refs.bib"
refs <- bibtex::read.bib(path)
if (sort) {
refs <- refs[sort(names(refs))]
}
titles <- purrr::map_chr(refs, purrr::pluck, "title") |>
stringr::str_replace_all("\\{|\\}", "") |>
stringr::str_replace_all("\\s\\s+", " ")
preview_width <- unlist(options("width")) - max(nchar(names(refs))) - 3
short_titles <- ifelse(
nchar(titles) > (preview_width - 3),
paste0(substr(titles, 1, (preview_width - 3)), "..."),
titles
)
tags <- paste0("[@", names(refs), "]")
padded <- stringr::str_pad(tags, max(nchar(tags)), "left")
cli::cli_text("bibliography {.file {path}}:")
print(glue::glue("{padded} {short_titles}"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment