Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

102 lines
3.1KB

  1. render_sass <- function(file, output = NULL, outdir = "dist") {
  2. if (is.null(output)) {
  3. output <- fs::path_file(file)
  4. fs::path_ext(output) <- "rstheme"
  5. }
  6. output <- fs::path(outdir, output)
  7. # cliapp::cli_alert_success("{path {fs::path_rel(file, getwd())}}")
  8. sass::sass(sass::sass_file(paste(file)), output = output)
  9. }
  10. get_theme_name <- function(file) {
  11. x <- readLines(file, warn = FALSE)
  12. x <- grep("rs-theme-name", x, value = TRUE)
  13. if (!length(x)) return("")
  14. x <- sub("^\\s*/\\*\\s*rs-theme-name:\\s", "", x)
  15. x <- sub("\\s*\\*/\\s*$", "", x)
  16. x
  17. }
  18. list_sassy_themes <- function(style = c("all", "light", "dark")) {
  19. if (!requireNamespace("rstudioapi", quietly = TRUE)) {
  20. stop("The {rstudioapi} package is required")
  21. }
  22. if (!rstudioapi::hasFun("getThemes")) {
  23. stop("Please upgrade RStudio to version 1.2+")
  24. }
  25. themes <- rstudioapi::getThemes()
  26. themes <- switch(
  27. match.arg(style),
  28. light = purrr::discard(themes, "isDark"),
  29. dark = purrr::keep(themes, "isDark"),
  30. themes
  31. )
  32. themes <- purrr::map_chr(themes, "name")
  33. themes <- themes[grepl("Sassy", themes)]
  34. unname(themes)
  35. }
  36. cp_to_rstudio <- function(file) {
  37. # cliapp::cli_alert("{arg \"{get_theme_name(file)}\"}")
  38. fs::file_copy(
  39. file,
  40. fs::path_home_r(".R", "rstudio", "themes", fs::path_file(file)),
  41. overwrite = TRUE
  42. )
  43. }
  44. base16_make_theme <- function(palette_file) {
  45. base16_palette <- fs::path_file(palette_file)
  46. base16_info <- base16_get_theme_info(palette_file)
  47. base16_attribution <- base16_info$attribution
  48. base16_name <- base16_info$name
  49. base16_rstudio_style <- base16_info$rstudio_style
  50. base16_template <- if (base16_info$isDark) {
  51. "base16-dark_template.scss"
  52. } else {
  53. "base16-light_template.scss"
  54. }
  55. base16_theme <- whisker::whisker.render(
  56. readLines(here::here("src", "base16", base16_template), warn = FALSE)
  57. )
  58. base16_theme_file <- sub("^_", "", base16_palette)
  59. # cliapp::cli_alert("Creating src/{arg {base16_theme_file}}")
  60. writeLines(base16_theme, fs::path(here::here("src", base16_theme_file)))
  61. }
  62. base16_get_theme_info <- function(palette_file) {
  63. # First and second comment lines of the palette_file must be
  64. # the base16 theme attribution (1) and the studio light/dark style (2)
  65. info <- list()
  66. base16_meta <- readLines(palette_file)
  67. base16_meta <- base16_meta[grepl("^\\s*/\\*", base16_meta)]
  68. info$attribution <- base16_meta[1]
  69. info$rstudio_style <- base16_meta[2]
  70. info$isDark <- grepl("is-dark: TRUE", info$rstudio_style, fixed = TRUE)
  71. if (info$rstudio_style == "") {
  72. info$isDark <- TRUE
  73. info$rstudio_style <- "/* rs-theme-is-dark: TRUE */"
  74. }
  75. rgx_name <- "/\\*\\s*([[:alpha:][:digit:][:punct:] ]+) by"
  76. info$name <- stringr::str_match(info$attribution, rgx_name)[1, 2]
  77. info
  78. }
  79. walk_sassy_themes <- function(style = c("all", "light", "dark"), auto = FALSE) {
  80. themes <- list_sassy_themes(style)
  81. for (theme in themes) {
  82. cliapp::cli_alert(theme)
  83. rstudioapi::applyTheme(theme)
  84. if (auto) {
  85. Sys.sleep(1)
  86. } else {
  87. res <- readline("Press enter for next theme or q to stop...")
  88. if (tolower(res) == "q") return()
  89. }
  90. }
  91. }