GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing cli.r-lib.org — Building a Semantic CLI IntroductionThe cli package helps you build a command line interface (CLI) without getting lost in details (colors, wrapping, spacing, etc.) of how each piece of the output is formatted. Instead, you can build command line output from semantic elements: lists, alerts, quotes code blocks, headers, etc. The formatting of each element is specified separately, in one or more cli themes. cli comes with a builtin theme, and if you are satisfied with that, then you never need to worry about formatting. A semantic cli is similar to how HTML and CSS work together to create a web site. In this introduction we will go over the functions that create semantic CLI elements, and also some common features of them. Building a command line interfaceTo build a CLI, you can simply start using the AlertsAlerts are typically short messages. cli has four types of alerts (success, info, warning, danger) and also a generic alert type: #> ✔ Updated database.
#> ℹ Reopened database.
#> ! Cannot reach GitHub, using local database cache.
#> ✖ Failed to connect to database.
#> → A generic alert TextText is automatically wrapped to the terminal width. #> Lorem ad ipsum veniam esse nisi deserunt duis. Qui incididunt elit elit mollit #> sint nulla consectetur aute commodo do elit laboris minim et. Laboris ipsum #> mollit voluptate et non do incididunt eiusmod. Anim consectetur mollit laborum #> occaecat eiusmod excepteur. Ullamco non tempor esse anim tempor magna non. Text may have ANSI style markup, ParagraphsParagraphs break the output. The default theme inserts an empty line before and after paragraphs, but only a single empty line is added between two paragraphs. #> This is some text. #> Some more text. #> #> Already a new paragraph. #>
Auto-closing containersIf a paragraph (or other container, see ‘Generic containers’ later),
is opened within a function, cli automatically closes it at the end of
the function, by default. So in the previous example the last
Headingscli supports three levels of headings. This is how they look in the
default theme. The default theme adds an empty line before headings, and
an empty line after #> ── Heading 1 ─────────────────────────────────────────────────────────────────── #>
#> ── Heading 2 ──
#>
#> ── Heading 3 InterpolationAll cli text is treated as a glue template, with special formatters available (see the ‘Inline text formatting’ Section): #> ℹ Downloaded 123.14 MB in 1.3s
Inline text formattingTo define inline markup, you can use the regular glue braces, and
after the opening brace, supply the name of the markup formatter with a
leading dot, e.g. for emphasized text, you use #> • Emphasized text #> • Strong importance #> • A piece of code: `sum(a) / length(a)` #> • A package name: cli #> • A function name: `cli_text()` #> • A keyboard key: press [ENTER] #> • A file name: /usr/bin/env #> • An email address: [email protected] #> • A URL: <https://acme.com> #> • An environment variable: `R_LIBS` #> • Some field To combine inline markup and string interpolation, you need to add another set of braces: #> ✖ Failed to download <https://httpbin.org/status/404>.
#> Some random numbers: 0.07, 0.1, 0.32, and 0.52. Inline lists of itemsWhen cli performs inline text formatting, it automatically collapses glue substitutions, after formatting. This is handy to create lists of files, packages, etc. #> Packages: pkg1, pkg2, and pkg3. #> Packages: pkg1, pkg2, and pkg3 By default class names are collapsed differently: #> Hey `x` has class <POSIXct/POSIXt>
Non-breaking spacesUse #> Qui mollit anim est in deserunt adipisicing nostrud duis enimX deserunt.X Ad #> quiX quiX magna animX esse non anim magnaX Lorem.X Dolore laboreX cupidatat #> magnaX etX officiaX etX esse nullaX eiusmod Lorem #> exercitationX cupidatatX velitX enim. NostrudX elit id laborum #> dolore.X LoremX esse ea sint proident eu officiaX nisiX fugiat mollit in #> LoremX velit. Exercitation ipsum consectetur ad nisiX utX eu. Listscli has three types of list: ordered, unordered and definition lists,
see #> 1. item 1 #> 2. item 2 #> 3. item 3 #> • item 1 #> • item 2 #> • item 3 #> item 1: description 1 #> item 2: description 2 #> item 3: description 3 Item text is wrapped to the terminal width: #> • Officia ad. #> • Minim velit ullamco cupidatat eu ipsum adipisicing ea dolore ipsum consequat #> id irure irure nulla ea nostrud consequat dolor magna commodo proident #> excepteur ullamco est consectetur do quis laboris tempor in laboris Lorem id #> laboris exercitation culpa voluptate deserunt. Adding list items iterativelyItems can be added one by one: #> • Item 1 #> • Item 2 #> • Item 3 The Adding text to an item iteratively
#> • First item #> This is still the first item #> • This is the second item Nested listsTo create nested lists, open nested containers: #> 1. Item 1 #> • Subitem 1 #> • Subitem 2 #> 2. Item 2 In Horizontal rules
#> ── Compiling mypackage ─────────────────────────────────────────────────────────
You can use the usual inline markup in the labels of the rule. The rule’s appearance is specified in the current theme. In particular:
The status barcli supports creating a status bar in the last line of the console, if the terminal supports the carriage return control character to move the cursor to the beginning of the line. This is supported in all terminals, in RStudio, Emacs, RGui, R.app, etc. It is not supported if the output is a file, e.g. typically on CI systems.
While it is possible to create multiple status bars, on a typical terminal only one of them can be shown at any time. cli by default shows the one that was last created or updated. While the status bar is active, cli can still produce output, as normal. This output is created “above” the status bar, which is always kept in the last line of the screen. See the following example: ThemingThe looks of the various CLI elements can be changed via themes. The cli package comes with a simple built-in theme, and new themes can be added as well. Tags, ids and classesSimilarly to HTML document, the elements of a CLI form a tree of
nodes. Each node has exactly one tag, at most one id, and optionally a
set of classes. E.g.
A cli theme is a named list, where the names are selectors based on
tag names, ids and classes, and the elements of the list are style
declarations. For example, the style of #> $`font-weight` #> [1] "bold" #> #> $`margin-top` #> [1] 1 #> #> $`margin-bottom` #> [1] 0 #> #> $fmt #> function (x) #> cli::rule(x, line_col = "cyan") #> <bytecode: 0x56128593acb8> #> <environment: 0x56128395fb88> #> See also Generic containers
#> → This will be red
#> → Back to normal color
Theming inline markupThe inline markup formatters always use a #> This is very important #> Back to the previous theme In addition to adding inline markup explicitly, like
#> Found some files: file1, file2, and file3. CLI messagesAll When a cli function is called:
#> <cli_message: cli message h1> Sub-ProcessesIf #> This is sub-process 16615 from callr #> [1] 16615 Utility functionsANSI colorscli has functions to create ANSI colored or styled output in the
console. These functions concatenate their arguments using
#> This is red.
Foreground colors: #> • black #> • blue #> • cyan #> • green #> • magenta #> • red #> • white #> • yellow #> • grey Note that these might actually look different depending on your terminal theme. Background colors: #> • black background #> • blue background #> • cyan background #> • green background #> • magenta background #> • red background #> • white background #> • yellow background Text styles: #> • dim style #> • blurred style #> • bold style #> • hidden style #> • inverse style #> • italic style #> • reset style #> • strikethrough style #> • underline style Not all Colors, background colors and styles can be combined: #> <cli_ansi_string>
#> [1] TITLE
#> <cli_ansi_string>
#> [1] This is a warning in pink!
#>
Console capabilitiesQuery the console width: #> [1] 80 Query if the console supports ansi escapes: #> [1] TRUE Hide the cursor, if the console supports it (no-op otherwise): See also Query if the console supports #> [1] TRUE Query if the console supports UTF-8 output: #> [1] TRUE Unicode charactersThe #> ✔ no errors | ✖ 2 warnings Here is a list of all symbols: #> ✔tick ≠neq #> ✖cross ≥geq #> ★star ≤leq #> ▇square ×times #> ◻square_small ▔upper_block_1 #> ◼square_small_filled ▀upper_block_4 #> ◯circle ▁lower_block_1 #> ◉circle_filled ▂lower_block_2 #> ◌circle_dotted ▃lower_block_3 #> ◎circle_double ▄lower_block_4 #> ⓞcircle_circle ▅lower_block_5 #> ⓧcircle_cross ▆lower_block_6 #> Ⓘcircle_pipe ▇lower_block_7 #> ?⃝circle_question_mark █lower_block_8 #> •bullet █full_block #> ․dot ⁰sup_0 #> ─line ¹sup_1 #> ═double_line ²sup_2 #> …ellipsis ³sup_3 #> …continue ⁴sup_4 #> ❯pointer ⁵sup_5 #> ℹinfo ⁶sup_6 #> ⚠warning ⁷sup_7 #> ☰menu ⁸sup_8 #> ☺smiley ⁹sup_9 #> ෴mustache ⁻sup_minus #> ♥heart ⁺sup_plus #> ↑arrow_up ▶play #> ↓arrow_down ■stop #> ←arrow_left ●record #> →arrow_right ‒figure_dash #> ◉radio_on –en_dash #> ◯radio_off —em_dash #> ☒checkbox_on “dquote_left #> ☐checkbox_off ”dquote_right #> ⓧcheckbox_circle_on ‘squote_left #> Ⓘcheckbox_circle_off ’squote_right #> ❓fancy_question_mark Most symbols were inspired by (and copied from) the awesome figures JavaScript project. SpinnersSee #> [1] "dots" "dots2" "dots3" #> [4] "dots4" "dots5" "dots6" #> [7] "dots7" "dots8" "dots9" #> [10] "dots10" "dots11" "dots12" #> [13] "dots13" "dots8Bit" "sand" #> [16] "line" "line2" "pipe" #> [19] "simpleDots" "simpleDotsScrolling" "star" #> [22] "star2" "flip" "hamburger" #> [25] "growVertical" "growHorizontal" "balloon" #> [28] "balloon2" "noise" "bounce" #> [31] "boxBounce" "boxBounce2" "triangle" #> [34] "arc" "circle" "squareCorners" #> [37] "circleQuarters" "circleHalves" "squish" #> [40] "toggle" "toggle2" "toggle3" #> [43] "toggle4" "toggle5" "toggle6" #> [46] "toggle7" "toggle8" "toggle9" #> [49] "toggle10" "toggle11" "toggle12" #> [52] "toggle13" "arrow" "arrow2" #> [55] "arrow3" "bouncingBar" "bouncingBall" #> [58] "smiley" "monkey" "hearts" #> [61] "clock" "earth" "material" #> [64] "moon" "runner" "pong" #> [67] "shark" "dqpb" "weather" #> [70] "christmas" "grenade" "point" #> [73] "layer" "betaWave" "fingerDance" #> [76] "fistBump" "soccerHeader" "mindblown" #> [79] "speaker" "orangePulse" "bluePulse" #> [82] "orangeBluePulse" "timeTravel" "aesthetic" #> [85] "growVeriticalDotsLR" "growVeriticalDotsRL" "growVeriticalDotsLL" #> [88] "growVeriticalDotsRR" #> $name #> [1] "dots" #> #> $interval #> [1] 80 #> #> $frames #> [1] "⠋" "⠙" "⠹" "⠸" "⠼" "⠴" "⠦" "⠧" "⠇" "⠏" #> |
| You are visitor 01402775 — 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. |