print.bm_bitmap() prints a representation of bitmap objects to the terminal.
It is a wrapper around format.bm_bitmap() which converts bitmap objects
to a character vector.
px_unicode and px_ascii are builtin character vectors intended for use with the px
argument (the former contains Unicode “Block Elements” while the latter is purely ASCII).
px_auto() chooses which character vector to use based on whether cli::is_utf8_output() is TRUE or not.
Usage
# S3 method for class 'bm_bitmap'
print(
x,
...,
px = getOption("bittermelon.px", px_auto()),
fg = getOption("bittermelon.fg", FALSE),
bg = getOption("bittermelon.bg", FALSE),
compress = getOption("bittermelon.compress", "none"),
downscale = getOption("bittermelon.downscale", FALSE)
)
# S3 method for class 'bm_bitmap'
format(
x,
...,
px = getOption("bittermelon.px", px_auto()),
fg = getOption("bittermelon.fg", FALSE),
bg = getOption("bittermelon.bg", FALSE),
compress = getOption("bittermelon.compress", "none"),
downscale = getOption("bittermelon.downscale", FALSE)
)
px_unicode
px_ascii
px_auto(unicode = px_unicode, ascii = px_ascii)Arguments
- x
A
bm_bitmap()object- ...
Further arguments passed to or from other methods.
- px
Character vector of the pixel to use for each integer value i.e. The first character for integer
0L, the second character for integer1L, and so on. Will be recycled.- fg
R color strings of foreground colors to use and/or cli ANSI style functions of class
cli_ansi_style.FALSE(default) for no foreground colors. Will be recycled and passed tocli::make_ansi_style().- bg
R color strings of background colors to use and/or cli ANSI style functions of class
cli_ansi_style.FALSE(default) for no background colors. Will be recycled and passed tocli::make_ansi_style()withbg = TRUE.- compress
If "none" (default) or "n" don't compress first with
bm_compress(). Otherwise compress first withbm_compress()passing the value ofcompressas itsdirectionargument (i.e. either "vertical" or "v", "horizontal" or "h", OR "both" or "b").- downscale
If
TRUEand the printed bitmap will be wider thangetOption("width")then shrink the image to fitgetOption("width")usingbm_downscale().- unicode
Character vector to use if
cli::is_utf8_output()isTRUE.- ascii
Character vector to use if
cli::is_utf8_output()isFALSE.
Value
A character vector of the string representation (print.bm_bitmap() does this invisibly).
As a side effect print.bm_bitmap() prints out the string representation to the terminal.
Fonts and terminal settings
Printing bitmaps/pixmaps may or may not look great in your terminal depending on a variety of factors:
The terminal should support the Unicode - UTF-8 encoding. We use
cli::is_utf8_output()to guess Unicode support which in turn looks atgetOption("cli.unicode")andl10n_info().The terminal should support ANSI sequences and if it does it should support many colors.
We use
cli::num_ansi_colors()to detect number of colors supported.num_ansi_colors()detection algorithm is complicated but it first looks atgetOption("cli.num_colors").
If
cli::num_ansi_colors()equals 16777216 then your terminal supports 24-bit ANSI colors.
If using the Windows Command Prompt window you may need to enable ANSI sequences support by doing
REG ADD HKCU\CONSOLE /f /v VirtualTerminalLevel /t REG_DWORD /d 1from the command-line or runningregedit(Registry Editor) and go toComputer\HKEY_CURRENT_USER\Consoleand setVirtualTerminalLevelto1.
The font used by the terminal should be a monoscale font that supports the Block Elements Unicode block.
The terminal text settings should have a cell spacing around 1.00 times width and 1.00 times height. For terminals configured by CSS styles this means a
line-heightof around1.0.
Examples
font_file <- system.file("fonts/spleen/spleen-8x16.hex.gz", package = "bittermelon")
font <- read_hex(font_file)
bm_R <- font[[str2ucp("R")]]
print(bm_R)
#> ░░░░░░░░
#> ░░░░░░░░
#> ██████░░
#> ██░░░██░
#> ██░░░██░
#> ██░░░██░
#> ██████░░
#> ██░░░██░
#> ██░░░██░
#> ██░░░██░
#> ██░░░██░
#> ██░░░██░
#> ░░░░░░░░
#> ░░░░░░░░
#> ░░░░░░░░
#> ░░░░░░░░
if (cli::is_utf8_output())
print(bm_R, px = px_unicode, compress = "vertical")
#>
#> ██▀▀▀█▄
#> ██ ██
#> ██▀▀▀█▄
#> ██ ██
#> ██ ██
#>
#>
bm_8 <- font[[str2ucp("8")]]
bm_8_with_border <- bm_extend(bm_extend(bm_8, left = 1L),
sides = 1L, value = 2L)
print(bm_8_with_border, px = c(".", "@", "X"))
#> XXXXXXXXXXX
#> X.........X
#> X.........X
#> X..@@@@@..X
#> X.@@...@@.X
#> X.@@...@@.X
#> X.@@...@@.X
#> X..@@@@@..X
#> X.@@...@@.X
#> X.@@...@@.X
#> X.@@...@@.X
#> X.@@...@@.X
#> X..@@@@@..X
#> X.........X
#> X.........X
#> X.........X
#> X.........X
#> XXXXXXXXXXX
if (cli::num_ansi_colors() >= 16L) {
print(bm_8_with_border, px = " ",
bg = c(cli::bg_br_white, cli::bg_blue, cli::bg_red))
}
#>
#>
#>
#>
#>
#>
#>
#>
#>
#>
#>
#>
#>
#>
#>
#>
#>
#>