Skip to content

Commit

Permalink
Add styling options
Browse files Browse the repository at this point in the history
  • Loading branch information
sdispater committed Dec 16, 2019
1 parent 0588ab7 commit af19b70
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 15 deletions.
5 changes: 3 additions & 2 deletions pastel/pastel.py
Expand Up @@ -10,7 +10,7 @@

class Pastel(object):

TAG_REGEX = "[a-z][a-z0-9_=;-]*"
TAG_REGEX = "[a-z][a-z0-9,_=;-]*"
FULL_TAG_REGEX = re.compile("(?isx)<(({}) | /({})?)>".format(TAG_REGEX, TAG_REGEX))

def __init__(self, colorized=False):
Expand Down Expand Up @@ -138,7 +138,8 @@ def _create_style_from_string(self, string):
style.set_background(match[1])
else:
try:
style.set_option(match[1])
for option in match[1].split(","):
style.set_option(option.strip())
except ValueError:
return False

Expand Down
32 changes: 29 additions & 3 deletions pastel/style.py
Expand Up @@ -13,7 +13,16 @@ class Style(object):
"blue": 34,
"magenta": 35,
"cyan": 36,
"white": 37,
"light_gray": 37,
"default": 39,
"dark_gray": 90,
"light_red": 91,
"light_green": 92,
"light_yellow": 93,
"light_blue": 94,
"light_magenta": 95,
"light_cyan": 96,
"white": 97,
}

BACKGROUND_COLORS = {
Expand All @@ -24,10 +33,27 @@ class Style(object):
"blue": 44,
"magenta": 45,
"cyan": 46,
"white": 47,
"light_gray": 47,
"default": 49,
"dark_gray": 100,
"light_red": 101,
"light_green": 102,
"light_yellow": 103,
"light_blue": 104,
"light_magenta": 105,
"light_cyan": 106,
"white": 107,
}

OPTIONS = {"bold": 1, "underscore": 4, "blink": 5, "reverse": 7, "conceal": 8}
OPTIONS = {
"bold": 1,
"dark": 2,
"italic": 3,
"underline": 4,
"blink": 5,
"reverse": 7,
"conceal": 8,
}

def __init__(self, foreground=None, background=None, options=None):
self._fg = foreground
Expand Down
14 changes: 7 additions & 7 deletions tests/test_pastel.py
Expand Up @@ -21,7 +21,7 @@ def test_bundled_styles(pastel):
assert pastel.has_style("comment")
assert pastel.has_style("question")

assert "\033[37;41msome error\033[0m" == pastel.colorize(
assert "\033[97;41msome error\033[0m" == pastel.colorize(
"<error>some error</error>"
)
assert "\033[32msome info\033[0m" == pastel.colorize("<info>some info</info>")
Expand All @@ -35,13 +35,13 @@ def test_bundled_styles(pastel):

def test_nested_styles(pastel):
assert (
"\033[37;41msome \033[0m\033[32msome info\033[0m\033[37;41m error\033[0m"
"\033[97;41msome \033[0m\033[32msome info\033[0m\033[97;41m error\033[0m"
== pastel.colorize("<error>some <info>some info</info> error</error>")
)


def test_adjacent_style(pastel):
assert "\033[37;41msome error\033[0m\033[32msome info\033[0m" == pastel.colorize(
assert "\033[97;41msome error\033[0m\033[32msome info\033[0m" == pastel.colorize(
"<error>some error</error><info>some info</info>"
)

Expand All @@ -58,7 +58,7 @@ def test_style_escaping(pastel):

def test_deep_nested_style(pastel):
assert (
"\033[37;41merror\033[0m\033[32minfo\033[0m\033[33mcomment\033[0m\033[37;41merror\033[0m"
"\033[97;41merror\033[0m\033[32minfo\033[0m\033[33mcomment\033[0m\033[97;41merror\033[0m"
== pastel.colorize(
"<error>error<info>info<comment>comment</comment></info>error</error>"
)
Expand All @@ -73,7 +73,7 @@ def test_new_style(pastel):
pastel.add_style("b", "blue", "white")

assert (
"\033[34;47msome \033[0m\033[34;47mcustom\033[0m\033[34;47m msg\033[0m"
"\033[34;107msome \033[0m\033[34;107mcustom\033[0m\033[34;107m msg\033[0m"
== pastel.colorize("<test>some <b>custom</b> msg</test>")
)

Expand All @@ -91,7 +91,7 @@ def test_new_style(pastel):
def test_redefined_style(pastel):
pastel.add_style("info", "blue", "white")

assert "\033[34;47msome custom msg\033[0m" == pastel.colorize(
assert "\033[34;107msome custom msg\033[0m" == pastel.colorize(
"<info>some custom msg</info>"
)

Expand Down Expand Up @@ -134,7 +134,7 @@ def test_non_decorated_pastel(non_decorated_pastel):

pastel.with_colors(True)

assert "\033[37;41msome error\033[0m" == pastel.colorize(
assert "\033[97;41msome error\033[0m" == pastel.colorize(
"<error>some error</error>"
)
assert "\033[32msome info\033[0m" == pastel.colorize("<info>some info</info>")
Expand Down
6 changes: 3 additions & 3 deletions tests/test_style.py
Expand Up @@ -6,17 +6,17 @@


def test_init():
style = Style("green", "black", ["bold", "underscore"])
style = Style("green", "black", ["bold", "underline"])
assert "\033[32;40;1;4mfoo\033[0m" == style.apply("foo")
assert "green" == style.foreground
assert "black" == style.background
assert ["bold", "underscore"] == style.options
assert ["bold", "underline"] == style.options

style = Style("red", None, ["blink"])
assert "\033[31;5mfoo\033[0m" == style.apply("foo")

style = Style(None, "white")
assert "\033[47mfoo\033[0m" == style.apply("foo")
assert "\033[107mfoo\033[0m" == style.apply("foo")

style = Style("red", None, "blink")
assert "\033[31;5mfoo\033[0m" == style.apply("foo")
Expand Down

0 comments on commit af19b70

Please sign in to comment.