From b3a39af7d32e044efe52cdd81ff622200c40edbe Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Wed, 13 Jul 2022 09:21:38 +0430 Subject: made it slightly faster which doesnt really matter much --- README.md | 2 +- colo/colo.py | 157 ++++++++++++++++++++++++++++++++----------------------- colo/profiler.py | 6 +++ pyproject.toml | 2 +- 4 files changed, 100 insertions(+), 67 deletions(-) create mode 100755 colo/profiler.py diff --git a/README.md b/README.md index 3b037c3..3c00182 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ A simple script that prints out the 256 terminal colors in different formats.
It can print the numbers, the hex value, rgb,hsi and the ansi escape sequence.
-```sh +```txt colo --help usage: colo [-h] [--ansi] [--hsi] [--rgb] [--number] [--name] [--hex] diff --git a/colo/colo.py b/colo/colo.py index 9247fa6..efb7efb 100644 --- a/colo/colo.py +++ b/colo/colo.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 # _*_ coding=utf-8 _*_ -"""prints colors""" +"""Prints the 256 terminal colors.""" import argparse import html.parser @@ -271,7 +271,7 @@ COLORS = """0Black#000000rgb(0,0,0)hsi(0 class Argparser: # pylint: disable=too-few-public-methods - """argparser class""" + """Argparser class.""" def __init__(self): parser = argparse.ArgumentParser() @@ -297,7 +297,7 @@ class Argparser: # pylint: disable=too-few-public-methods class ColoParser(html.parser.HTMLParser): - """html parser that ignores new lines""" + """Html parser that ignores new lines.""" def handle_data(self, data): if data == "\n": @@ -306,106 +306,130 @@ class ColoParser(html.parser.HTMLParser): colo_list.append(data) -def color_hsi(number_colo_list) -> str: - """print hsi color codes""" - print_list = str() +def color_hsi(number_colo_list) -> None: + """Print hsi color codes.""" hsi_colo_list = [ colo_list[j] for j in range(0, len(colo_list)) if ((j + 1) % 5) == 0 ] for i, number_colo in enumerate(number_colo_list): if i % 6 == 0 and i != 0: - print_list += "\n" - print_list += BASH_STR.replace("XXX", number_colo).replace( - "YYY", hsi_colo_list[i] + print() + print( + "\x1b[38;5;" + + number_colo + + "mcolour" + + number_colo + + " " + + hsi_colo_list[i] + + " \x1b[0m\t", + end="", ) - return print_list -def color_rgb(number_colo_list) -> str: - """print rgb color codes""" - print_list = str() +def color_rgb(number_colo_list) -> None: + """Print rgb color codes.""" rgb_colo_list = [ colo_list[j] for j in range(0, len(colo_list)) if ((j + 1) % 5) == 4 ] for i, number_colo in enumerate(number_colo_list): if i % 6 == 0 and i != 0: - print_list += "\n" - print_list += BASH_STR.replace("XXX", number_colo).replace( - "YYY", rgb_colo_list[i] + print() + print( + "\x1b[38;5;" + + number_colo + + "mcolour" + + number_colo + + " " + + rgb_colo_list[i] + + " \x1b[0m\t", + end="", ) - return print_list -def color_number(number_colo_list) -> str: - """print color numbers""" - print_list = str() +def color_number(number_colo_list) -> None: + """Print color numbers.""" for i, number_colo in enumerate(number_colo_list): if i % 12 == 0 and i != 0: - print_list += "\n" - print_list += BASH_STR.replace("XXX", number_colo).replace( - "YYY", number_colo + print() + print( + "\x1b[38;5;" + + number_colo + + "mcolour" + + number_colo + + " " + + number_colo + + " \x1b[0m\t", + end="", ) - return print_list -def color_name(number_colo_list) -> str: - """print color names""" - print_list = str() +def color_name(number_colo_list) -> None: + """Print color names.""" name_colo_list = [ colo_list[j] for j in range(0, len(colo_list)) if ((j + 1) % 5) == 2 ] for i, number_colo in enumerate(number_colo_list): if i % 6 == 0 and i != 0: - print_list += "\n" - print_list += BASH_STR.replace("XXX", number_colo).replace( - "YYY", name_colo_list[i] + print() + print( + "\x1b[38;5;" + + number_colo + + "mcolour" + + number_colo + + " " + + name_colo_list[i] + + " \x1b[0m\t", + end="", ) - return print_list -def color_hex(number_colo_list) -> str: - """print color hex codes""" - print_list = str() +def color_hex(number_colo_list) -> None: + """Print color hex codes.""" hex_colo_list = [ colo_list[j] for j in range(0, len(colo_list)) if ((j + 1) % 5) == 3 ] for i, number_colo in enumerate(number_colo_list): if i % 8 == 0 and i != 0: - print_list += "\n" - print_list += BASH_STR.replace("XXX", number_colo).replace( - "YYY", hex_colo_list[i] + print() + print( + "\x1b[38;5;" + + number_colo + + "mcolour" + + number_colo + + " " + + hex_colo_list[i] + + " \x1b[0m\t", + end="", ) - return print_list -def color_ansi(number_colo_list) -> str: - """print color ansi escape sequnces""" - print_list = str() +def color_ansi(number_colo_list) -> None: + """Print color ansi escape sequnces.""" for i, number_colo in enumerate(number_colo_list): if i % 6 == 0 and i != 0: - print_list += "\n" - print_list += BASH_STR.replace("XXX", number_colo).replace( - "YYY", BASH_ANSI_STR.replace("XXX", number_colo) + print() + print( + "\x1b[38;5;" + + number_colo + + "mcolour" + + number_colo + + " " + + "\\x1b[38;5;" + + number_colo + + "m" + + " \x1b[0m\t", + end="", ) - return print_list -def color_default(number_colo_list) -> str: - """print numbers only""" - print_list = str() - for i, number_colo in enumerate(number_colo_list): - if i % 12 == 0 and i != 0: - print_list += "\n" - print_list += BASH_STR.replace("XXX", number_colo).replace( - "YYY", number_colo - ) - return print_list +def color_default(number_colo_list) -> None: + """Default action.""" + color_number(number_colo_list) def colo() -> None: - """prints the colors""" + """Print the colors.""" argparser = Argparser() - print_list = str() lines = COLORS.split("\n") for line in lines: parser = ColoParser() @@ -415,17 +439,20 @@ def colo() -> None: ] if argparser.args.hsi: - print_list = color_hsi(number_colo_list) + color_hsi(number_colo_list) elif argparser.args.rgb: - print_list = color_rgb(number_colo_list) + color_rgb(number_colo_list) elif argparser.args.number: - print_list = color_number(number_colo_list) + color_number(number_colo_list) elif argparser.args.name: - print_list = color_name(number_colo_list) + color_name(number_colo_list) elif argparser.args.hex: - print_list = color_hex(number_colo_list) + color_hex(number_colo_list) elif argparser.args.ansi: - print_list = color_ansi(number_colo_list) + color_ansi(number_colo_list) else: - print_list = color_default(number_colo_list) - print(print_list) + color_default(number_colo_list) + + +if __name__ == "__main__": + colo() diff --git a/colo/profiler.py b/colo/profiler.py new file mode 100755 index 0000000..8be31b1 --- /dev/null +++ b/colo/profiler.py @@ -0,0 +1,6 @@ +#!/usr/bin/env python3 + +import cProfile +import colo + +cProfile.run("colo.colo()") diff --git a/pyproject.toml b/pyproject.toml index e18457e..24e3951 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "colo" -version = "1.0.0" +version = "1.1.0" description = "A simple script to print out the 256 terminal colors in various formats" authors = ["terminaldweller "] license = "GPL3" -- cgit v1.2.3