Add emoji selector

Signed-off-by: Yohann D'ANELLO <ynerant@crans.org>
This commit is contained in:
Yohann D'ANELLO 2020-12-21 16:04:06 +01:00
parent 2f752592a1
commit 90918ddd71
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
1 changed files with 57 additions and 0 deletions

View File

@ -11,6 +11,7 @@ from threading import Thread
from typing import Any, List, Optional, Tuple from typing import Any, List, Optional, Tuple
import emoji import emoji
from emoji import unicode_codes
from squinnondation.term_manager import TermManager from squinnondation.term_manager import TermManager
@ -64,6 +65,7 @@ class Squinnondation:
squirrel = Squirrel(instance, nickname) squirrel = Squirrel(instance, nickname)
squirrel.refresh_history() squirrel.refresh_history()
squirrel.refresh_input() squirrel.refresh_input()
squirrel.refresh_emoji_pad()
if instance.args.client_address and instance.args.client_port: if instance.args.client_address and instance.args.client_port:
hazelnut = Hazelnut(address=instance.args.client_address, port=instance.args.client_port) hazelnut = Hazelnut(address=instance.args.client_address, port=instance.args.client_port)
@ -74,6 +76,7 @@ class Squinnondation:
while True: while True:
squirrel.refresh_history() squirrel.refresh_history()
squirrel.refresh_input() squirrel.refresh_input()
squirrel.refresh_emoji_pad()
key = screen.getkey(curses.LINES - 1, 3 + len(squirrel.nickname) + squirrel.input_index) key = screen.getkey(curses.LINES - 1, 3 + len(squirrel.nickname) + squirrel.input_index)
if key == "\x7f": # backspace if key == "\x7f": # backspace
if squirrel.input_index: if squirrel.input_index:
@ -106,6 +109,23 @@ class Squinnondation:
elif key == "KEY_END": elif key == "KEY_END":
squirrel.input_index = len(squirrel.input_buffer) squirrel.input_index = len(squirrel.input_buffer)
continue continue
elif key == "KEY_MOUSE":
try:
_, x, y, _, attr = curses.getmouse()
except curses.error:
# This is not a valid click
continue
if y == curses.LINES - 1 and x >= curses.COLS - 3:
# Click on the emoji
squirrel.emoji_panel_page *= -1
else:
squirrel.add_message(f"<system> *mouse click at ({y}, {x}): {attr}*")
if squirrel.emoji_panel_page > 0 and y == curses.LINES - 4 and x >= curses.COLS - 5:
squirrel.emoji_panel_page += 1
elif squirrel.emoji_panel_page > 1 and y == curses.LINES - curses.LINES // 2 - 1 \
and x >= curses.COLS - 5:
squirrel.emoji_panel_page -= 1
continue
elif len(key) > 1: elif len(key) > 1:
squirrel.add_message(f"<system> *unmanaged key press: {key}*") squirrel.add_message(f"<system> *unmanaged key press: {key}*")
continue continue
@ -445,6 +465,9 @@ class Squirrel(Hazelnut):
self.history = [] self.history = []
self.history_pad = curses.newpad(curses.LINES - 2, curses.COLS) self.history_pad = curses.newpad(curses.LINES - 2, curses.COLS)
self.input_pad = curses.newpad(1, curses.COLS) self.input_pad = curses.newpad(1, curses.COLS)
self.emoji_pad = curses.newpad(18, 12)
self.emoji_panel_page = -1
curses.init_color(curses.COLOR_WHITE, 1000, 1000, 1000) curses.init_color(curses.COLOR_WHITE, 1000, 1000, 1000)
for i in range(curses.COLOR_BLACK + 1, curses.COLOR_WHITE): for i in range(curses.COLOR_BLACK + 1, curses.COLOR_WHITE):
curses.init_pair(i + 1, i, curses.COLOR_BLACK) curses.init_pair(i + 1, i, curses.COLOR_BLACK)
@ -609,8 +632,42 @@ class Squirrel(Hazelnut):
self.input_pad.addstr(0, 1, self.nickname, curses.A_BOLD | curses.color_pair(color_id + 1)) self.input_pad.addstr(0, 1, self.nickname, curses.A_BOLD | curses.color_pair(color_id + 1))
self.input_pad.addstr(0, 1 + len(self.nickname), "> ") self.input_pad.addstr(0, 1 + len(self.nickname), "> ")
self.input_pad.addstr(0, 3 + len(self.nickname), self.input_buffer) self.input_pad.addstr(0, 3 + len(self.nickname), self.input_buffer)
self.input_pad.addstr(0, self.input_pad.getmaxyx()[1] - 3, "😀")
self.input_pad.refresh(0, 0, curses.LINES - 1, 0, curses.LINES - 1, curses.COLS - 1) self.input_pad.refresh(0, 0, curses.LINES - 1, 0, curses.LINES - 1, curses.COLS - 1)
def refresh_emoji_pad(self) -> None:
"""
Display the emoji pad if necessary.
"""
self.emoji_pad.erase()
if self.emoji_panel_page > 0:
height, width = curses.LINES // 2, curses.COLS // 3
self.emoji_pad.resize(height + 1, width + 1)
self.emoji_pad.addstr(0, 0, "" + (width - 2) * "" + "")
self.emoji_pad.addstr(0, (width - 14) // 2, " == EMOJIS == ")
for i in range(1, height):
self.emoji_pad.addstr(i, 0, "" + (width - 2) * " " + "")
self.emoji_pad.addstr(height - 1, 0, "" + (width - 2) * "" + "")
emojis = list(unicode_codes.UNICODE_EMOJI)
size = (height - 2) * (width - 4) // 4
page = emojis[(self.emoji_panel_page - 1) * size:self.emoji_panel_page * size]
if self.emoji_panel_page != 1:
self.emoji_pad.addstr(1, width - 2, "")
if len(page) == size:
self.emoji_pad.addstr(height - 2, width - 2, "")
for i in range(height - 2):
for j in range((width - 4) // 4):
index = i * (width - 4) // 4 + j
if index < len(page):
self.emoji_pad.addstr(i + 1, 4 * j + 1, page[index])
self.emoji_pad.refresh(0, 0, curses.LINES - height - 2, curses.COLS - width - 2,
curses.LINES - 2, curses.COLS - 2)
class Worm(Thread): class Worm(Thread):
""" """