Enhancing Terminal Output with the Rich CLI (with examples)

Enhancing Terminal Output with the Rich CLI (with examples)

Introduction

The command-line interface (CLI) tool, Rich, offers a wide range of features to enhance terminal output. From syntax highlighting and line numbers to custom themes and formatting options, Rich allows developers to create visually appealing and easily readable text in the terminal. In this article, we will explore eight different use cases of the Rich CLI command, along with code examples and demonstrations for each one.

1. Display a File with Syntax Highlighting

Code:

rich path/to/file.py

Motivation:

When working with source code files, it can be beneficial to display them with syntax highlighting. Syntax highlighting improves code readability by visually differentiating between different parts of the code, such as keywords, variables, and comments.

Explanation:

The command rich path/to/file.py will display the content of the specified file (file.py in this case) with syntax highlighting applied. Rich automatically detects the file’s language based on its extension and applies the appropriate syntax highlighting rules.

Example Output:

Syntax Highlighting

2. Add Line Numbers and Indentation Guides

Code:

rich path/to/file.py --line-number --guides

Motivation:

When analyzing source code, it can be useful to have line numbers and indentation guides to visualise the structure of the code. Line numbers help with referencing specific lines during discussions or debugging, while indentation guides help identify code blocks and nesting levels.

Explanation:

By adding the --line-number flag, Rich will display line numbers alongside the content of the file. The --guides flag will add indentation guides, making it easier to visualize the code’s hierarchy.

Example Output:

Line Numbers and Indentation Guides

3. Apply a Theme

Code:

rich path/to/file.py --theme monokai

Motivation:

Applying a theme can dramatically alter the visual appearance of text displayed in the terminal. Themes offer various color schemes and styles that can improve readability and create a personalized environment.

Explanation:

The --theme option allows applying a specific theme to the displayed file. In this example, we use the monokai theme, which is known for its vibrant colors and eye-catching syntax highlighting.

Example Output:

Monokai Theme

4. Display a File in an Interactive Pager

Code:

rich path/to/file.py --pager

Motivation:

When dealing with large files, scrolling through the entire content in the terminal can become tedious. An interactive pager allows navigating through the content more efficiently, providing a better user experience.

Explanation:

The --pager flag enables the interactive pager mode. Instead of displaying the entire file content at once, Rich will display the content page by page, allowing you to scroll up and down using keyboard shortcuts.

Example Output:

Interactive Pager

5. Display Contents from a URL

Code:

rich https://raw.githubusercontent.com/Textualize/rich-cli/main/README.md --markdown --pager

Motivation:

Being able to display the contents from a URL can be useful when you want to read documentation or view files hosted on the web without leaving the terminal.

Explanation:

By providing a URL instead of a file path, Rich can fetch and display the contents of that URL. In this example, we display the README.md file from the Rich CLI GitHub repository. The --markdown flag tells Rich to interpret the content as Markdown, allowing it to apply appropriate formatting.

Example Output:

Display Contents from a URL

6. Export a File as HTML

Code:

rich path/to/file.md --export-html path/to/file.html

Motivation:

Exporting a file as HTML allows sharing formatted text outside of the terminal. It can be used to generate documentation, create reports, or publish content on the web.

Explanation:

By using the --export-html option, Rich can convert the specified Markdown file to an HTML file, ready for further processing or direct web publishing. The output HTML file will preserve the formatting, including syntax highlighting and other visual enhancements.

Example Output:

Assuming path/to/file.md contains Markdown content:

Export a File as HTML

7. Display Text with Formatting Tags, Custom Alignment, and Line Width

Code:

rich --print "Hello [green on black]Stylized[/green on black] [bold]World[/bold]" --right --width 10

Motivation:

Sometimes, displaying simple text with custom formatting options, such as colors and alignment, can help draw attention to specific parts or create a visually pleasing output.

Explanation:

The --print option allows displaying custom text with specified formatting tags. In this example, we display the text “Hello Stylized World” with the word “Stylized” in green on a black background and the word “World” in bold. The --right flag aligns the output to the right, and the --width option sets the maximum line width to 10 characters.

Example Output:

Custom Text Formatting

Conclusion

The Rich CLI tool offers a powerful set of features to enhance terminal output. By utilizing syntax highlighting, line numbers, themes, interactive pagers, and more, developers can improve the readability and visual appeal of text in the terminal. Whether it is displaying code, reading documentation from a URL, or creating HTML exports, Rich provides a toolbox for creating fancy and stylish output in the terminal.

Related Posts

How to use the command 'valgrind' (with examples)

How to use the command 'valgrind' (with examples)

Valgrind is a wrapper for a set of expert tools used for profiling, optimizing, and debugging programs.

Read More
How to use the command k8s-unused-secret-detector (with examples)

How to use the command k8s-unused-secret-detector (with examples)

This article will explain how to use the command “k8s-unused-secret-detector” along with different use cases.

Read More
How to use the command "supervisord" (with examples)

How to use the command "supervisord" (with examples)

Supervisord is a server part of the Supervisor system, which is used for controlling processes on UNIX-like operating systems.

Read More