Skip to content

Commit

Permalink
Emote completion(closes #42)
Browse files Browse the repository at this point in the history
  • Loading branch information
douglascdev committed Dec 7, 2023
1 parent acb2803 commit 3558593
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
2 changes: 1 addition & 1 deletion hasherino/components/chat_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async def on_scroll(self, event: ft.OnScrollEvent):
)

async def add_author_to_user_list(self, author: str):
tab_name = await self.memory_storage.get("channel")
tab_name = await self.persistent_storage.get("channel")

# Get existing list from memory or initialize a new one
if user_list := await self.memory_storage.get("channel_user_list"):
Expand Down
39 changes: 37 additions & 2 deletions hasherino/components/new_message_row.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,38 @@ async def new_message_clear_error(self, e):
await self.page.update_async()

async def emote_completion(self):
logging.info("Emote completion")
if not self.new_message.value:
return

stv_emotes = await self.memory_storage.get("7tv_emotes")
channel_stv_emotes = (
stv_emotes[await self.persistent_storage.get("channel")]
if stv_emotes
else {}
)
emote_map: dict[str, Emote] = await self.memory_storage.get("ttv_emote_sets")
emote_names = sorted(list(emote_map.keys()) + list(channel_stv_emotes.keys()))

if emote_names:
last_space_index = self.new_message.value.rfind(" ")

# If a space is not found it'll return -1, so getting the last word with last_space_index+1 works either way.
last_word = self.new_message.value[last_space_index + 1 :]

logging.debug(
f"Attempting emote completion. last_space_index: {last_space_index} last_word: {last_word}"
)

for emote_name in emote_names:
if emote_name.lower().startswith(last_word.lower()):
self.new_message.value = (
f"{self.new_message.value[:-len(last_word)]}{emote_name}"
)
logging.debug(
f"Found emote completion for {last_word} -> {emote_name}."
)
await self.new_message.update_async()
return

async def user_completion(self):
user_list = await self.memory_storage.get("channel_user_list")
Expand All @@ -66,7 +97,11 @@ async def user_completion(self):
f"Attempting username completion. last_space_index: {last_space_index} last_word: {last_word}"
)

for user in user_list[await self.persistent_storage.get("channel")]:
sorted_user_list = sorted(
user_list[await self.persistent_storage.get("channel")]
)

for user in sorted_user_list:
if user.lower().startswith(last_word.lower()):
self.new_message.value = (
f"{self.new_message.value[:-len(last_word)]}{user}"
Expand Down

0 comments on commit 3558593

Please sign in to comment.