Skip to content
Draft
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
14 changes: 11 additions & 3 deletions colorama/ansi.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,15 @@ class AnsiStyle(AnsiCodes):
NORMAL = 22
RESET_ALL = 0

Fore = AnsiFore()
Back = AnsiBack()
Style = AnsiStyle()

def _make_ansi_codes_type(codes):
"""Return a type whose attributes are the rendered values from *codes*."""
attributes = vars(codes).copy()
attributes['__module__'] = __name__
return type(codes.__class__.__name__, (), attributes)


Fore = _make_ansi_codes_type(AnsiFore())
Back = _make_ansi_codes_type(AnsiBack())
Style = _make_ansi_codes_type(AnsiStyle())
Cursor = AnsiCursor()
10 changes: 10 additions & 0 deletions colorama/tests/ansi_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright Jonathan Hartley 2013. BSD 3-Clause license, see LICENSE file.
import sys
from typing import Optional
from unittest import TestCase, main

from ..ansi import Back, Fore, Style
Expand Down Expand Up @@ -66,6 +67,15 @@ def testBackAttributes(self):
self.assertEqual(Back.LIGHTWHITE_EX, '\033[107m')


def testAnsiCodesCanBeUsedAsTypingTypes(self):
self.assertIsInstance(Back, type)

def colored_background(color: Optional[Back] = Back.RED):
return color

self.assertEqual(colored_background(), Back.RED)


def testStyleAttributes(self):
self.assertEqual(Style.DIM, '\033[2m')
self.assertEqual(Style.NORMAL, '\033[22m')
Expand Down