Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/palette.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
run: curl -Lo palette.json 'https://raw.githubusercontent.com/catppuccin/palette/main/palette.json'

- name: Run codegen
run: uv run --extra matplotlib build.py
run: uv run --extra matplotlib --extra pygments build.py

- name: Create Pull Request
uses: peter-evans/create-pull-request@v8
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ Available IPython themes:
- `catppuccin-macchiato`
- `catppuccin-mocha`

Each theme also has a variant without italics, such as `catppuccin-mocha:noitalic`.

> [!IMPORTANT]
> This requires IPython 9.18 or newer.
>
Expand Down
69 changes: 54 additions & 15 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pathlib import Path
from typing import Any, cast

from catppuccin.extras.pygments import _make_styles
from catppuccin.models import HSL, RGB, Color, Flavor, FlavorColors, Palette

HEADER = '''"""Catppuccin palette definition."""
Expand Down Expand Up @@ -99,16 +100,50 @@ def ipython_theme(flavor: Flavor) -> dict[str, Any]:
}


def codegen() -> str:
"""Generate contents of `catppuccin/palette.py`."""
def ipython_noitalic_theme(flavor: Flavor) -> dict[str, Any]:
"""Create the :noitalic variant of the IPython theme for the given flavor.

This works by loading the existing theme and returning a modified copy with
all `italic` parts replaced with `noitalic. It's done this way rather than
just removing `italic` to ensure it still overrides any italics in the base
style.
"""
theme = ipython_theme(flavor)
noitalic = {
str(token).removeprefix("Token."): " ".join(
"noitalic" if part == "italic" else part for part in style.split()
)
for token, style in _make_styles(flavor.colors).items()
if "italic" in style.split()
}
return {
**theme,
"name": f"{theme['name']}:noitalic",
"extra_style": {**theme["extra_style"], **noitalic},
}


def ipython_theme_filename(name: str) -> str:
"""Return the file that IPython loads for a given theme name.

Practically speaking this means replacing `:` with `-` and appending `.json`.
"""
return name.replace(":", "-") + ".json"


def load_palette() -> Palette:
"""Build the palette from `./palette.json`."""
palette_json = load_palette_json()
palette = Palette(
return Palette(
*[
make_flavor(identifier, fields)
for identifier, fields in palette_json.items()
]
)


def codegen(palette: Palette) -> str:
"""Generate contents of `catppuccin/palette.py`."""
lines = [
HEADER,
f"PALETTE = {palette!r}",
Expand All @@ -118,10 +153,15 @@ def codegen() -> str:


if __name__ == "__main__":
# we build from the latest palette data rather than a stale `catppuccin.PALETTE`.
# `catppuccin.PALETTE` gets rewritten later, but even then we'd have to
# reload a bunch of modules to get it loaded.
palette = load_palette()

print("running codegen")
palette_path = Path.cwd() / "catppuccin" / "palette.py"
with palette_path.open("w", newline="\n") as f:
f.write(codegen())
f.write(codegen(palette))
print("formatting with ruff")
ruff_format = f"ruff format {palette_path}"
subprocess.run(ruff_format.split(), check=True, stdout=subprocess.DEVNULL)
Expand All @@ -130,20 +170,19 @@ def codegen() -> str:
# Generate the matplotlib styles
print("generating matplotlib styles")
from catppuccin.extras.matplotlib import CATPPUCCIN_STYLE_DIRECTORY
from catppuccin.palette import PALETTE

template_text = (
CATPPUCCIN_STYLE_DIRECTORY / "_catppuccin_template.txt"
).read_text()

for key, palette in asdict(PALETTE).items():
for key, flavor in asdict(palette).items():
print(f"- {key}")
text = template_text
text = text.replace("<palette>", key)
for color in palette["colors"]:
for color in flavor["colors"]:
text = text.replace(
f"<{color}>",
palette["colors"][color]["hex"].replace("#", ""),
flavor["colors"][color]["hex"].replace("#", ""),
)
style_path = CATPPUCCIN_STYLE_DIRECTORY / f"{key}.mplstyle"
with style_path.open("w", newline="\n") as f:
Expand All @@ -153,11 +192,11 @@ def codegen() -> str:
# Generate the IPython themes
print("generating ipython themes")
IPYTHON_THEME_DIRECTORY.mkdir(exist_ok=True)
for flavor in PALETTE:
theme = ipython_theme(flavor)
print(f"- {theme['name']}")
theme_path = IPYTHON_THEME_DIRECTORY / f"{theme['name']}.json"
with theme_path.open("w", newline="\n") as f:
json.dump(theme, f, indent=2)
f.write("\n")
for flavor in palette:
for theme in (ipython_theme(flavor), ipython_noitalic_theme(flavor)):
print(f"- {theme['name']}")
theme_path = IPYTHON_THEME_DIRECTORY / ipython_theme_filename(theme["name"])
with theme_path.open("w", newline="\n") as f:
json.dump(theme, f, indent=2)
f.write("\n")
print("ipython themes generation complete")
36 changes: 36 additions & 0 deletions catppuccin/extras/ipython_themes/catppuccin-frappe-noitalic.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "catppuccin-frappe:noitalic",
"base": "catppuccin-frappe",
"extra_style": {
"Prompt": "#a6d189",
"PromptNum": "#a6d189 bold",
"OutPrompt": "#ef9f76",
"OutPromptNum": "#ef9f76 bold",
"Header": "#e78284",
"Topline": "#e78284",
"ExcName": "#e78284 bold",
"Filename": "#8caaee",
"FilenameEm": "#8caaee bold",
"Lineno": "#838ba7",
"LinenoEm": "#babbf1 bold",
"Line": "#c6d0f5",
"TB.Name": "#ca9ee6",
"TB.NameEm": "#ca9ee6 bold",
"VName": "#81c8be",
"ValEm": "#ef9f76 bold",
"Normal": "",
"NormalEm": "#c6d0f5 bold",
"Caret": "",
"TbHighlight": "bg:#51576d",
"Breakpoint": "",
"Breakpoint.Enabled": "#e78284",
"Breakpoint.Disabled": "#838ba7",
"Keyword.Declaration": "#ca9ee6 noitalic",
"Name.Builtin": "#e78284 noitalic",
"Name.Label": "#81c8be noitalic",
"Name.Variable": "#c6d0f5 noitalic",
"Name.Variable.Class": "#e5c890 noitalic",
"Name.Variable.Global": "#c6d0f5 noitalic",
"Name.Variable.Instance": "#c6d0f5 noitalic"
}
}
36 changes: 36 additions & 0 deletions catppuccin/extras/ipython_themes/catppuccin-latte-noitalic.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "catppuccin-latte:noitalic",
"base": "catppuccin-latte",
"extra_style": {
"Prompt": "#40a02b",
"PromptNum": "#40a02b bold",
"OutPrompt": "#fe640b",
"OutPromptNum": "#fe640b bold",
"Header": "#d20f39",
"Topline": "#d20f39",
"ExcName": "#d20f39 bold",
"Filename": "#1e66f5",
"FilenameEm": "#1e66f5 bold",
"Lineno": "#8c8fa1",
"LinenoEm": "#7287fd bold",
"Line": "#4c4f69",
"TB.Name": "#8839ef",
"TB.NameEm": "#8839ef bold",
"VName": "#179299",
"ValEm": "#fe640b bold",
"Normal": "",
"NormalEm": "#4c4f69 bold",
"Caret": "",
"TbHighlight": "bg:#bcc0cc",
"Breakpoint": "",
"Breakpoint.Enabled": "#d20f39",
"Breakpoint.Disabled": "#8c8fa1",
"Keyword.Declaration": "#8839ef noitalic",
"Name.Builtin": "#d20f39 noitalic",
"Name.Label": "#179299 noitalic",
"Name.Variable": "#4c4f69 noitalic",
"Name.Variable.Class": "#df8e1d noitalic",
"Name.Variable.Global": "#4c4f69 noitalic",
"Name.Variable.Instance": "#4c4f69 noitalic"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "catppuccin-macchiato:noitalic",
"base": "catppuccin-macchiato",
"extra_style": {
"Prompt": "#a6da95",
"PromptNum": "#a6da95 bold",
"OutPrompt": "#f5a97f",
"OutPromptNum": "#f5a97f bold",
"Header": "#ed8796",
"Topline": "#ed8796",
"ExcName": "#ed8796 bold",
"Filename": "#8aadf4",
"FilenameEm": "#8aadf4 bold",
"Lineno": "#8087a2",
"LinenoEm": "#b7bdf8 bold",
"Line": "#cad3f5",
"TB.Name": "#c6a0f6",
"TB.NameEm": "#c6a0f6 bold",
"VName": "#8bd5ca",
"ValEm": "#f5a97f bold",
"Normal": "",
"NormalEm": "#cad3f5 bold",
"Caret": "",
"TbHighlight": "bg:#494d64",
"Breakpoint": "",
"Breakpoint.Enabled": "#ed8796",
"Breakpoint.Disabled": "#8087a2",
"Keyword.Declaration": "#c6a0f6 noitalic",
"Name.Builtin": "#ed8796 noitalic",
"Name.Label": "#8bd5ca noitalic",
"Name.Variable": "#cad3f5 noitalic",
"Name.Variable.Class": "#eed49f noitalic",
"Name.Variable.Global": "#cad3f5 noitalic",
"Name.Variable.Instance": "#cad3f5 noitalic"
}
}
36 changes: 36 additions & 0 deletions catppuccin/extras/ipython_themes/catppuccin-mocha-noitalic.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "catppuccin-mocha:noitalic",
"base": "catppuccin-mocha",
"extra_style": {
"Prompt": "#a6e3a1",
"PromptNum": "#a6e3a1 bold",
"OutPrompt": "#fab387",
"OutPromptNum": "#fab387 bold",
"Header": "#f38ba8",
"Topline": "#f38ba8",
"ExcName": "#f38ba8 bold",
"Filename": "#89b4fa",
"FilenameEm": "#89b4fa bold",
"Lineno": "#7f849c",
"LinenoEm": "#b4befe bold",
"Line": "#cdd6f4",
"TB.Name": "#cba6f7",
"TB.NameEm": "#cba6f7 bold",
"VName": "#94e2d5",
"ValEm": "#fab387 bold",
"Normal": "",
"NormalEm": "#cdd6f4 bold",
"Caret": "",
"TbHighlight": "bg:#45475a",
"Breakpoint": "",
"Breakpoint.Enabled": "#f38ba8",
"Breakpoint.Disabled": "#7f849c",
"Keyword.Declaration": "#cba6f7 noitalic",
"Name.Builtin": "#f38ba8 noitalic",
"Name.Label": "#94e2d5 noitalic",
"Name.Variable": "#cdd6f4 noitalic",
"Name.Variable.Class": "#f9e2af noitalic",
"Name.Variable.Global": "#cdd6f4 noitalic",
"Name.Variable.Instance": "#cdd6f4 noitalic"
}
}
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ catppuccin-latte = "catppuccin.extras:ipython_themes"
catppuccin-frappe = "catppuccin.extras:ipython_themes"
catppuccin-macchiato = "catppuccin.extras:ipython_themes"
catppuccin-mocha = "catppuccin.extras:ipython_themes"
"catppuccin-latte:noitalic" = "catppuccin.extras:ipython_themes"
"catppuccin-frappe:noitalic" = "catppuccin.extras:ipython_themes"
"catppuccin-macchiato:noitalic" = "catppuccin.extras:ipython_themes"
"catppuccin-mocha:noitalic" = "catppuccin.extras:ipython_themes"

[project.optional-dependencies]
matplotlib = ["matplotlib>=3.8.4"]
Expand Down
41 changes: 41 additions & 0 deletions tests/test_ipython.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from types import SimpleNamespace

import pytest
from IPython.terminal.interactiveshell import TerminalInteractiveShell
from IPython.utils.PyColorize import theme_table
from pygments.styles import get_style_by_name
from pygments.token import Keyword, Token
Expand All @@ -22,3 +25,41 @@ def test_ipython_theme_is_registered(flavor: Flavor) -> None:
# rest is ipython-specific
assert styles[Token.TbHighlight] == f"bg:{flavor.colors.surface1.hex}"
assert styles[Token.Prompt] == flavor.colors.green.hex


@pytest.mark.parametrize("flavor", list(PALETTE), ids=lambda f: f.identifier)
def test_ipython_noitalic_theme_is_registered(flavor: Flavor) -> None:
base_name = f"catppuccin-{flavor.identifier}"
name = f"{base_name}:noitalic"
assert name in theme_table

theme = theme_table[name]
assert theme.name == name
assert theme.base == base_name

styles = theme.as_pygments_style().styles
italic = {
token: style
for token, style in theme_table[base_name].as_pygments_style().styles.items()
if "italic" in style.split()
}

# sanity check that our style actually has italics in the first place
assert italic

for token, style in italic.items():
assert styles[token] == style.replace("italic", "noitalic")
assert not [token for token, style in styles.items() if "italic" in style.split()]


@pytest.mark.parametrize(
("theme_name", "italic"),
[("catppuccin-mocha", True), ("catppuccin-mocha:noitalic", False)],
)
def test_ipython_input_line_italics(theme_name: str, italic: bool) -> None: # noqa: FBT001
shell = SimpleNamespace(colors=theme_name, highlighting_style_overrides={})
make_style = TerminalInteractiveShell._make_style_from_name_or_cls # noqa: SLF001
style = make_style(shell, "legacy") # type: ignore[no-untyped-call, arg-type]
attrs = style.get_attrs_for_style_str("class:pygments.name.builtin")
assert attrs.italic is italic
assert attrs.color == PALETTE.mocha.colors.red.hex.removeprefix("#")
Loading