From ff2b168ae720a70d828dde569fbae4ec016da577 Mon Sep 17 00:00:00 2001 From: Giovane Costa Date: Wed, 8 Nov 2023 11:18:02 -0300 Subject: [PATCH] Implement inverted --- .../adapters/console/console_adapter.py | 8 +++++--- .../adapters/git_mosaic/git_mosaic_adapter.py | 4 ++-- .../charmaps/basic/basic_charmap.py | 19 +++++++++---------- .../usecases/write_in_console.py | 4 ++-- .../usecases/write_mosaic.py | 10 ++++++---- tests/charmaps/test_basic_charmap.py | 16 ++++++++-------- 6 files changed, 32 insertions(+), 29 deletions(-) diff --git a/src/awesome_git_mosaic/adapters/console/console_adapter.py b/src/awesome_git_mosaic/adapters/console/console_adapter.py index 2a18a0a..c01b9e9 100644 --- a/src/awesome_git_mosaic/adapters/console/console_adapter.py +++ b/src/awesome_git_mosaic/adapters/console/console_adapter.py @@ -8,21 +8,23 @@ CONSOLE_PIXEL = "▉" CONSOLE_SPACE = " " +CONSOLE_BG = '░' class ConsoleAdapter: def __init__(self, charmap: Optional["Charmap"] = None): self.charmap = charmap or BasicCharmap() - def output(self, message: str, with_spaces: bool = True) -> str: - lines = self.charmap.translate(message, with_spaces) + def output(self, message: str, with_spaces: bool = True, background: bool = False, inverted: bool = False) -> str: + lines = self.charmap.translate(message, with_spaces, inverted) + bg = CONSOLE_BG if background else CONSOLE_SPACE output = [] for line in lines: output_line = "" for char in line: output_line += ( - CONSOLE_PIXEL if self.charmap.is_pixel(char) else CONSOLE_SPACE + CONSOLE_PIXEL if self.charmap.is_pixel(char) else bg ) output.append(output_line) diff --git a/src/awesome_git_mosaic/adapters/git_mosaic/git_mosaic_adapter.py b/src/awesome_git_mosaic/adapters/git_mosaic/git_mosaic_adapter.py index 5236bdd..ffa5f40 100644 --- a/src/awesome_git_mosaic/adapters/git_mosaic/git_mosaic_adapter.py +++ b/src/awesome_git_mosaic/adapters/git_mosaic/git_mosaic_adapter.py @@ -17,10 +17,10 @@ def output( message: str, reference_day: Optional[datetime] = None, with_spaces: bool = True, - background: bool = False, + inverted: bool = False, ) -> List[datetime]: reference_day = reference_day or datetime.today() - lines = self.charmap.translate(message, with_spaces, background) + lines = self.charmap.translate(message, with_spaces, inverted) output = [] y_offset = 0 diff --git a/src/awesome_git_mosaic/charmaps/basic/basic_charmap.py b/src/awesome_git_mosaic/charmaps/basic/basic_charmap.py index 229e2a8..8fd4ce5 100644 --- a/src/awesome_git_mosaic/charmaps/basic/basic_charmap.py +++ b/src/awesome_git_mosaic/charmaps/basic/basic_charmap.py @@ -16,6 +16,7 @@ def __init__( char_height: int = 7, char_list: str = " abcdefghijklmnopqrstuvwxyz0123456789#", ) -> None: + self.char_height = char_height if not model_file: current_dir = path.dirname(path.abspath(__file__)) model_file = path.join(current_dir, "char_model.txt") @@ -23,20 +24,18 @@ def __init__( model_file, char_width, char_height, char_list ) - def translate( - self, string: str, with_spaces: bool = True, background: bool = False - ) -> list: + def translate(self, string: str, with_spaces: bool = True, inverted: bool = False) -> list: string = unidecode(string).lower() mapped_chars = [self.chars[c] for c in string] - if with_spaces: - space = CHARMAP_PIXEL if background else " " - else: - space = "" - output = [] + space = " " if with_spaces else "" - for line in range(len(mapped_chars[0])): - output.append(space.join(["".join(char[line]) for char in mapped_chars])) + output = [] + for line in range(self.char_height): + line_str = space.join(["".join(char[line]) for char in mapped_chars]) + if inverted: + line_str = line_str.translate(str.maketrans(f"{CHARMAP_PIXEL} ", f" {CHARMAP_PIXEL}")) + output.append(line_str) return output diff --git a/src/awesome_git_mosaic/usecases/write_in_console.py b/src/awesome_git_mosaic/usecases/write_in_console.py index c7da4bc..fa1398c 100644 --- a/src/awesome_git_mosaic/usecases/write_in_console.py +++ b/src/awesome_git_mosaic/usecases/write_in_console.py @@ -5,6 +5,6 @@ class WriteInConsole: def __init__(self, console_adapter: ConsoleAdapter = None): self.console_adapter = console_adapter or ConsoleAdapter() - def write(self, text: str, with_spaces: bool = True): - output = self.console_adapter.output(text, with_spaces) + def write(self, text: str, with_spaces: bool = True, background: bool = False, inverted: bool = False): + output = self.console_adapter.output(text, with_spaces, background, inverted) print(output) diff --git a/src/awesome_git_mosaic/usecases/write_mosaic.py b/src/awesome_git_mosaic/usecases/write_mosaic.py index c4d5e30..9bf40a4 100644 --- a/src/awesome_git_mosaic/usecases/write_mosaic.py +++ b/src/awesome_git_mosaic/usecases/write_mosaic.py @@ -22,17 +22,19 @@ def write( message: str, strength: int = 15, multiply: int = 1, + with_spaces: bool = True, background: bool = False, + inverted: bool = False, ): timestamps = [] if background: - bgstr = "#" * (len(message) * multiply) + bgstr = " " * (len(message) * multiply) timestamps += self.git_mosaic_adapter.output( - bgstr, datetime.today(), True, True + bgstr, datetime.today(), with_spaces, True ) - for i in range(strength): - timestamps += self.git_mosaic_adapter.output(f"{message} " * multiply) + for _ in range(strength): + timestamps += self.git_mosaic_adapter.output(f"{message} " * multiply, None, with_spaces, inverted) timestamps.sort() diff --git a/tests/charmaps/test_basic_charmap.py b/tests/charmaps/test_basic_charmap.py index 6bad16c..ff67a93 100644 --- a/tests/charmaps/test_basic_charmap.py +++ b/tests/charmaps/test_basic_charmap.py @@ -20,19 +20,19 @@ def test_translate(self): assert lines == fox - def test_translate_with_background(self): + def test_translate_inverted(self): charmap = BasicCharmap() lines = charmap.translate('FoX', True, True) fox = [ - 'oooooo ooo oo o', - 'o oo ooo o', - 'o oo oo o o ', - 'ooo oo oo o ', - 'o oo oo o o ', - 'o oo ooo o', - 'o o ooo oo o' + ' oo oo ooo ', + ' ooooo ooo o ooo ', + ' ooooo ooo oo o o', + ' ooo ooo ooo oo', + ' ooooo ooo oo o o', + ' ooooo ooo o ooo ', + ' oooooo oo ooo ' ] assert lines == fox