Logo

dev-resources.site

for different kinds of informations.

Making Your CLI Applications Pop with Styled Outputs

Published at
1/10/2025
Categories
cli
python
styling
beginners
Author
kelseyroche
Categories
4 categories in total
cli
open
python
open
styling
open
beginners
open
Author
11 person written this
kelseyroche
open
Making Your CLI Applications Pop with Styled Outputs

Console apps don’t have to be boring! Sure, they’re often thought of as plain and functional, but adding a bit of color, bold text, and style can make a world of difference. With Python libraries like colorama and rich, it’s surprisingly easy to give your CLI tools some personality. Let’s check out how you can bring them to life!

Why Style Your CLI Outputs?

  • Improve Usability: Highlight important messages like errors or warnings.

  • Enhance Readability: Use color coding to organize output.

  • Make It Fun: Add personality to your application with creative styling.

colorama

colorama is a lightweight library that enables ANSI color codes on Windows and other platforms, making it a great choice for simple styling needs.

Installation:

pip install colorama

Enter fullscreen mode Exit fullscreen mode

Basic Usage:
Here’s how to use colorama to style your console outputs:

from colorama import Fore, Back, Style, init

Enter fullscreen mode Exit fullscreen mode

Initialize colorama

init()

print(Fore.RED + "This is red text")
print(Back.YELLOW + "This has a yellow background")
print(Style.BRIGHT + "This text is bold")
print(Style.RESET_ALL + "Back to normal style")

Enter fullscreen mode Exit fullscreen mode

Output:

  • Red text
  • Yellow background
  • Bold text
  • Reset to normal

Use Case: Highlight error messages:

print(Fore.RED + "Error: Invalid input!" + Style.RESET_ALL)

Enter fullscreen mode Exit fullscreen mode

rich

If you want more advanced features, like tables, progress bars, and markdown support, rich is an excellent choice.

Installation:

pip install rich

Enter fullscreen mode Exit fullscreen mode

Basic Styling:

from rich.console import Console

console = Console()

console.print("[bold magenta]Hello, World![/bold magenta]")
console.print("[red]Error:[/red] Something went wrong.")
Enter fullscreen mode Exit fullscreen mode

Output:

  • Bold magenta text
  • Red text with “Error” label

Use Case: Formatting a table:

from rich.table import Table

table = Table(title="Sample Table")

table.add_column("Name", style="cyan")
table.add_column("Age", justify="right", style="green")
table.add_row("Alice", "30")
table.add_row("Bob", "25")

console.print(table)
Enter fullscreen mode Exit fullscreen mode

Combining colorama and rich

You can combine the simplicity of colorama for basic styling and the advanced features of rich to create a dynamic CLI experience.

Example:

from colorama import Fore, Style, init
from rich.console import Console
Enter fullscreen mode Exit fullscreen mode

Initialize colorama

init()

Enter fullscreen mode Exit fullscreen mode

Create a rich console

console = Console()

print(Fore.YELLOW + "Welcome to the Styled CLI App!" + Style.RESET_ALL)
console.print("[green]Let's get started![/green]")
Enter fullscreen mode Exit fullscreen mode

Tips for Effective CLI Styling

  • Use Color Consistently: Assign specific colors to warnings, errors, and success messages.
  • Don’t Overdo It: Too much color or bold text can overwhelm users.
  • Test for Readability: Ensure your colors are readable on light and dark terminal themes.
  • Provide a Plain Mode: Offer a way to disable styling for users who prefer plain text.

Conclusion

Styling your CLI outputs can significantly enhance user experience, making your applications more functional and fun. Whether you use colorama for lightweight color support or rich for advanced formatting, Python makes it easy to create visually appealing console tools.

Have you tried adding styles to your CLI apps? Let me know your experiences in the comments!

Resources:
https://pypi.org/project/colorama/
https://www.geeksforgeeks.org/introduction-to-python-colorama/
https://rich.readthedocs.io/en/stable/introduction.html
https://www.geeksforgeeks.org/installing-and-using-rich-package-in-python/

cli Article's
30 articles in total
Favicon
RAG - Creating the SQLite database and config file
Favicon
Simplifying API Routes in Next.js with next-api-gen
Favicon
Modernizing HyperGraph's CLI: A Journey Towards Better Architecture
Favicon
Making Python CLIs More Maintainable: A Journey with Dynamic Command Loading
Favicon
Making Your CLI Applications Pop with Styled Outputs
Favicon
Makefile
Favicon
Can I start and stop Docker Desktop using CLI?
Favicon
How to Install and Run Redis Directly on macOS (Without Homebrew)
Favicon
From iTerm To WezTerm
Favicon
RAG - Designing the CLI interface
Favicon
Configuring Cisco firewall in Linux machine with Minicom
Favicon
KillPy: The Tool to Clean Up Your Python Virtual Environments 🧹🐍
Favicon
Ubuntu Linux Commands Categorized
Favicon
Rust-Powered Password Decrypter: Find the String Behind the Hash! 🦀🔒
Favicon
Mastering Vim and NvChad for Coding and Development: A Comprehensive Guide
Favicon
The Ultimate Cheat Sheet: CLI Man Pages, tldr, and cheat.sh
Favicon
Git Command Line Mastery: 10 Essential Commands for Version Control Using Git
Favicon
Building My First NPM Package: A CLI for Scaffolding Express Servers
Favicon
.NET Development Essentials on macOS with VS Code
Favicon
How to Configure AWS CLI on Linux
Favicon
Building a GitHub Activity CLI - A Ruby Journey
Favicon
Enhanced CIDR Block Calculator with Expanded Input Formats in Go
Favicon
using cat printer on linux
Favicon
Task Tracker CLI
Favicon
Context Dump: Simplifying AI File Preparation
Favicon
MyTask ToDo CLI Tool...
Favicon
I made wut – a CLI that explains your last command with an LLM
Favicon
Magento 2 commands for maintenance and deployment
Favicon
Rusty Journal: A Minimal and Efficient CLI Journal & ToDo App
Favicon
[Boost]

Featured ones: