GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — cli/R/format.R at main · cderv/cli

Expand file tree

/

format.R

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
90 lines (80 loc) · 2.44 KB
/

format.R

Copy path

File metadata and controls

90 lines (80 loc) · 2.44 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#' Format a value for printing
#'
#' This function can be used directly, or via the `{.val ...}` inline
#' style. `{.val {expr}}` calls `cli_format()` automatically on the value
#' of `expr`, before styling and collapsing it.
#'
#' It is possible to define new S3 methods for `cli_format` and then
#' these will be used automatically for `{.val ...}` expressions.
#'
#' @param x The object to format.
#' @param style List of formatting options, see the individual methods
#' for the style options they support.
#' @param ... Additional arguments for methods.
#'
#' @export
#' @seealso [cli_vec()]
#' @examples
#' things <- c(rep("this", 3), "that")
#' cli_format(things)
#' cli_text("{.val {things}}")
#'
#' nums <- 1:5 / 7
#' cli_format(nums, style = list(digits = 2))
#' cli_text("{.val {nums}}")
#' divid <- cli_div(theme = list(.val = list(digits = 3)))
#' cli_text("{.val {nums}}")
#' cli_end(divid)
cli_format <- function(x, style = list(), ...)
UseMethod("cli_format")
#' @rdname cli_format
#' @export
cli_format.default <- function(x, style = list(), ...) {
x
}
#' * Styles for character vectors:
#' - `string_quote` is the quoting character for [encodeString()].
#'
#' @rdname cli_format
#' @export
cli_format.character <- function(x, style = list(), ...) {
quote <- style$string_quote %||% "'"
encodeString(x, quote = quote)
}
#' * Styles for numeric vectors:
#' - `digits` is the number of digits to print after the decimal point.
#'
#' @rdname cli_format
#' @export
cli_format.numeric <- function(x, style = list(), ...) {
digits <- style$digits
if (!is.null(digits)) x <- round(x, digits)
x
}
#' Add custom cli style to a vector
#'
#' @details
#' You can use this function to change the default parameters of
#' [glue::glue_collapse()], see an example below.
#'
#' The style is added as an attribute, so operations that remove
#' attributes will remove the style as well.
#'
#' @param x Vector that will be collapsed by cli.
#' @param style Style to apply to the vector. It is used as a theme on
#' a `span` element that is created for the vector. You can set `vec_sep`
#' and `vec_last` to modify the `sep` and `last` arguments of
#' [glue::glue_collapse()]. See an example below.
#'
#' @export
#' @seealso [cli_format()]
#' @examples
#' v <- cli_vec(
#' c("foo", "bar", "foobar"),
#' style = list(vec_sep = " & ", vec_last = " & ")
#' )
#' cli_text("My list: {v}.")
cli_vec <- function(x, style = list()) {
attr(x, "cli_style") <- style
x
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.