diff options
author | Trygve Aaberge <trygveaa@gmail.com> | 2022-11-21 00:10:50 +0100 |
---|---|---|
committer | Trygve Aaberge <trygveaa@gmail.com> | 2024-02-18 11:32:53 +0100 |
commit | 0a7aecb9e8398a32fc041411ae45b85bcf477cea (patch) | |
tree | 8b569713719d996f21eddf1cbee9072d58b8066c | |
parent | c28cc0bc54b1ec55c657d8c32c54a01d68e8fc52 (diff) | |
download | wee-slack-0a7aecb9e8398a32fc041411ae45b85bcf477cea.tar.gz |
Show a loading indicator in the input
-rw-r--r-- | slack/api.py | 30 | ||||
-rw-r--r-- | slack/config.py | 9 | ||||
-rw-r--r-- | slack/init.py | 38 |
3 files changed, 57 insertions, 20 deletions
diff --git a/slack/api.py b/slack/api.py index 063c34f..f0cad96 100644 --- a/slack/api.py +++ b/slack/api.py @@ -20,6 +20,15 @@ if TYPE_CHECKING: ) +def get_conversation_from_buffer_pointer( + buffer_pointer: str, +) -> Optional[SlackConversation]: + for workspace in shared.workspaces.values(): + for conversation in workspace.conversations.values(): + if conversation.buffer_pointer == buffer_pointer: + return conversation + + class SlackApi: def __init__(self, workspace: SlackWorkspace): self.workspace = workspace @@ -99,13 +108,6 @@ class SlackWorkspace: self.users[id] = user return user - def get_conversation_from_buffer_pointer( - self, buffer_pointer: str - ) -> Optional[SlackConversation]: - for conversation in self.conversations.values(): - if conversation.buffer_pointer == buffer_pointer: - return conversation - class SlackUser: def __init__(self, workspace: SlackWorkspace, id: str): @@ -127,8 +129,9 @@ class SlackConversation: self.workspace = workspace self.id = id # TODO: buffer_pointer may be accessed by buffer_switch before it's initialized - self.buffer_pointer: str + self.buffer_pointer: str = "" self.name: str + self.loading = False self.history_filled = False self.history_pending = False @@ -136,17 +139,25 @@ class SlackConversation: def api(self) -> SlackApi: return self.workspace.api + def set_loading(self, loading: bool): + self.loading = loading + weechat.bar_item_update("input_text") + async def init(self): info = await self.fetch_info() self.name = info["channel"]["name"] self.buffer_pointer = weechat.buffer_new(self.name, "", "", "", "") async def fetch_info(self): - return await self.api.fetch("conversations.info", {"channel": self.id}) + self.set_loading(True) + info = await self.api.fetch("conversations.info", {"channel": self.id}) + self.set_loading(False) + return info async def fill_history(self): if self.history_filled or self.history_pending: return + self.set_loading(True) self.history_pending = True history = await self.api.fetch("conversations.history", {"channel": self.id}) @@ -163,6 +174,7 @@ class SlackConversation: print(f"history w/o fetch took: {time.time() - start}") self.history_filled = True self.history_pending = False + self.set_loading(False) class SlackMessage: diff --git a/slack/config.py b/slack/config.py index e9be0ba..8ee1e1c 100644 --- a/slack/config.py +++ b/slack/config.py @@ -158,10 +158,17 @@ class SlackConfigSectionColor: def __init__(self, weechat_config: WeeChatConfig): self._section = WeeChatSection(weechat_config, "color") + self.loading = WeeChatOption( + self._section, + "loading", + "text color for the loading text", + WeeChatColor("yellow"), + ) + self.reaction_suffix = WeeChatOption( self._section, "reaction_suffix", - "Color to use for the [:wave:(@user)] suffix on messages that have " + "text color for the [:wave:(@user)] suffix on messages that have " "reactions attached to them.", WeeChatColor("darkgray"), ) diff --git a/slack/init.py b/slack/init.py index 3964a51..e8e8ec3 100644 --- a/slack/init.py +++ b/slack/init.py @@ -1,10 +1,11 @@ import weechat +from slack.api import get_conversation_from_buffer_pointer from slack.commands import register_commands from slack.config import SlackConfig from slack.shared import shared from slack.task import create_task, sleep -from slack.util import get_callback_name +from slack.util import get_callback_name, with_color SCRIPT_AUTHOR = "Trygve Aaberge <trygveaa@gmail.com>" SCRIPT_LICENSE = "MIT" @@ -17,18 +18,30 @@ def shutdown_cb(): return weechat.WEECHAT_RC_OK -def signal_buffer_switch_cb(data: str, signal: str, signal_data: str) -> int: - current_conversation = None - for workspace in shared.workspaces.values(): - conversation = workspace.get_conversation_from_buffer_pointer(signal_data) - if conversation: - current_conversation = conversation - - if current_conversation: - create_task(current_conversation.fill_history()) +def signal_buffer_switch_cb(data: str, signal: str, buffer_pointer: str) -> int: + conversation = get_conversation_from_buffer_pointer(buffer_pointer) + if conversation: + create_task(conversation.fill_history()) return weechat.WEECHAT_RC_OK +def modifier_input_text_display_with_cursor_cb( + data: str, modifier: str, buffer_pointer: str, string: str +) -> str: + conversation = get_conversation_from_buffer_pointer(buffer_pointer) + if conversation and conversation.loading: + input_delim_color = weechat.config_string( + weechat.config_get("weechat.bar.input.color_delim") + ) + return ( + f"{with_color(input_delim_color, '[')}" + f"{with_color(shared.config.color.loading.value, 'loading')}" + f"{with_color(input_delim_color, ']')}" + f" {string}" + ) + return string + + async def init(): auto_connect = weechat.info_get("auto_connect", "") == "1" if auto_connect: @@ -60,5 +73,10 @@ def main(): weechat.hook_signal( "window_switch", get_callback_name(signal_buffer_switch_cb), "" ) + weechat.hook_modifier( + "input_text_display_with_cursor", + get_callback_name(modifier_input_text_display_with_cursor_cb), + "", + ) create_task(init(), final=True) |