Format bold, italic, underline text
Signed-off-by: Yohann D'ANELLO <ynerant@crans.org>
This commit is contained in:
parent
a1b93e851e
commit
fce8e01a5b
|
@ -388,7 +388,7 @@ class Squirrel(Hazelnut):
|
||||||
curses.init_pair(i + 1, i, curses.COLOR_BLACK)
|
curses.init_pair(i + 1, i, curses.COLOR_BLACK)
|
||||||
|
|
||||||
self.hazelnuts = dict()
|
self.hazelnuts = dict()
|
||||||
self.history.append(f"<system> Listening on {self.address}:{self.port}")
|
self.history.append(f"<system> *Listening on {self.address}:{self.port}*")
|
||||||
|
|
||||||
def find_hazelnut(self, address: str, port: int) -> Hazelnut:
|
def find_hazelnut(self, address: str, port: int) -> Hazelnut:
|
||||||
"""
|
"""
|
||||||
|
@ -426,6 +426,64 @@ class Squirrel(Hazelnut):
|
||||||
"""
|
"""
|
||||||
return self.socket.recvfrom(1024)
|
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:
|
def refresh_history(self) -> None:
|
||||||
"""
|
"""
|
||||||
Rewrite the history of the messages.
|
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, nickname, curses.A_BOLD | curses.color_pair(color_id + 1))
|
||||||
self.history_pad.addstr(i, 1 + len(nickname), "> ")
|
self.history_pad.addstr(i, 1 + len(nickname), "> ")
|
||||||
attrs = curses.color_pair(0)
|
attrs = curses.color_pair(0)
|
||||||
if nickname == "system":
|
self.print_markdown(self.history_pad, i, 3 + len(nickname), msg)
|
||||||
attrs |= curses.A_ITALIC
|
|
||||||
self.history_pad.addstr(i, 3 + len(nickname), msg, attrs)
|
|
||||||
self.history_pad.refresh(0, 0, 0, 0, curses.LINES - 2, curses.COLS)
|
self.history_pad.refresh(0, 0, 0, 0, curses.LINES - 2, curses.COLS)
|
||||||
|
|
||||||
def refresh_input(self) -> None:
|
def refresh_input(self) -> None:
|
||||||
|
@ -474,7 +530,7 @@ class Worm(Thread):
|
||||||
try:
|
try:
|
||||||
pkt, hazelnut = self.squirrel.receive_packet()
|
pkt, hazelnut = self.squirrel.receive_packet()
|
||||||
except ValueError as error:
|
except ValueError as error:
|
||||||
self.squirrel.history.append("<system> An error occured while receiving a packet: {}".format(error))
|
self.squirrel.history.append("<system> *An error occured while receiving a packet: {}*".format(error))
|
||||||
else:
|
else:
|
||||||
self.squirrel.history.append(pkt.body[0].data.decode('UTF-8'))
|
self.squirrel.history.append(pkt.body[0].data.decode('UTF-8'))
|
||||||
self.squirrel.refresh_history()
|
self.squirrel.refresh_history()
|
||||||
|
|
Loading…
Reference in New Issue