From 6e03ee16a63debd947516cadd2a1eeaa0e5f3183 Mon Sep 17 00:00:00 2001 From: Manitej Boorgu Date: Thu, 6 Aug 2020 05:10:14 +0000 Subject: [PATCH 1/6] Impelmented Everything - All Color Utils worked, tests work good --- README.md | 2 +- src/replit/__init__.py | 7 +-- src/replit/utils/__init__.py | 105 +++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 7 deletions(-) create mode 100644 src/replit/utils/__init__.py diff --git a/README.md b/README.md index 297aa3d..b989200 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Replit-py is a python library designed to be run from a repl on [repl.it](https: - Fully featured database client for Repl DB - Audio library which can play tones and files -- Clear the terminal +- Terminal Utillity library which can create and clear colors better than most libaries ### Documentation diff --git a/src/replit/__init__.py b/src/replit/__init__.py index 9d74f1f..256c40a 100644 --- a/src/replit/__init__.py +++ b/src/replit/__init__.py @@ -1,12 +1,7 @@ """The replit python module.""" from . import maqpy +from . import utils from .audio import Audio from .database import db - -def clear() -> None: - """Clear the terminal.""" - print("\033[H\033[2J", end="", flush=True) - - audio = Audio() diff --git a/src/replit/utils/__init__.py b/src/replit/utils/__init__.py new file mode 100644 index 0000000..fad3be4 --- /dev/null +++ b/src/replit/utils/__init__.py @@ -0,0 +1,105 @@ +# Pure Python ANSI Color Escape Code generator + +def clear() -> None: + """Clear the terminal.""" + print("\033[H\033[2J", end="", flush=True) + +class hexdec(): + def __init__(self, hexvalue : str): + + ''' + Convert Hex Value to RGB + then generate an ANSI escape code + ''' + + hexvalue = hexvalue.lstrip('#') + self.rgb = tuple(int(hexvalue[i:i+2], 16) for i in (0, 2, 4)) + + r, g, b = self.rgb + + self.fg = f'\033[38;2;{r};{g};{b}m' + self.bg = f'\033[48;2;{r};{g};{b}m' + +class rgb(): + def __init__(self, r : int, g : int, b : int): + + ''' + Generate an ANSI escape code for color + + Exceptions: + ValueError + + ''' + + if r < 0 or g < 0 or b < 0: + raise ValueError('RGB Pallete - No Color Support for colors under 0') + if r > 255 or g > 255 or b > 255: + raise ValueError('RGB Pallete - No Color Support for colors over 255') + + self.fg = f'\033[38;2;{r};{g};{b}m' + self.bg = f'\033[48;2;{r};{g};{b}m' + +class bit(): + def __init__(self, value : int): + ''' + Use a 8bit colorpallete + colors from 0-255 (256 total) + Exceptions: + ValueError + ''' + + if value > 255: + raise ValueError('8 Bit Pallete - No Color Support for Colors over 255') + + self.fg = f'\033[38;5;{value}m' + self.bg = f'\033[48;5;{value}m' + +attributes = { #use only repl.it supported ansi codes. Codes such as blink do not work. + 'reset': 0, + 'bold': 1, + 'faint': 2, + 'italic': 3, + 'underline': 4, + 'highlight': 7 +} + +class attr(): + def __init__(self, attrib : str): + ''' + Custom Attributes such as bold and italic + Returns ANSI Escape + Exceptions: + ValueError + ''' + + if attrib in attributes: + self.attr = f'\033[{attributes[attrib]}m' + else: + raise ValueError(f"Attributes - {attrib} is not supported.") + +#predefined variables +reset = attr('reset').attr +bold = attr('bold').attr +italic = attr('italic').attr +red = rgb(255, 0, 0) +orange = rgb(255, 165, 0) +yellow = rgb(255,255,0) +green = rgb(0,255,0) +blue = rgb(0,0,255) +indigo = rgb(75,0,130) +violet = rgb(238,130,238) +purple = rgb(128,0,128) +pink = rgb(255,105,180) +brown = rgb(165,42,42) +brightred = rgb(250,128,114) +brightorange = rgb(255,215,0) +brightyellow = rgb(255, 255, 102) +brightgreen = rgb(102, 255, 102) +brightblue = rgb(102, 178, 255) +brightpurple = rgb(178, 102, 255) +darkred = rgb(139,0,0) +darkorange = rgb(255,140,0) +darkyellow = rgb(204, 204, 0) +darkgreen = rgb(0, 153, 0) +darkblue = rgb(0, 0, 204) +darkpurple = rgb(102, 0, 204) \ No newline at end of file From 2f4b9fb48cf05cbe5fa93b2202e7c740c0d11f37 Mon Sep 17 00:00:00 2001 From: techpixel <68567672+techpixel@users.noreply.github.com> Date: Thu, 6 Aug 2020 00:14:13 -0500 Subject: [PATCH 2/6] Add Backwards Compatibility Support --- src/replit/__init__.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/replit/__init__.py b/src/replit/__init__.py index 256c40a..ce89638 100644 --- a/src/replit/__init__.py +++ b/src/replit/__init__.py @@ -4,4 +4,8 @@ from . import utils from .audio import Audio from .database import db +def clear() -> None: + """Clear the terminal.""" + print("\033[H\033[2J", end="", flush=True) + audio = Audio() From f20c8bb9bcf66dad444d95b2de33ff3e12759326 Mon Sep 17 00:00:00 2001 From: Manitej Boorgu Date: Thu, 6 Aug 2020 23:35:10 +0000 Subject: [PATCH 3/6] Added in Changes --- src/replit/__init__.py | 2 +- src/replit/maqpy/html.py | 44 +++++----- .../{utils/__init__.py => termutils.py} | 86 ++++++++++--------- 3 files changed, 67 insertions(+), 65 deletions(-) rename src/replit/{utils/__init__.py => termutils.py} (56%) diff --git a/src/replit/__init__.py b/src/replit/__init__.py index 256c40a..14f6245 100644 --- a/src/replit/__init__.py +++ b/src/replit/__init__.py @@ -1,6 +1,6 @@ """The replit python module.""" from . import maqpy -from . import utils +from . import termutils from .audio import Audio from .database import db diff --git a/src/replit/maqpy/html.py b/src/replit/maqpy/html.py index 0014bb9..eb877d0 100644 --- a/src/replit/maqpy/html.py +++ b/src/replit/maqpy/html.py @@ -58,29 +58,29 @@ class Link(HTMLElement): class Page(flask.Response): - """Represents an HTML page.""" + """Represents an HTML page.""" - def __init__(self, title: str = None, head: str = "", body: str = "") -> None: - """Initialize the class. + def __init__(self, title: str = None, head: str = "", body: str = "") -> None: + """Initialize the class. - Args: - title (str): The title of the page. If not provided no title tag will be sent. - head (str): The HTML to put in the head of the page. Defaults to nothing. - body (str): The HTML to put in the body of the page. Defaults to nothing. - """ - self.title = title - self.head = head - self.body = body + Args: + title (str): The title of the page. If not provided no title tag will be sent. + head (str): The HTML to put in the head of the page. Defaults to nothing. + body (str): The HTML to put in the body of the page. Defaults to nothing. + """ + self.title = title + self.head = head + self.body = body - title_html = f"{self.title}\n " if self.title else "" - super().__init__( + title_html = f"{self.title}\n " if self.title else "" + super().__init__( f""" - - - {title_html}{self.head} - - - {self.body} - -""" - ) + + + {title_html}{self.head} + + + {self.body} + + """ + ) diff --git a/src/replit/utils/__init__.py b/src/replit/termutils.py similarity index 56% rename from src/replit/utils/__init__.py rename to src/replit/termutils.py index fad3be4..ced73c4 100644 --- a/src/replit/utils/__init__.py +++ b/src/replit/termutils.py @@ -4,23 +4,11 @@ def clear() -> None: """Clear the terminal.""" print("\033[H\033[2J", end="", flush=True) -class hexdec(): - def __init__(self, hexvalue : str): +class color(): + ''' + Dynamic Color: Accepts RGB Color + ''' - ''' - Convert Hex Value to RGB - then generate an ANSI escape code - ''' - - hexvalue = hexvalue.lstrip('#') - self.rgb = tuple(int(hexvalue[i:i+2], 16) for i in (0, 2, 4)) - - r, g, b = self.rgb - - self.fg = f'\033[38;2;{r};{g};{b}m' - self.bg = f'\033[48;2;{r};{g};{b}m' - -class rgb(): def __init__(self, r : int, g : int, b : int): ''' @@ -32,20 +20,34 @@ class rgb(): ''' if r < 0 or g < 0 or b < 0: - raise ValueError('RGB Pallete - No Color Support for colors under 0') + raise ValueError('No Color Support for colors under 0') if r > 255 or g > 255 or b > 255: - raise ValueError('RGB Pallete - No Color Support for colors over 255') + raise ValueError('No Color Support for colors over 255') + self.rgb = (r, g, b) self.fg = f'\033[38;2;{r};{g};{b}m' self.bg = f'\033[48;2;{r};{g};{b}m' + @classmethod + def hexdec(cls, hexvalue : str): + ''' + Convert Hex Value to RGB + then generate an ANSI escape code + ''' + + try: + hexvalue = hexvalue.lstrip('#') + r, g, b = tuple(int(hexvalue[i:i+2], 16) for i in (0, 2, 4)) + except Exception as e: + raise ValueError(f'Error while converting Hex to RGB - {e}') + + return cls(r, g, b) + class bit(): def __init__(self, value : int): ''' Use a 8bit colorpallete colors from 0-255 (256 total) - Exceptions: - ValueError ''' if value > 255: @@ -81,25 +83,25 @@ class attr(): reset = attr('reset').attr bold = attr('bold').attr italic = attr('italic').attr -red = rgb(255, 0, 0) -orange = rgb(255, 165, 0) -yellow = rgb(255,255,0) -green = rgb(0,255,0) -blue = rgb(0,0,255) -indigo = rgb(75,0,130) -violet = rgb(238,130,238) -purple = rgb(128,0,128) -pink = rgb(255,105,180) -brown = rgb(165,42,42) -brightred = rgb(250,128,114) -brightorange = rgb(255,215,0) -brightyellow = rgb(255, 255, 102) -brightgreen = rgb(102, 255, 102) -brightblue = rgb(102, 178, 255) -brightpurple = rgb(178, 102, 255) -darkred = rgb(139,0,0) -darkorange = rgb(255,140,0) -darkyellow = rgb(204, 204, 0) -darkgreen = rgb(0, 153, 0) -darkblue = rgb(0, 0, 204) -darkpurple = rgb(102, 0, 204) \ No newline at end of file +red = color(255, 0, 0) +orange = color(255, 165, 0) +yellow = color(255,255,0) +green = color(0,255,0) +blue = color(0,0,255) +indigo = color(75,0,130) +violet = color(238,130,238) +purple = color(128,0,128) +pink = color(255,105,180) +brown = color(165,42,42) +brightred = color(250,128,114) +brightorange = color(255,215,0) +brightyellow = color(255, 255, 102) +brightgreen = color(102, 255, 102) +brightblue = color(102, 178, 255) +brightpurple = color(178, 102, 255) +darkred = color(139,0,0) +darkorange = color(255,140,0) +darkyellow = color(204, 204, 0) +darkgreen = color(0, 153, 0) +darkblue = color(0, 0, 204) +darkpurple = color(102, 0, 204) From 785ac979b5f474d216856c39e1181064b46db576 Mon Sep 17 00:00:00 2001 From: Manitej Boorgu Date: Fri, 7 Aug 2020 01:55:33 +0000 Subject: [PATCH 4/6] ran flake and black again --- src/replit/termutils.py | 182 +++++++++++++++++++++++----------------- 1 file changed, 105 insertions(+), 77 deletions(-) diff --git a/src/replit/termutils.py b/src/replit/termutils.py index ced73c4..1102a88 100644 --- a/src/replit/termutils.py +++ b/src/replit/termutils.py @@ -1,106 +1,134 @@ -# Pure Python ANSI Color Escape Code generator +"""Pure Python ANSI Color Escape Code generator.""" + def clear() -> None: """Clear the terminal.""" print("\033[H\033[2J", end="", flush=True) -class color(): - ''' - Dynamic Color: Accepts RGB Color - ''' - def __init__(self, r : int, g : int, b : int): +class color: + """Dynamic Color: Accepts RGB Color.""" - ''' - Generate an ANSI escape code for color + def __init__(self, r: int, g: int, b: int) -> None: - Exceptions: - ValueError - - ''' + """ + Generate an ANSI escape code for color - if r < 0 or g < 0 or b < 0: - raise ValueError('No Color Support for colors under 0') - if r > 255 or g > 255 or b > 255: - raise ValueError('No Color Support for colors over 255') + Args: + r (int): Amount of red in color + g (int): Amount of green in color + b (int): Amount of blue in color - self.rgb = (r, g, b) - self.fg = f'\033[38;2;{r};{g};{b}m' - self.bg = f'\033[48;2;{r};{g};{b}m' + Raises: + ValueError + """ - @classmethod - def hexdec(cls, hexvalue : str): - ''' - Convert Hex Value to RGB - then generate an ANSI escape code - ''' + if r < 0 or g < 0 or b < 0: + raise ValueError("No Color Support for colors under 0") + if r > 255 or g > 255 or b > 255: + raise ValueError("No Color Support for colors over 255") - try: - hexvalue = hexvalue.lstrip('#') - r, g, b = tuple(int(hexvalue[i:i+2], 16) for i in (0, 2, 4)) - except Exception as e: - raise ValueError(f'Error while converting Hex to RGB - {e}') + self.rgb = (r, g, b) + self.fg = f"\033[38;2;{r};{g};{b}m" + self.bg = f"\033[48;2;{r};{g};{b}m" - return cls(r, g, b) + @classmethod + def hexdec(cls, hexvalue: str) -> None: + """ + Convert Hex Value to RGB + then generate an ANSI escape code -class bit(): - def __init__(self, value : int): - ''' - Use a 8bit colorpallete - colors from 0-255 (256 total) - ''' + Args: + hexvalue (str): The color's hex value + + Raises: + ValueError - if value > 255: - raise ValueError('8 Bit Pallete - No Color Support for Colors over 255') + Returns: + color : RGB colors from Hex Value + """ - self.fg = f'\033[38;5;{value}m' - self.bg = f'\033[48;5;{value}m' + try: + hexvalue = hexvalue.lstrip("#") + r, g, b = tuple(int(hexvalue[i : i + 2], 16) for i in (0, 2, 4)) + except Exception as e: + raise ValueError(f"Error while converting Hex to RGB - {e}") -attributes = { #use only repl.it supported ansi codes. Codes such as blink do not work. - 'reset': 0, - 'bold': 1, - 'faint': 2, - 'italic': 3, - 'underline': 4, - 'highlight': 7 + return cls(r, g, b) + + +class bit: + def __init__(self, value: int) -> None: + """ + Use a 8bit colorpallete + colors from 0-255 (256 total) + + Args: + value (int): A color value from 0 to 255 + + Raises: + ValueError + """ + + if value > 255: + raise ValueError("8 Bit Pallete - No Color Support for Colors over 255") + if value < 0: + raise ValueError("8 Bit Pallete - No Color Support for colors under 0") + + self.fg = f"\033[38;5;{value}m" + self.bg = f"\033[48;5;{value}m" + + +attributes = { # use only repl.it supported ansi codes. Codes such as blink do not work. + "reset": 0, + "bold": 1, + "faint": 2, + "italic": 3, + "underline": 4, + "highlight": 7, } -class attr(): - def __init__(self, attrib : str): - ''' - Custom Attributes such as bold and italic - Returns ANSI Escape - Exceptions: - ValueError - ''' - if attrib in attributes: - self.attr = f'\033[{attributes[attrib]}m' - else: - raise ValueError(f"Attributes - {attrib} is not supported.") - -#predefined variables -reset = attr('reset').attr -bold = attr('bold').attr -italic = attr('italic').attr +class attr: + def __init__(self, attrib: str) -> None: + """ + Custom Attributes such as bold and italic + Returns ANSI Escape + + Args: + attrib (str): Special Styles for characters + + Raises: + ValueError + """ + + if attrib in attributes: + self.attr = f"\033[{attributes[attrib]}m" + else: + raise ValueError(f"Attributes - {attrib} is not supported.") + + +reset = attr("reset").attr +bold = attr("bold").attr +italic = attr("italic").attr red = color(255, 0, 0) orange = color(255, 165, 0) -yellow = color(255,255,0) -green = color(0,255,0) -blue = color(0,0,255) -indigo = color(75,0,130) -violet = color(238,130,238) -purple = color(128,0,128) -pink = color(255,105,180) -brown = color(165,42,42) -brightred = color(250,128,114) -brightorange = color(255,215,0) +yellow = color(255, 255, 0) +green = color(0, 255, 0) +blue = color(0, 0, 255) +indigo = color(75, 0, 130) +violet = color(238, 130, 238) +purple = color(128, 0, 128) +pink = color(255, 105, 180) +brown = color(165, 42, 42) +brightred = color(250, 128, 114) +brightorange = color(255, 215, 0) brightyellow = color(255, 255, 102) brightgreen = color(102, 255, 102) brightblue = color(102, 178, 255) brightpurple = color(178, 102, 255) -darkred = color(139,0,0) -darkorange = color(255,140,0) +darkred = color(139, 0, 0) +darkorange = color(255, 140, 0) darkyellow = color(204, 204, 0) darkgreen = color(0, 153, 0) darkblue = color(0, 0, 204) From c2a3e5bd72826dc5240bde7b2fbfd01f0172c55a Mon Sep 17 00:00:00 2001 From: Manitej Boorgu Date: Fri, 7 Aug 2020 03:55:03 +0000 Subject: [PATCH 5/6] added in hsv and hls support as classmethods using colorsys --- src/replit/termutils.py | 45 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/src/replit/termutils.py b/src/replit/termutils.py index 1102a88..446b712 100644 --- a/src/replit/termutils.py +++ b/src/replit/termutils.py @@ -1,5 +1,5 @@ """Pure Python ANSI Color Escape Code generator.""" - +import colorsys def clear() -> None: """Clear the terminal.""" @@ -56,6 +56,49 @@ class color: return cls(r, g, b) + @classmethod + def hsv(cls, h, s, v) -> None: + """ + Convert Hex Value to RGB + then generate an ANSI escape code + + Args: + hexvalue (str): The color's hex value + + Raises: + ValueError + + Returns: + color : RGB colors from Hex Value + """ + try: + r, g, b = colorsys.hsv_to_rgb(h, s, v) + except: + raise ValueError('Converting HSV to RGB ran into an error') + + return cls(r, g, b) + + @classmethod + def hls(cls, h, l, s) + """ + Convert Hex Value to RGB + then generate an ANSI escape code + + Args: + hexvalue (str): The color's hex value + + Raises: + ValueError + + Returns: + color : RGB colors from Hex Value + """ + try: + r, g, b = colorsys.hls_to_rgb(h, s, v) + except: + raise ValueError('Converting HLS to RGB ran into an error') + + return cls(r, g, b) class bit: def __init__(self, value: int) -> None: From b1b10eb2d071bf243b51b792722a16824fc7a339 Mon Sep 17 00:00:00 2001 From: Manitej Boorgu Date: Fri, 7 Aug 2020 21:33:17 +0000 Subject: [PATCH 6/6] capitalized class names --- src/replit/termutils.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/replit/termutils.py b/src/replit/termutils.py index 446b712..d51d781 100644 --- a/src/replit/termutils.py +++ b/src/replit/termutils.py @@ -6,7 +6,7 @@ def clear() -> None: print("\033[H\033[2J", end="", flush=True) -class color: +class Color: """Dynamic Color: Accepts RGB Color.""" def __init__(self, r: int, g: int, b: int) -> None: @@ -100,7 +100,7 @@ class color: return cls(r, g, b) -class bit: +class Bit: def __init__(self, value: int) -> None: """ Use a 8bit colorpallete @@ -132,7 +132,7 @@ attributes = { # use only repl.it supported ansi codes. Codes such as blink do } -class attr: +class Attr: def __init__(self, attrib: str) -> None: """ Custom Attributes such as bold and italic