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
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

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

from colorama import Fore, Back, Style, init

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")

Output:

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

Use Case: Highlight error messages:

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

rich

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

Installation:

pip install rich

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.")

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)

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

Initialize colorama

init()

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]")

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/

Featured ones: