| 1 | from contextlib import contextmanager |
| 2 | |
| 3 | from pytermgui import ( |
| 4 | ColorSystem, |
| 5 | HEXColor, |
| 6 | IndexedColor, |
| 7 | RGBColor, |
| 8 | StandardColor, |
| 9 | str_to_color, |
| 10 | terminal, |
| 11 | ) |
| 12 | |
| 13 | terminal.forced_colorsystem = ColorSystem.TRUE |
| 14 | |
| 15 | |
| 16 | @contextmanager |
| 17 | def set_colorsystem(term, system): |
| 18 | old = term.colorsystem |
| 19 | |
| 20 | try: |
| 21 | term.forced_colorsystem = system |
| 22 | yield |
| 23 | |
| 24 | finally: |
| 25 | term.forced_colorsystem = old |
| 26 | |
| 27 | |
| 28 | def test_fg_simple(): |
| 29 | with set_colorsystem(terminal, ColorSystem.STANDARD): |
| 30 | color = str_to_color("4") |
| 31 | |
| 32 | assert isinstance(color, IndexedColor) |
| 33 | assert color.name == "4" |
| 34 | assert color.sequence == "\x1b[34m" |
| 35 | |
| 36 | |
| 37 | def test_bg_simple(): |
| 38 | with set_colorsystem(terminal, ColorSystem.STANDARD): |
| 39 | color = str_to_color("@2") |
| 40 | |
| 41 | assert isinstance(color, IndexedColor) |
| 42 | assert color.name == "@2" |
| 43 | assert color.sequence == "\x1b[42m" |
| 44 | |
| 45 | |
| 46 | def test_fg_named(): |
| 47 | with set_colorsystem(terminal, ColorSystem.STANDARD): |
| 48 | color = str_to_color("green") |
| 49 | |
| 50 | assert isinstance(color, StandardColor) |
| 51 | assert not color.background |
| 52 | assert color.name == "2" |
| 53 | assert color.sequence == "\x1b[32m" |
| 54 | |
| 55 | |
| 56 | def test_bg_named(): |
| 57 | with set_colorsystem(terminal, ColorSystem.STANDARD): |
| 58 | color = str_to_color("@ansi-bright-blue") |
| 59 | |
| 60 | assert isinstance(color, IndexedColor) |
| 61 | assert color.name == "@12" |
| 62 | assert color.sequence == "\x1b[104m" |
| 63 | |
| 64 | |
| 65 | def test_fg_indexed(): |
| 66 | color = str_to_color("121") |
| 67 | |
| 68 | assert isinstance(color, IndexedColor) |
| 69 | assert color.name == "121" |
| 70 | assert color.sequence == "\x1b[38;5;121m" |
| 71 | assert color.sequence == "\x1b[38;5;121m" |
| 72 | |
| 73 | |
| 74 | def test_bg_indexed(): |
| 75 | color = str_to_color("@96") |
| 76 | |
| 77 | assert isinstance(color, IndexedColor) |
| 78 | assert color.name == "@96" |
| 79 | assert color.sequence == "\x1b[48;5;96m" |
| 80 | |
| 81 | |
| 82 | def test_fg_rgb(): |
| 83 | color = str_to_color("45;123;36") |
| 84 | |
| 85 | assert isinstance(color, RGBColor) |
| 86 | assert color.name == "45;123;36" |
| 87 | assert color.sequence == "\x1b[38;2;45;123;36m" |
| 88 | |
| 89 | |
| 90 | def test_bg_rgb(): |
| 91 | color = str_to_color("@123;23;45") |
| 92 | |
| 93 | assert isinstance(color, RGBColor) |
| 94 | assert color.name == "@123;23;45" |
| 95 | assert color.sequence == "\x1b[48;2;123;23;45m" |
| 96 | |
| 97 | |
| 98 | def test_fg_hex(): |
| 99 | color = str_to_color("#123456") |
| 100 | |
| 101 | assert isinstance(color, HEXColor) |
| 102 | assert color.name == "#123456" |
| 103 | assert color.sequence == "\x1b[38;2;18;52;86m" |
| 104 | assert color.sequence == "\x1b[38;2;18;52;86m" |
| 105 | |
| 106 | |
| 107 | def test_bg_hex(): |
| 108 | color = str_to_color("#abcdef") |
| 109 | |
| 110 | assert isinstance(color, HEXColor) |
| 111 | assert color.name == "#abcdef" |
| 112 | assert color.sequence == "\x1b[38;2;171;205;239m" |
| 113 | assert color.sequence == "\x1b[38;2;171;205;239m" |