Skip to content

Commit 6c1d248

Browse files
committed
feat: refactor and add pygments integration
1 parent 279fc38 commit 6c1d248

10 files changed

Lines changed: 181 additions & 28 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ ensure consistency in the codebase.
7373
```bash
7474
isort .
7575
black .
76-
pylint catppuccin.py
76+
pylint catppuccin
7777
mypy .
78-
pytest --cov
78+
pytest --cov catppuccin
7979
```
8080

8181
These tools are all installed as part of the `dev` dependency group with

catppuccin/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"""🐍 Soothing pastel theme for Python."""
2+
from catppuccin.colour import Colour as Colour
3+
from catppuccin.flavour import Flavour as Flavour

catppuccin/colour.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
Functionality relating to individual colours.
3+
"""
4+
from dataclasses import dataclass
5+
from typing import Tuple
6+
7+
8+
@dataclass(frozen=True)
9+
class Colour:
10+
"""A colour with three channels; red, green, and blue."""
11+
12+
red: int
13+
green: int
14+
blue: int
15+
16+
@property
17+
def rgb(self) -> Tuple[int, int, int]:
18+
"""Get the colour as a 3-tuple of red, green, and blue."""
19+
return (self.red, self.green, self.blue)
20+
21+
@property
22+
def hex(self) -> str:
23+
"""Get the colour as a lowercase hex string."""
24+
return f"{self.red:02x}{self.green:02x}{self.blue:02x}"

catppuccin/extras/__init__.py

Whitespace-only changes.

catppuccin/extras/pygments.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
"""
2+
Pygments styles for all Catppuccin flavours.
3+
"""
4+
from pygments.style import Style
5+
from pygments.token import (
6+
Comment,
7+
Error,
8+
Keyword,
9+
Literal,
10+
Name,
11+
Number,
12+
Operator,
13+
Punctuation,
14+
String,
15+
Text,
16+
Token,
17+
_TokenType,
18+
)
19+
20+
from catppuccin.flavour import Flavour
21+
22+
23+
def _make_styles(flavour: Flavour) -> dict[_TokenType, str]:
24+
return {
25+
Token: f"#{flavour.text.hex}",
26+
Text: f"#{flavour.text.hex}",
27+
Error: f"#{flavour.red.hex}",
28+
Keyword: f"#{flavour.mauve.hex}",
29+
Keyword.Constant: f"#{flavour.peach.hex}",
30+
Keyword.Declaration: f"#{flavour.blue.hex}",
31+
Keyword.Namespace: f"#{flavour.teal.hex}",
32+
Keyword.Pseudo: f"#{flavour.mauve.hex}",
33+
Keyword.Reserved: f"#{flavour.mauve.hex}",
34+
Keyword.Type: f"#{flavour.blue.hex}",
35+
Name: f"#{flavour.peach.hex}",
36+
Name.Attribute: f"#{flavour.blue.hex}",
37+
Name.Constant: f"#{flavour.yellow.hex}",
38+
Name.Decorator: f"#{flavour.blue.hex}",
39+
Name.Function: f"#{flavour.blue.hex}",
40+
Name.Function.Magic: f"#{flavour.sky.hex}",
41+
Name.Label: f"#{flavour.blue.hex}",
42+
Name.Tag: f"#{flavour.mauve.hex}",
43+
Literal: f"#{flavour.text.hex}",
44+
String: f"#{flavour.green.hex}",
45+
Number: f"#{flavour.peach.hex}",
46+
Punctuation: f"#{flavour.text.hex}",
47+
Operator: f"#{flavour.sky.hex}",
48+
Comment: f"#{flavour.overlay0.hex}",
49+
}
50+
51+
52+
class LatteStyle(Style): # pylint: disable=too-few-public-methods
53+
"""Catppuccin Latte pygments style."""
54+
55+
styles = _make_styles(Flavour.latte())
56+
57+
58+
class FrappeStyle(Style): # pylint: disable=too-few-public-methods
59+
"""Catppuccin Frappé pygments style."""
60+
61+
styles = _make_styles(Flavour.frappe())
62+
63+
64+
class MacchiatoStyle(Style): # pylint: disable=too-few-public-methods
65+
"""Catppuccin Macchiato pygments style."""
66+
67+
styles = _make_styles(Flavour.macchiato())
68+
69+
70+
class MochaStyle(Style): # pylint: disable=too-few-public-methods
71+
"""Catppuccin Mocha pygments style."""
72+
73+
styles = _make_styles(Flavour.mocha())

catppuccin.py renamed to catppuccin/flavour.py

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,10 @@
1-
"""🐍 Soothing pastel theme for Python."""
1+
"""
2+
Functionality relating to Catppuccin flavours.
3+
A flavour is a collection of colours.
4+
"""
25
from dataclasses import dataclass
3-
from typing import Tuple
46

5-
6-
@dataclass(frozen=True)
7-
class Colour:
8-
"""A colour with three channels; red, green, and blue."""
9-
10-
red: int
11-
green: int
12-
blue: int
13-
14-
@property
15-
def rgb(self) -> Tuple[int, int, int]:
16-
"""Get the colour as a 3-tuple of red, green, and blue."""
17-
return (self.red, self.green, self.blue)
18-
19-
@property
20-
def hex(self) -> str:
21-
"""Get the colour as a lowercase hex string."""
22-
return f"{self.red:02x}{self.green:02x}{self.blue:02x}"
7+
from catppuccin.colour import Colour
238

249

2510
@dataclass(frozen=True)

poetry.lock

Lines changed: 59 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ readme = "README.md"
1111

1212
[tool.poetry.dependencies]
1313
python = "^3.7.2"
14+
pygments = { version = "^2.13.0", optional = true }
15+
16+
[tool.poetry.extras]
17+
pygments = ["pygments"]
1418

1519
[tool.poetry.group.dev.dependencies]
1620
pylint = "^2.15.5"
@@ -19,6 +23,13 @@ black = "^22.10.0"
1923
isort = "^5.10.1"
2024
pytest = "^7.2.0"
2125
pytest-cov = "^4.0.0"
26+
types-setuptools = "^65.5.0.3"
27+
types-pygments = "^2.13.1.1"
28+
29+
[tool.pylint.messages_control]
30+
disable = [
31+
"useless-import-alias", # pyright compatibility
32+
]
2233

23-
[too.isort]
34+
[tool.isort]
2435
profile = "black"

tests/test_colour.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from catppuccin import Colour
1+
from catppuccin.colour import Colour
22

33

44
def test_colour_to_rgb():

tests/test_flavour.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
from typing import cast
22

3-
from catppuccin import Flavour
4-
5-
from .conftest import ColourJSON, FlavourJSON, PaletteJSON
3+
from catppuccin.flavour import Flavour
4+
from tests.conftest import ColourJSON, FlavourJSON, PaletteJSON
65

76

87
def validate_flavour(flavour: Flavour, flavour_json: FlavourJSON) -> None:

0 commit comments

Comments
 (0)