Skip to content

Commit

Permalink
Implement inverted
Browse files Browse the repository at this point in the history
  • Loading branch information
justgigio committed Nov 8, 2023
1 parent 5944c43 commit ff2b168
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 29 deletions.
8 changes: 5 additions & 3 deletions src/awesome_git_mosaic/adapters/console/console_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 9 additions & 10 deletions src/awesome_git_mosaic/charmaps/basic/basic_charmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,26 @@ 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")
self.chars = self._load_char_model(
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

Expand Down
4 changes: 2 additions & 2 deletions src/awesome_git_mosaic/usecases/write_in_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
10 changes: 6 additions & 4 deletions src/awesome_git_mosaic/usecases/write_mosaic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
16 changes: 8 additions & 8 deletions tests/charmaps/test_basic_charmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ff2b168

Please sign in to comment.