Skip to content

Commit

Permalink
feat: prepare data structure for codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
backwardspy committed Feb 16, 2024
1 parent 8ab4bf8 commit 9b7ce4a
Show file tree
Hide file tree
Showing 14 changed files with 2,948 additions and 524 deletions.
62 changes: 39 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,42 @@ pip install catppuccin

## Usage

Get access to the palette with the `catppuccin.PALETTE` constant:

```python
>>> from catppuccin import Flavor
>>> Flavor.latte().mauve.hex
'8839ef'
>>> Flavor.mocha().teal.rgb
(148, 226, 213)
from catppuccin import PALETTE

PALETTE.latte.colors.mauve.hex
# '#8839ef'
PALETTE.mocha.colors.teal.rgb
# RGB(r=148, g=226, b=213)
```

`Flavor` is a [`dataclass`](https://docs.python.org/3/library/dataclasses.html),
so you can inspect its fields to get access to the full set of color names and values:
The `Palette` data structure matches [the palette JSON](https://github.com/catppuccin/palette/blob/main/palette.json).

### dataclasses

`Palette`, `Flavor`, `Color` et cetera are all [`dataclasses`](https://docs.python.org/3/library/dataclasses.html),
so you can also inspect and iterate their fields using methods from the dataclass module.

For example, to list all color names and their hex codes:

```python
>>> from dataclasses import fields
>>> flavor = Flavor.frappe()
>>> for field in fields(flavor):
color = getattr(flavor, field.name)
print(f"{field.name}: #{color.hex}")
rosewater: #f2d5cf
flamingo: #eebebe
pink: #f4b8e4
...
base: #303446
mantle: #292c3c
crust: #232634
from dataclasses import fields
from catppuccin import PALETTE

flavor = PALETTE.frappe
for field in fields(flavor.colors):
color = getattr(flavor.colors, field.name)
print(f"{field.name}: {color.hex}")

# rosewater: #f2d5cf
# flamingo: #eebebe
# pink: #f4b8e4
# ...
# base: #303446
# mantle: #292c3c
# crust: #232634
```

## Pygments Styles
Expand All @@ -61,9 +73,10 @@ The styles are registered as importlib entrypoints, which allows Pygments to
find them by name:

```python
>>> from pygments.styles import get_style_by_name
>>> get_style_by_name("catppuccin-frappe")
catppuccin.extras.pygments.FrappeStyle
from pygments.styles import get_style_by_name

get_style_by_name("catppuccin-frappe")
# catppuccin.extras.pygments.FrappeStyle
```

The following style names are available:
Expand All @@ -88,7 +101,10 @@ c.TerminalInteractiveShell.true_color = True
c.TerminalInteractiveShell.highlighting_style = "catppuccin-mocha"
```

Putting this into your [IPython configuration](https://ipython.readthedocs.io/en/stable/config/intro.html) and ensuring `catppuccin[pygments]` is installed in the same environment will give you Catppuccin Mocha syntax highlighting in the REPL. See [here](https://github.com/backwardspy/dots/blob/f6991570d6691212e27e266517656192f910ccbf/dot_config/ipython/profile_default/ipython_config.py) for a more complete example configuration.
Putting this into your [IPython configuration](https://ipython.readthedocs.io/en/stable/config/intro.html)
and ensuring `catppuccin[pygments]` is installed in the same environment will
give you Catppuccin Mocha syntax highlighting in the REPL. See [here](https://github.com/backwardspy/dots/blob/f6991570d6691212e27e266517656192f910ccbf/dot_config/ipython/profile_default/ipython_config.py)
for an example of a more complete configuration.

## Contribution

Expand Down
7 changes: 4 additions & 3 deletions catppuccin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""🐍 Soothing pastel theme for Python."""
from catppuccin.color import Color
from catppuccin.flavor import Flavor

__all__ = ["Color", "Flavor"]

from catppuccin.palette import PALETTE

__all__ = ["PALETTE"]
87 changes: 0 additions & 87 deletions catppuccin/color.py

This file was deleted.

99 changes: 52 additions & 47 deletions catppuccin/extras/pygments.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Pygments styles for all Catppuccin flavors."""
from __future__ import annotations

from typing import TYPE_CHECKING

from pygments.style import Style
from pygments.token import (
Comment,
Expand All @@ -18,82 +20,85 @@
_TokenType,
)

from catppuccin.flavor import Flavor
from catppuccin import PALETTE

if TYPE_CHECKING:
from catppuccin.models import FlavorColors


def _make_styles(flavor: Flavor) -> dict[_TokenType, str]:
def _make_styles(colors: FlavorColors) -> dict[_TokenType, str]:
return {
Token: f"#{flavor.text.hex}",
Text: f"#{flavor.text.hex}",
Error: f"#{flavor.red.hex}",
Keyword: f"#{flavor.mauve.hex}",
Keyword.Constant: f"#{flavor.peach.hex}",
Keyword.Declaration: f"#{flavor.blue.hex}",
Keyword.Namespace: f"#{flavor.teal.hex}",
Keyword.Pseudo: f"#{flavor.mauve.hex}",
Keyword.Reserved: f"#{flavor.mauve.hex}",
Keyword.Type: f"#{flavor.blue.hex}",
Name: f"#{flavor.peach.hex}",
Name.Attribute: f"#{flavor.blue.hex}",
Name.Constant: f"#{flavor.yellow.hex}",
Name.Decorator: f"#{flavor.blue.hex}",
Name.Function: f"#{flavor.blue.hex}",
Name.Function.Magic: f"#{flavor.sky.hex}",
Name.Label: f"#{flavor.blue.hex}",
Name.Tag: f"#{flavor.mauve.hex}",
Literal: f"#{flavor.text.hex}",
String: f"#{flavor.green.hex}",
Number: f"#{flavor.peach.hex}",
Punctuation: f"#{flavor.text.hex}",
Operator: f"#{flavor.sky.hex}",
Comment: f"#{flavor.overlay0.hex}",
Generic.Heading: f"#{flavor.blue.hex} bold",
Token: colors.text.hex,
Text: colors.text.hex,
Error: colors.red.hex,
Keyword: colors.mauve.hex,
Keyword.Constant: colors.peach.hex,
Keyword.Declaration: colors.blue.hex,
Keyword.Namespace: colors.teal.hex,
Keyword.Pseudo: colors.mauve.hex,
Keyword.Reserved: colors.mauve.hex,
Keyword.Type: colors.blue.hex,
Name: colors.peach.hex,
Name.Attribute: colors.blue.hex,
Name.Constant: colors.yellow.hex,
Name.Decorator: colors.blue.hex,
Name.Function: colors.blue.hex,
Name.Function.Magic: colors.sky.hex,
Name.Label: colors.blue.hex,
Name.Tag: colors.mauve.hex,
Literal: colors.text.hex,
String: colors.green.hex,
Number: colors.peach.hex,
Punctuation: colors.text.hex,
Operator: colors.sky.hex,
Comment: colors.overlay0.hex,
Generic.Heading: f"{colors.blue.hex} bold",
}


class LatteStyle(Style):
"""Catppuccin Latte pygments style."""

_flavor = Flavor.latte()
_colors = PALETTE.latte.colors

background_color = f"#{_flavor.base.hex}"
line_number_background_color = f"#{_flavor.mantle.hex}"
line_number_color = f"#{_flavor.text.hex}"
background_color = _colors.base.hex
line_number_background_color = _colors.mantle.hex
line_number_color = _colors.text.hex

styles = _make_styles(_flavor)
styles = _make_styles(_colors)


class FrappeStyle(Style):
"""Catppuccin Frappé pygments style."""

_flavor = Flavor.frappe()
_colors = PALETTE.frappe.colors

background_color = f"#{_flavor.base.hex}"
line_number_background_color = f"#{_flavor.mantle.hex}"
line_number_color = f"#{_flavor.text.hex}"
background_color = _colors.base.hex
line_number_background_color = _colors.mantle.hex
line_number_color = _colors.text.hex

styles = _make_styles(_flavor)
styles = _make_styles(_colors)


class MacchiatoStyle(Style):
"""Catppuccin Macchiato pygments style."""

_flavor = Flavor.macchiato()
_colors = PALETTE.macchiato.colors

background_color = f"#{_flavor.base.hex}"
line_number_background_color = f"#{_flavor.mantle.hex}"
line_number_color = f"#{_flavor.text.hex}"
background_color = _colors.base.hex
line_number_background_color = _colors.mantle.hex
line_number_color = _colors.text.hex

styles = _make_styles(_flavor)
styles = _make_styles(_colors)


class MochaStyle(Style):
"""Catppuccin Mocha pygments style."""

_flavor = Flavor.mocha()
_colors = PALETTE.mocha.colors

background_color = f"#{_flavor.base.hex}"
line_number_background_color = f"#{_flavor.mantle.hex}"
line_number_color = f"#{_flavor.text.hex}"
background_color = _colors.base.hex
line_number_background_color = _colors.mantle.hex
line_number_color = _colors.text.hex

styles = _make_styles(_flavor)
styles = _make_styles(_colors)

0 comments on commit 9b7ce4a

Please sign in to comment.