Markdown-formatting and emojis can be disabled with a CLI

Signed-off-by: Yohann D'ANELLO <ynerant@crans.org>
This commit is contained in:
Yohann D'ANELLO 2020-12-21 16:04:04 +01:00
parent 9835fff1f9
commit ce61532f35
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
1 changed files with 26 additions and 16 deletions

View File

@ -19,8 +19,8 @@ class Squinnondation:
args: Any
bind_address: str
bind_port: int
client_address: str
client_port: int
no_emoji: bool
no_markdown: bool
def parse_arguments(self) -> None:
parser = ArgumentParser(description="MIRC client.")
@ -28,12 +28,14 @@ class Squinnondation:
help="Address of the client.")
parser.add_argument('bind_port', type=int, default=2500,
help="Port of the client. Must be between 1024 and 65535.")
parser.add_argument('--client_address', type=str, default="localhost",
parser.add_argument('--client_address', type=str, default=None,
help="Address of the first neighbour.")
parser.add_argument('--client_port', type=int, default=2500,
parser.add_argument('--client_port', type=int, default=0,
help="Port of the first neighbour. Must be between 1024 and 65535.")
parser.add_argument('--bind-only', '-b', action='store_true',
help="Don't connect to another client, only listen to connections.")
parser.add_argument('--no-emoji', '-ne', action='store_true',
help="Don't replace emojis.")
parser.add_argument('--no-markdown', '-nm', action='store_true',
help="Don't replace emojis.")
self.args = parser.parse_args()
if not (1024 <= self.args.bind_port <= 65535) and (1024 <= self.args.client_port <= 65535):
@ -41,8 +43,8 @@ class Squinnondation:
self.bind_address = self.args.bind_address
self.bind_port = self.args.bind_port
self.client_address = self.args.client_address
self.client_port = self.args.client_port
self.no_emoji = self.args.no_emoji
self.no_markdown = self.args.no_markdown
@staticmethod
def main() -> None: # pragma: no cover
@ -54,13 +56,13 @@ class Squinnondation:
screen.addstr(0, 0, "Enter your nickname: ")
nickname = screen.getstr().decode("UTF-8")
squirrel = Squirrel(nickname, instance.bind_address, instance.bind_port)
squirrel = Squirrel(instance, nickname)
squirrel.refresh_history()
squirrel.refresh_input()
if not instance.args.bind_only:
hazelnut = Hazelnut(address=instance.client_address, port=instance.client_port)
squirrel.hazelnuts[(instance.client_address, instance.client_port)] = hazelnut
if instance.args.client_address and instance.args.client_port:
hazelnut = Hazelnut(address=instance.args.client_address, port=instance.args.client_port)
squirrel.hazelnuts[(instance.args.client_address, instance.args.client_port)] = hazelnut
Worm(squirrel).start()
@ -375,13 +377,15 @@ class Squirrel(Hazelnut):
"""
The squirrel is the user of the program. It can speak with other clients, that are called hazelnuts.
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def __init__(self, instance: Squinnondation, nickname: str):
super().__init__(nickname, instance.bind_address, instance.bind_port)
# Create UDP socket
self.socket = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
# Bind the socket
self.socket.bind((str(self.address), self.port))
self.squinnondation = instance
self.history = []
self.history_pad = curses.newpad(curses.LINES - 2, curses.COLS)
self.input_pad = curses.newpad(1, curses.COLS)
@ -439,6 +443,14 @@ class Squirrel(Hazelnut):
_text_: italic
~~text~~: strikethrough
"""
# Replace :emoji_name: by the good emoji
if not self.squinnondation.no_emoji:
msg = emoji.emojize(msg, use_aliases=True)
if self.squinnondation.no_markdown:
pad.addstr(y, x, msg)
return len(msg)
underline_match = re.match("(.*)__(.*)__(.*)", msg)
if underline_match:
before, text, after = underline_match.group(1), underline_match.group(2), underline_match.group(3)
@ -488,8 +500,6 @@ class Squirrel(Hazelnut):
len_after = self.print_markdown(pad, y, x + len_before + len_mid, after, bold, italic, underline, strike)
return len_before + len_mid + len_after
# Replace :emoji_name: by the good emoji
msg = emoji.emojize(msg, use_aliases=True)
size = len(msg)
attrs = 0