def test_args(self): result = colored("hello", "red bold") self.assertEqual(result, "\033[31;1mhello\033[0m") result = colored("hello", "red, bold") self.assertEqual(result, "\033[31;1mhello\033[0m") result = colored("hello", ["red", "bold"]) self.assertEqual(result, "\033[31;1mhello\033[0m")
def test_lines(self): result = colored("hello\nworld", "red", eachline=True) self.assertEqual(result, "\033[31mhello\033[0m\n\033[31mworld\033[0m") result = colored("hello\r\nworld", "red", eachline=True) self.assertEqual(result, "\033[31mhello\033[0m\r\n\033[31mworld\033[0m") result = colored("hello\nworld\n", "red", eachline=True) self.assertEqual(result, "\033[31mhello\033[0m\n\033[31mworld\033[0m\n") with self.assertRaisesRegex(TypeError, r".*positional arguments.*"): colored("hello\nworld", "red", True)
#!/usr/bin/env python3 # Re-implementation of the output of 256colors2.pl by Todd Larason. # This is not a port of the Perl script, but a mimic of the output. from ansicol import colored print('System colors:') for i in range(2): for j in range(8): print(colored(' ', 'on_ansi{}'.format(i * 8 + j)), end='') print() print() print('Color cube, 6x6x6:') for g in range(6): for r in range(6): for b in range(6): print(colored(' ', 'on_rgb{}{}{}'.format(r, g, b)), end='') print(' ', end='') print() print('Grayscale ramp:') for i in range(24): print(colored(' ', 'on_grey{}'.format(i)), end='') print()
def test_arg(self): result = colored("hello", "red") self.assertEqual(result, "\033[31mhello\033[0m")
def test_empty(self): result = colored("", "red") self.assertEqual(result, "")
def test_bad(self): with self.assertRaisesRegex(ValueError, r"invalid attribute name.*"): colored("hello", "infrared")
def test_env(self): with patch.dict("os.environ", {"ANSI_COLORS_DISABLED": "1"}): result = colored("hello", "red") self.assertEqual(result, "hello")