Manage rich themes for CLI applications
Project description
Rich Theme Manager
Description
Implements a basic "theme manager" class for managing rich Themes in your rich CLI application.
The rich package provides an easy way to define custom styles and themes for use with rich. This package provides a simple way to manage the themes: e.g. to list, add, remove themes, for the user to preview themes, and to manage themes on disk.
Synopsis
from rich.console import Console
from rich.style import Style
from rich_theme_manager import Theme, ThemeManager
THEMES = [
Theme(
name="dark",
description="Dark mode theme",
tags=["dark"],
styles={
"error": Style(color="rgb(255,85,85)", bold=True),
"filepath": Style(color="rgb(80,250,123)", bold=True),
"time": Style(color="rgb(139,233,253)", bold=True),
},
),
Theme(
name="light",
description="Light mode theme",
styles={
"error": Style(color="#b31d28", bold=True, underline=True, italic=True),
"filepath": Style(color="#22863a", bold=True),
"time": Style(color="#032f62", bold=True),
},
),
Theme(
name="mono",
description="Monochromatic theme",
tags=["mono", "colorblind"],
styles={
"error": "reverse italic",
"filepath": "bold underline",
"time": "bold",
},
),
]
if __name__ == "__main__":
theme_manager = ThemeManager(themes=THEMES)
theme_manager.list_themes(show_path=False)
dark = theme_manager.get("dark")
theme_manager.preview_theme(dark, show_path=False)
console = Console(theme=THEMES[0])
console.print("[error]Oh No![/error]")
Example app
A simple example app that demonstrates the ThemeManager class comes with rich_theme_manager in __main__.py
:
python -m rich_theme_manager
:
usage: rich_theme_manager [-h] [--example [EXAMPLE]] [--list] [--preview THEME] [--config THEME]
Example CLI usage of rich_theme_manager
optional arguments:
-h, --help show this help message and exit
--example [EXAMPLE] Show example output for theme.
--list List themes.
--preview THEME Preview theme.
--config THEME Print configuration for theme THEME.
python -m rich_theme_manager --list
:
python -m rich_theme_manager --preview dark
:
Documentation
Coming!
In a nutshell, use from rich_theme_manager import Theme
instead of from rich.theme import Theme
and use from rich_theme_manager import ThemeManager
to create a ThemeManager instance.
rich_theme_manager.ThemeManager(theme_dir: Optional[str] = None, themes: Optional[List[Theme]] = None)
If you specify a theme_dir
(for example, using click.get_app_dir), ThemeManager will read/write themes from/to that directory. If you specify a themes
list, ThemeManager will use that list of themes as the default themes and will write the defaults to the theme directory if not already present.
This allows you to easily define default "starting" themes for your application but the user can then edit the theme files (which are INI files created by configparser) to customize the CLI.
Theme subclasses rich.theme.Theme
. You must specify a name and can optionally specify a description, a list of tags, and a path to save the theme to (via Theme.save()
).
Theme(name: str, description: Optional[str] = None, styles: Optional[Mapping[str, StyleType]] = None, inherit: bool = True, tags: Optional[List[str]] = None, path: Optional[str] = None)
License
MIT License
Contributing
Contributions of all kinds are welcome!
Credits
Thank you to Will McGugan for creating rich and helping to make our command line interfaces more beautiful!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for rich_theme_manager-0.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1c63447b886dc9161205530a0d81c5f160b26ab6463a4185c9fa99ae3eb7973d |
|
MD5 | 70f742311ee891cd9e11afe2e0b6e4ad |
|
BLAKE2b-256 | c855843278e89a66a93eb35cb142ace4f810819fdd8b17db53736dc62ec6d8c0 |