🖍️This is a feature-complete clone of the awesome Chalk (JavaScript) library.

Overview



Chalk


Terminal string styling done right

This is a feature-complete clone of the awesome Chalk (JavaScript) library.

All credits go to Sindre Sorhus.

PyPI version Build Status codecov Code style: black mypy license




Highlights

  • Fluent, auto-complete-friendly API for maximum coding efficiency
  • Ability to nest styles
  • Proper handling of styling edge cases (same test cases as Chalk)
  • Auto-detection of terminal color capabilities
  • 256/Truecolor color support, with fallback to basic colors depending on capabilities
  • Same conventions as Chalk to manually control color modes via FORCE_COLOR
  • No dependencies
  • Fully typed (mypy strict), no stubs required

Install

$ pip install yachalk

The only requirement is a modern Python (3.6+).

Usage

from yachalk import chalk

print(chalk.blue("Hello world!"))

Chalk comes with an easy to use composable API where you just chain and nest the styles you want.

from yachalk import chalk

# Combine styled and normal strings
print(chalk.blue("Hello") + " World" + chalk.red("!"))

# Compose multiple styles using the chainable API
print(chalk.blue.bg_red.bold("Hello world!"))

# Use within f-strings
print(f"Found {chalk.bold(num_results)} results.")

# Pass in multiple arguments
print(chalk.blue("Hello", "World!"))

# Nest styles...
print(chalk.red(f"Hello {chalk.underline.bg_blue('world')}!"))

# Nest styles of the same type even (color, underline, background)
print(chalk.green(
    "I am a green line " +
    chalk.blue.underline.bold("with a blue substring") +
    " that becomes green again!"
))

# Use RGB or HEX colors
print(chalk.rgb(123, 45, 67).underline("Underlined reddish color"))
print(chalk.hex("#DEADED").bold("Bold gray!"))

Easily define and re-use your own themes:

from yachalk import chalk

error = chalk.bold.red
warning = chalk.hex("#FFA500")

print(error("Error!"))
print(warning("Warning!"))

Prior art: Why yet another chalk clone?

The Python ecosystem has a large number libraries for terminal styling/coloring. However, after working with Chalk in JavaScript for a while, I always missed to have the same convenience in Python. Inspired by Chalk, I wanted to have a terminal styling library satisfying the following design criteria:

  • Automatic reset handling: Many Python libraries require manual handling of ANSI reset codes. This is error prone, and a common source of coloring issues. It also means that these libraries cannot handle advanced edge cases like proper handling of newlines in all contexts, because that requires internal reset handling.
  • Single symbol import: Some libraries require to import special symbols for foreground/background/modifiers/... depending on the desired styling. This is tedious in my opinion, because you have to adapt the imports all the time when you change the particular styling.
  • Auto-complete friendly: I don't want to memorize a style/color API, I'd like to have full auto-complete support. Some existing Chalk clones seem to generate all style properties dynamically, which means that an IDE cannot support with auto-completion.
  • Support of nested styles: Sometimes it is convenient to embed a style into an existing styled context. With Chalk this simply works. None of the libraries I tried have support of nested styles.
  • Support of edge cases: Chalk has solutions for many edge cases like newline handling, or certain challenges in nested styles. The Python libraries I tried didn't support these. Yachalk is tested against the same test cases as Chalk, so it should support them all.
  • Not print focused: Some libraries provide an API with a focus on offering modified print functions. I prefer the single responsibility principle: Styling should only do styling, and return a string. This still leaves the possibility to print the string, write it to a file, or pass it around freely.
  • True color support: Today most terminal have true color support, so it makes sense to support it in the API. Many older Python libraries only support the basic 16 colors.
  • Capabilities auto detection / fallbacks: Chalk is fully backwards compatible on dumber terminals, by approximating colors with what is available on a particular terminal. I haven't found this feature in existing Python libraries.
  • Zero dependencies: Some libraries are based e.g. based on curses, which is a heavy dependency for something as simple as styling/coloring.
  • Fully typed: I like optional typing, but often library type stubs come with bad types. Yachalk runs in strict mypy mode, which means that no stubs are needed and its type should be correct by design.

I started collecting existing libraries in a feature matrix, but since I keep finding more and more libraries, I've given up on filling it completely 😉 . Nonetheless, feel free to open an issue if it contains an error or misses an important solution.

comparison

Links to related projects:

API

In general there is no need to remember the API, because it is written in a way that it fully auto-completes in common IDEs:

auto_complete

chalk.