Skip to content

Commit

Permalink
export VGA_COLORS to Python and get rid of other hardcoded themes
Browse files Browse the repository at this point in the history
  • Loading branch information
apparebit committed Aug 14, 2024
1 parent 4a40809 commit 18612ba
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 93 deletions.
7 changes: 3 additions & 4 deletions docs/book.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,11 @@ def test2() -> None:
from prettypretty.color import Color, OkVersion
from prettypretty.color.style import AnsiColor, EmbeddedRgb, Fidelity
from prettypretty.color.style import TerminalColor, TrueColor
from prettypretty.color.trans import Translator
from prettypretty.theme import VGA
red = VGA[AnsiColor.BrightRed.to_8bit() + 2]
from prettypretty.color.trans import Translator, VGA_COLORS
red = VGA_COLORS[AnsiColor.BrightRed.to_8bit() + 2]
assert red == Color.srgb(1.0, 0.333333333333333, 0.333333333333333)

translator = Translator(OkVersion.Revised, VGA)
translator = Translator(OkVersion.Revised, VGA_COLORS)
also_red = translator.resolve(AnsiColor.BrightRed)
assert red == also_red

Expand Down
7 changes: 3 additions & 4 deletions docs/src/overview/integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,11 @@ The Python version is a close match:
~from prettypretty.color import Color, OkVersion
~from prettypretty.color.style import AnsiColor, EmbeddedRgb, Fidelity
~from prettypretty.color.style import TerminalColor, TrueColor
~from prettypretty.color.trans import Translator
~from prettypretty.theme import VGA
red = VGA[AnsiColor.BrightRed.to_8bit() + 2]
~from prettypretty.color.trans import Translator, VGA_COLORS
red = VGA_COLORS[AnsiColor.BrightRed.to_8bit() + 2]
assert red == Color.srgb(1.0, 0.333333333333333, 0.333333333333333)

translator = Translator(OkVersion.Revised, VGA)
translator = Translator(OkVersion.Revised, VGA_COLORS)
also_red = translator.resolve(AnsiColor.BrightRed)
assert red == also_red

Expand Down
3 changes: 3 additions & 0 deletions prettypretty/color/trans.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ from typing import Self
from . import Color, OkVersion, style


VGA_COLORS: list[Color] = ...


class ThemeEntry_Default(ThemeEntry):
"""A theme entry for one of the default colors."""
def __new__(cls, color: style.DefaultColor) -> Self: ...
Expand Down
19 changes: 3 additions & 16 deletions prettypretty/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
from .color.style import ( # pyright: ignore [reportMissingModuleSource]
AnsiColor, EmbeddedRgb, Fidelity, Layer, TrueColor
)
from .theme import MACOS_TERMINAL, VGA, XTERM, current_translator
from .color.trans import VGA_COLORS # pyright: ignore [reportMissingModuleSource]
from .theme import current_translator
from .terminal import Terminal


Expand Down Expand Up @@ -331,24 +332,10 @@ def create_parser() -> argparse.ArgumentParser:
)

group = parser.add_mutually_exclusive_group()
group.add_argument(
'--macos-terminal',
action='store_const',
const=MACOS_TERMINAL,
dest='theme',
help="use the same colors as the Basic theme for macOS Terminal"
)
group.add_argument(
'--xterm',
action='store_const',
const=XTERM,
dest='theme',
help='use the same colors as xterm'
)
group.add_argument(
'--vga',
action='store_const',
const=VGA,
const=VGA_COLORS,
dest='theme',
help='use the same colors as VGA in text mode'
)
Expand Down
4 changes: 2 additions & 2 deletions prettypretty/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
spectrum, # pyright: ignore [reportMissingModuleSource]
trans, # pyright: ignore [reportMissingModuleSource]
)
from .theme import current_translator, VGA
from .theme import current_translator


def create_parser() -> argparse.ArgumentParser:
Expand Down Expand Up @@ -58,7 +58,7 @@ def create_parser() -> argparse.ArgumentParser:
parser.add_argument(
"--vga",
action="store_const",
const=VGA,
const=trans.VGA_COLORS,
dest="theme",
help="use VGA colors instead of querying terminal"
)
Expand Down
69 changes: 2 additions & 67 deletions prettypretty/theme.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,75 +7,10 @@
from contextlib import contextmanager

from .color import Color, OkVersion
from .color.trans import Translator # pyright: ignore [reportMissingModuleSource]
from .color.trans import Translator, VGA_COLORS # pyright: ignore [reportMissingModuleSource]

MACOS_TERMINAL = (
Color.parse("#000000"),
Color.parse("#ffffff"),
Color.parse("#000000"),
Color.parse("#990000"),
Color.parse("#00a600"),
Color.parse("#999900"),
Color.parse("#0000b2"),
Color.parse("#b200b2"),
Color.parse("#00a6b2"),
Color.parse("#bfbfbf"),
Color.parse("#666666"),
Color.parse("#e50000"),
Color.parse("#00d900"),
Color.parse("#e5e500"),
Color.parse("#0000ff"),
Color.parse("#e500e5"),
Color.parse("#00e5e5"),
Color.parse("#e5e5e5"),
)


VGA = (
Color.parse("#000000"),
Color.parse("#ffffff"),
Color.parse("#000000"),
Color.parse("#aa0000"),
Color.parse("#00aa00"),
Color.parse("#aa5500"),
Color.parse("#0000aa"),
Color.parse("#aa00aa"),
Color.parse("#00aaaa"),
Color.parse("#aaaaaa"),
Color.parse("#555555"),
Color.parse("#ff5555"),
Color.parse("#55ff55"),
Color.parse("#ffff55"),
Color.parse("#5555ff"),
Color.parse("#ff55ff"),
Color.parse("#55ffff"),
Color.parse("#ffffff"),
)


XTERM = (
Color.parse("#000000"),
Color.parse("#ffffff"),
Color.parse("#000000"),
Color.parse("#cd0000"),
Color.parse("#00cd00"),
Color.parse("#cdcd00"),
Color.parse("#0000ee"),
Color.parse("#cd00cd"),
Color.parse("#00cdcd"),
Color.parse("#e5e5e5"),
Color.parse("#7f7f7f"),
Color.parse("#ff0000"),
Color.parse("#00ff00"),
Color.parse("#ffff00"),
Color.parse("#5c5cff"),
Color.parse("#ff00ff"),
Color.parse("#00ffff"),
Color.parse("#ffffff"),
)


_current_translator: list[Translator] = [Translator(OkVersion.Revised, VGA)]
_current_translator: list[Translator] = [Translator(OkVersion.Revised, VGA_COLORS)]

@contextmanager
def new_theme(theme_colors: list[Color]) -> Iterator[Translator]:
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ pub fn color(m: &Bound<'_, PyModule>) -> PyResult<()> {
modtrans.add_class::<crate::trans::ThemeEntry>()?;
modtrans.add_class::<crate::trans::ThemeEntryIterator>()?;
modtrans.add_class::<crate::trans::Translator>()?;
modtrans.add("VGA_COLORS", crate::trans::VGA_COLORS)?;
m.add_submodule(&modtrans)?;

// Only change __name__ attribute after submodule has been added.
Expand Down

0 comments on commit 18612ba

Please sign in to comment.