Learn R Programming

cli

Helpers for Developing Command Line Interfaces

A suite of tools to build attractive command line interfaces (CLIs), from semantic elements: headers, lists, alerts, paragraphs, etc. Supports theming via a CSS-like language. It also contains a number of lower level CLI elements: rules, boxes, trees, and Unicode symbols with ASCII alternatives. It supports ANSI markup for terminal colors and font styles.


Features

  • Build a CLI using semantic elements: headings, lists, alerts, paragraphs.
  • Theming via a CSS-like language.
  • Terminal colors and font styles.
  • All cli text can contain interpreted string literals, via the glue package.
  • Progress bars from R and C code.
  • Error and warning messages with rich text formatting.
  • Support for pluralized messages.
  • ANSI styled string manipulation.

Installation

Install the stable version from CRAN:

install.packages("cli")

Install the development version from GitHub:

pak::pak("r-lib/cli")

Short tour

Some of the more commonly used cli elements, and features.

Short alert messages

One liner messages to inform or warn.

pkgs <- c("foo", "bar", "foobar")
cli_alert_success("Downloaded {length(pkgs)} packages.")
db_url <- "example.com:port"
cli_alert_info("Reopened database {.url {db_url}}.")
cli_alert_warning("Cannot reach GitHub, using local database cache.")
cli_alert_danger("Failed to connect to database.")
cli_alert("A generic alert")

Headings

Three levels of headings.

cli_h1("Heading 1")
cli_h2("Heading 2")
cli_h3("Heading 3")

Lists

Ordered, unordered and description lists, that can be nested.

fun <- function() {
  cli_ol()
  cli_li("Item 1")
  ulid <- cli_ul()
  cli_li("Subitem 1")
  cli_li("Subitem 2")
  cli_end(ulid)
  cli_li("Item 2")
  cli_end()
}
fun()

Themes

Theming via a CSS-like language.

fun <- function() {
  cli_div(theme = list(span.emph = list(color = "orange")))
  cli_text("This is very {.emph important}")
  cli_end()
  cli_text("Back to the {.emph previous theme}")
}
fun()

Command substitution

Automatic command substitution via the glue package.

size <- 123143123
dt <- 1.3454
cli_alert_info(c(
  "Downloaded {prettyunits::pretty_bytes(size)} in ",
  "{prettyunits::pretty_sec(dt)}"))

Pluralization

Pluralization support.

nfiles <- 3
ndirs <- 1
cli_alert_info("Found {nfiles} file{?s} and {ndirs} director{?y/ies}.")

Progress bars

clean <- function() {
  cli_progress_bar("Cleaning data", total = 100)
  for (i in 1:100) {
    Sys.sleep(5/100)
    cli_progress_update()
  }
}
clean()

Documentation

See at https://cli.r-lib.org/ and also in the installed package: help(package = "cli").

Code of Conduct

Please note that the cli project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

License

MIT © Posit Software, PBC

Copy Link

Version

Install

install.packages('cli')

Monthly Downloads

1,513,653

Version

3.6.3

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Last Published

June 21st, 2024

Functions in cli (3.6.3)

ansi_html

Convert ANSI styled text to HTML
ansi_has_any

Check if a string has some ANSI styling
ansi_grep

Like base::grep() and base::grepl(), but for ANSI strings
ansi_align

Align an ANSI colored string
ansi_nzchar

Like base::nzchar(), but for ANSI strings
truecolor

ANSI colors palettes
ansi_html_style

CSS styles for the output of ansi_html()
ansi_strwrap

Wrap an ANSI styled string to a certain width
ansi_nchar

Count number of characters in an ANSI colored string
cli

Compose multiple cli functions
ansi_regex

Perl compatible regular expression that matches ANSI escape sequences
builtin_theme

The built-in CLI theme
ansi_strsplit

Split an ANSI colored string
ansi_simplify

Simplify ANSI styling tags
ansi_substr

Substring(s) of an ANSI colored string
ansi_trimws

Remove leading and/or trailing whitespace from an ANSI string
cat_line

cat() helpers
cli_format

Format a value for printing
ansi_collapse

Collapse a vector into a string scalar
ansi_strtrim

Truncate an ANSI string
list_border_styles

Draw a banner-like box in the console
cli_alert

CLI alerts
ansi_substring

Substring(s) of an ANSI colored string
cli_abort

Signal an error, warning or message with a cli formatted message
cli_code

A block of code
cli_format_method

Create a format method for an object using cli tools
cli_div

Generic CLI container
ansi_toupper

ANSI character translation and case folding
cli_dl

Definition list
ansi_columns

Format a character vector in multiple columns
cli_debug_doc

Debug cli internals
cli_end

Close a CLI container
cli_blockquote

CLI block quote
cli_output_connection

The connection option that cli would use
cli_par

CLI paragraph
cli_progress_bar

cli progress bars
cli_progress_builtin_handlers

cli progress handlers
cli_sitrep

cli situation report
ansi_string

Labels a character vector as containing ANSI control codes.
cli_progress_output

Add text output to a progress bar
cli_progress_step

Simplified cli progress messages, with styling
cli_fmt

Capture the output of cli functions instead of printing it
cli_h1

CLI headings
code_highlight

Syntax highlight R code
ansi_strip

Remove ANSI escape sequences from a string
cli_li

CLI list item(s)
cli_status_clear

Clear the status bar (superseded)
cli_status_update

Update the status bar (superseded)
format_inline

Format and returns a line of text
cli_list_themes

List the currently active themes
cli-config

cli environment variables and options
cli_ol

Ordered CLI list
get_spinner

Character vector to put a spinner on the screen
cli_status

Update the status bar (superseded)
inline-markup

About inline markup in the semantic cli
cli_progress_demo

cli progress bar demo
is_ansi_tty

Detect if a stream support ANSI escape characters
cli_progress_message

Simplified cli progress messages
cli_text

CLI text
pluralization

About cli pluralization
cli_ul

Unordered CLI list
code_theme_list

Syntax highlighting themes
pluralize

String templating with pluralization
cli_vec

Add custom cli style to a vector
cli_verbatim

CLI verbatim text
hash_emoji

Emoji hash
spark_line

Draw a sparkline line graph with Braille characters.
diff_chr

Compare two character vectors elementwise
start_app

Start, stop, query the default cli application
cli-package

cli: Helpers for Developing Command Line Interfaces
test_that_cli

Test cli output with testthat
faq

Frequently Asked Questions
keypress

Read a single keypress at the terminal
diff_str

Compare two character strings, character by character
containers

About cli containers
format_error

Format an error, warning or diagnostic message
demo_spinners

Show a demo of some (by default all) spinners
make_spinner

Create a spinner
themes

About cli themes
is_dynamic_tty

Detect whether a stream supports \\r (Carriage return)
cli_bullets

List of items
is_utf8_output

Whether cli is emitting UTF-8 characters
cli_bullets_raw

List of verbatim items
hash_md5

MD5 hash
cli_process_start

Indicate the start and termination of some computation in the status bar (superseded)
links

cli hyperlinks
list_spinners

List all available spinners
match_selector_node

Match a selector node to a container
parse_selector

Parse a CSS3-like selector
match_selector

Match a selector to a container stack
rule

Make a rule with one or two text labels
no

Pluralization helper functions
pretty_print_code

Turn on pretty-printing functions at the R console
ruler

Print the helpful ruler to the screen
make_ansi_style

Create a new ANSI style
cli_progress_along

Add a progress bar to a mapping function or for loop
cli_progress_styles

List of built-in cli progress styles
num_ansi_colors

Detect the number of ANSI colors to use
spark_bar

Draw a sparkline bar graph with unicode block characters
simple_theme

A simple CLI theme
progress-c

The cli progress C API
cli_rule

CLI horizontal rule
combine_ansi_styles

Combine two or more ANSI styles
console_width

Determine the width of the console
tree

Draw a tree
cli_progress_num

Progress bar utility functions.
unicode-width-workaround

Working around the bad Unicode character widths
has_keypress_support

Check if the current platform/terminal supports reading single keys.
utf8_graphemes

Break an UTF-8 character vector into grapheme clusters
progress-variables

Progress bar variables
hash_animal

Adjective-animal hash
hash_sha1

SHA-1 hash
style_hyperlink

Terminal Hyperlinks
utf8_substr

Substring of an UTF-8 string
hash_sha256

SHA-256 hash
symbol

Various handy symbols to use in a command line UI
utf8_nchar

Count the number of characters in a character vector
vt_output

Simulate (a subset of) a VT-5xx ANSI terminal
ansi_hide_cursor

Hide/show cursor in a terminal
ansi-styles

ANSI colored text