From fce8e01a5b7c29d8c5fb0effee9cb3332caaed4d Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Mon, 21 Dec 2020 16:04:02 +0100 Subject: [PATCH] Format bold, italic, underline text Signed-off-by: Yohann D'ANELLO --- squinnondation/squinnondation.py | 66 +++++++++++++++++++++++++++++--- 1 file changed, 61 insertions(+), 5 deletions(-) diff --git a/squinnondation/squinnondation.py b/squinnondation/squinnondation.py index f468f6c..b1548a4 100644 --- a/squinnondation/squinnondation.py +++ b/squinnondation/squinnondation.py @@ -388,7 +388,7 @@ class Squirrel(Hazelnut): curses.init_pair(i + 1, i, curses.COLOR_BLACK) self.hazelnuts = dict() - self.history.append(f" Listening on {self.address}:{self.port}") + self.history.append(f" *Listening on {self.address}:{self.port}*") def find_hazelnut(self, address: str, port: int) -> Hazelnut: """ @@ -426,6 +426,64 @@ class Squirrel(Hazelnut): """ return self.socket.recvfrom(1024) + def print_markdown(self, pad: Any, y: int, x: int, msg: str, + bold: bool = False, italic: bool = False, underline: bool = False) -> int: + """ + Parse a markdown-formatted text and format the text as bold, italic or underlined text. + ***text***: bold, italic + **text**: bold + *text*: italic + __text__: underline + _text_: italic + """ + underline_match = re.match("(.*)__(.*)__(.*)", msg) + if underline_match: + before, underlined, after = underline_match.group(1), underline_match.group(2), underline_match.group(3) + len_before = self.print_markdown(pad, y, x, before, bold, italic, underline) + len_mid = self.print_markdown(pad, y, x + len_before, underlined, bold, italic, not underline) + len_after = self.print_markdown(pad, y, x + len_before + len_mid, after, bold, italic, underline) + return len_before + len_mid + len_after + + italic_match = re.match("(.*)_(.*)_(.*)", msg) + if italic_match: + before, underlined, after = italic_match.group(1), italic_match.group(2), italic_match.group(3) + len_before = self.print_markdown(pad, y, x, before, bold, italic, underline) + len_mid = self.print_markdown(pad, y, x + len_before, underlined, bold, not italic, underline) + len_after = self.print_markdown(pad, y, x + len_before + len_mid, after, bold, italic, underline) + return len_before + len_mid + len_after + + bold_italic_match = re.match("(.*)\\*\\*\\*(.*)\\*\\*\\*(.*)", msg) + if bold_italic_match: + before, underlined, after = bold_italic_match.group(1), bold_italic_match.group(2),\ + bold_italic_match.group(3) + len_before = self.print_markdown(pad, y, x, before, bold, italic, underline) + len_mid = self.print_markdown(pad, y, x + len_before, underlined, not bold, not italic, underline) + len_after = self.print_markdown(pad, y, x + len_before + len_mid, after, bold, italic, underline) + return len_before + len_mid + len_after + + bold_match = re.match("(.*)\\*\\*(.*)\\*\\*(.*)", msg) + if bold_match: + before, underlined, after = bold_match.group(1), bold_match.group(2), bold_match.group(3) + len_before = self.print_markdown(pad, y, x, before, bold, italic, underline) + len_mid = self.print_markdown(pad, y, x + len_before, underlined, not bold, italic, underline) + len_after = self.print_markdown(pad, y, x + len_before + len_mid, after, bold, italic, underline) + return len_before + len_mid + len_after + + italic_match = re.match("(.*)\\*(.*)\\*(.*)", msg) + if italic_match: + before, underlined, after = italic_match.group(1), italic_match.group(2), italic_match.group(3) + len_before = self.print_markdown(pad, y, x, before, bold, italic, underline) + len_mid = self.print_markdown(pad, y, x + len_before, underlined, bold, not italic, underline) + len_after = self.print_markdown(pad, y, x + len_before + len_mid, after, bold, italic, underline) + return len_before + len_mid + len_after + + attrs = 0 + attrs |= curses.A_BOLD if bold else 0 + attrs |= curses.A_ITALIC if italic else 0 + attrs |= curses.A_UNDERLINE if underline else 0 + pad.addstr(y, x, msg, attrs) + return len(msg) + def refresh_history(self) -> None: """ Rewrite the history of the messages. @@ -442,9 +500,7 @@ class Squirrel(Hazelnut): self.history_pad.addstr(i, 1, nickname, curses.A_BOLD | curses.color_pair(color_id + 1)) self.history_pad.addstr(i, 1 + len(nickname), "> ") attrs = curses.color_pair(0) - if nickname == "system": - attrs |= curses.A_ITALIC - self.history_pad.addstr(i, 3 + len(nickname), msg, attrs) + self.print_markdown(self.history_pad, i, 3 + len(nickname), msg) self.history_pad.refresh(0, 0, 0, 0, curses.LINES - 2, curses.COLS) def refresh_input(self) -> None: @@ -474,7 +530,7 @@ class Worm(Thread): try: pkt, hazelnut = self.squirrel.receive_packet() except ValueError as error: - self.squirrel.history.append(" An error occured while receiving a packet: {}".format(error)) + self.squirrel.history.append(" *An error occured while receiving a packet: {}*".format(error)) else: self.squirrel.history.append(pkt.body[0].data.decode('UTF-8')) self.squirrel.refresh_history()