-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from mccarthy-m-g/print_style
Add new print style options
- Loading branch information
Showing
19 changed files
with
710 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
#' @importFrom pillar pillar_shaft | ||
#' @export | ||
pillar_shaft.palettes_colour <- function(x, ...) { | ||
# Hex codes should always be printed in tibbles. | ||
op <- options(palettes.print_hex = TRUE) | ||
on.exit(options(op)) | ||
pillar::new_pillar_shaft_simple(colour_format_symbol(x)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,28 @@ | ||
colour_symbol <- function(x) { | ||
style_box <- cli::make_ansi_style(x) | ||
style_box(cli::symbol[getOption("palettes.symbol", default = "bullet")]) | ||
style_box(pal_symbol[getOption("palettes.print_symbol")]) | ||
} | ||
|
||
colour_format_symbol <- function(x, ...) { | ||
vapply( | ||
print_symbol <- !isFALSE(getOption("palettes.print_symbol")) | ||
print_hex <- isTRUE(getOption("palettes.print_hex")) | ||
|
||
out <- purrr::map_chr( | ||
x, | ||
function(x) { | ||
colour_index <- !is.na(x) | ||
if (colour_index) { | ||
paste(colour_symbol(x), format(x)) | ||
function(.x) { | ||
colour_index <- !is.na(.x) | ||
if (print_hex && print_symbol) { | ||
ifelse(colour_index, paste(colour_symbol(.x), format(.x)), "<NA>") | ||
} else if (!print_hex && print_symbol) { | ||
ifelse(colour_index, colour_symbol(.x), "<NA>") | ||
} else if (print_hex && !print_symbol) { | ||
ifelse(colour_index, format(.x), "<NA>") | ||
} else { | ||
" <NA>" | ||
ifelse(colour_index, paste0("\"", .x, "\""), "NA") | ||
} | ||
}, | ||
FUN.VALUE = character(1), | ||
USE.NAMES = FALSE | ||
} | ||
) | ||
|
||
out_width <- max(cli::ansi_nchar(out, type = "width")) | ||
cli::ansi_align(out, width = out_width, align = "right", type = "width") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
#' Symbols to use in colour previews | ||
#' | ||
#' List the symbols available to use in colour previews. | ||
#' | ||
#' @details | ||
#' By default, Unicode characters are used for symbols in colour previews in | ||
#' UTF-8 supported outputs. They automatically fall back to ASCII characters | ||
#' when the output does not support them. | ||
#' | ||
#' To change the symbol used for colour previews, set the `palettes.print_symbol` | ||
#' option to a symbol name listed in `list_colour_symbols()`. | ||
#' @return This function is called for its side effects and has no return value. | ||
#' @seealso `help("palettes-options")`, [cli::is_utf8_output()] | ||
#' @export | ||
#' | ||
#' @examples | ||
#' list_colour_symbols() | ||
list_colour_symbols <- function() { | ||
width <- 50 | ||
cat( | ||
cli::rule( | ||
left = "options(palettes.print_symbol = ...)", | ||
width = width + 10 | ||
), | ||
cli::ansi_columns( | ||
paste0(pal_symbol, "\t", names(pal_symbol)), | ||
width = width, | ||
fill = "cols", | ||
max_cols = 2, | ||
align = "left" | ||
), | ||
sep = "\n" | ||
) | ||
} | ||
|
||
symbol_utf8 <- list( | ||
"circle_small" = "\u2022", | ||
"circle" = "\u25CF", | ||
"circle_medium" = "\u26AB", | ||
"circle_large" = "\u2B24", | ||
|
||
"ellipse_horizontal" = "\u2B2C", | ||
"ellipse_vertical" = "\u2B2E", | ||
|
||
"triangle_up" = "\u25B2", | ||
"triangle_down" = "\u25BC", | ||
"triangle_left" = "\u25C0", | ||
"triangle_right" = "\u25B6", | ||
|
||
"square_small" = "\u25AA", | ||
"square" = "\u25A0", | ||
"square_medium" = "\u25FC", | ||
"square_large" = "\u2B1B", | ||
|
||
"block" = "\u2587", | ||
"block_full" = "\u2588", | ||
|
||
"diamond_small" = "\u2B29", | ||
"diamond" = "\u25C6", | ||
"diamond_medium" = "\u2B25", | ||
|
||
"pentagon" = "\u2B1F", | ||
"hexagon" = "\u2B22", | ||
|
||
"star" = "\u2605", | ||
"heart" = "\u2665", | ||
"smiley" = "\u263A", | ||
"moustache" = "\u0DF4" | ||
) | ||
|
||
symbol_ascii <- list( | ||
"circle_small" = "*", | ||
"circle" = "*", | ||
"circle_medium" = "*", | ||
"circle_large" = "*", | ||
|
||
"ellipse_horizontal" = "*", | ||
"ellipse_vertical" = "*", | ||
|
||
"triangle_up" = "^", | ||
"triangle_down" = "v", | ||
"triangle_left" = "<", | ||
"triangle_right" = ">", | ||
|
||
"square_small" = "[x]", | ||
"square" = "[x]", | ||
"square_medium" = "[x]", | ||
"square_large" = "[x]", | ||
|
||
"block" = "#", | ||
"block_full" = "#", | ||
|
||
"diamond_small" = "[+]", | ||
"diamond" = "[+]", | ||
"diamond_medium" = "[+]", | ||
|
||
"pentagon" = "*", | ||
"hexagon" = "*", | ||
|
||
"star" = "*", | ||
"heart" = "<3", | ||
"smiley" = ":)", | ||
"moustache" = "/\\/" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
## nocov start | ||
|
||
package_env <- environment() | ||
|
||
.onLoad <- function(libname, pkgname) { | ||
op <- options() | ||
op.palettes <- list( | ||
palettes.print_symbol = "circle_small", | ||
palettes.print_hex = TRUE, | ||
palettes.print_alpha = FALSE, | ||
palettes.print_sep = "", | ||
palettes.print_width = 1, | ||
palettes.print_index = FALSE | ||
) | ||
toset <- !(names(op.palettes) %in% names(op)) | ||
if (any(toset)) options(op.palettes[toset]) | ||
|
||
makeActiveBinding( | ||
"pal_symbol", | ||
function() { | ||
if (cli::is_utf8_output()) { | ||
symbol_utf8 | ||
} else { | ||
symbol_ascii | ||
} | ||
}, | ||
package_env | ||
) | ||
|
||
invisible() | ||
} | ||
|
||
## nocov end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.