From bfcf843ebcb50aae97cb083186e4a43e2ecc2f14 Mon Sep 17 00:00:00 2001 From: Bishoy Hany <167128903+Bishoy-at-pieces@users.noreply.github.com> Date: Wed, 9 Oct 2024 16:51:35 +0300 Subject: [PATCH 1/2] add progress bar for better user experience --- copilot/ask_view.py | 5 ++++ progress_bar.py | 62 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 progress_bar.py diff --git a/copilot/ask_view.py b/copilot/ask_view.py index 00fec9d..9629970 100644 --- a/copilot/ask_view.py +++ b/copilot/ask_view.py @@ -4,6 +4,7 @@ from .._pieces_lib.pieces_os_client import QGPTStreamOutput from .._pieces_lib.pieces_os_client.wrapper.basic_identifier.chat import BasicChat from ..settings import PiecesSettings +from ..progress_bar import ProgressBar import re @@ -23,6 +24,7 @@ def __init__(self): self._gpt_view = None self._view_name = None self._secondary_view = None + self.progress_bar = ProgressBar("Pieces Copilot") @property def gpt_view(self) -> View: @@ -134,6 +136,8 @@ def on_message_callback(self,message: QGPTStreamOutput): self.show_failed() self.reset_view() + if message.status != "IN-PROGRESS": + self.progress_bar.stop() def show_failed(self): self.gpt_view.add_regions( @@ -192,6 +196,7 @@ def ask(self,pipeline=None): self.new_line() self.remove_context_phantom() self.add_role("Copilot") + self.progress_bar.start() sublime.set_timeout_async(lambda: PiecesSettings.api_client.copilot.stream_question(query,pipeline)) def add_role(self,role): diff --git a/progress_bar.py b/progress_bar.py new file mode 100644 index 0000000..23a92b0 --- /dev/null +++ b/progress_bar.py @@ -0,0 +1,62 @@ +import sublime +import time + +class ProgressBar: + def __init__(self, label, width=10, update_interval=100, total=None): + """ + Initialize the progress bar with a label, width, update interval, and total progress. + + :param label: The label to display with the progress bar. + :param width: The width of the progress bar. + :param update_interval: The interval (in milliseconds) between updates. + :param total: The total amount of progress for measured progress bars. + """ + self.label = label + self.width = width + self.update_interval = update_interval + self.total = total + self.current = 0 + self._done = False + + def start(self): + """Start the progress bar.""" + self._done = False + self._update() + + def stop(self): + """Stop the progress bar and clear the status message.""" + sublime.status_message(f"{self.label} [✔ Complete]") + self._done = True + + def update_progress(self, progress): + """Update the current progress for measured progress bars.""" + if self.total is not None: + self.current = progress + if self.current >= self.total: + self.stop() + + def _update(self, status=0): + """Update the progress bar status.""" + if self._done: + time.sleep(2) + return + + if self.total is not None: + # Measured progress bar + percentage = int((self.current / self.total) * 100) + before = int((self.current / self.total) * self.width) + after = self.width - before + progress_bar = f"[{'█' * before}{'░' * after}] {percentage}%" + else: + # Unmeasured progress bar + status = status % (2 * self.width) + before = min(status, (2 * self.width) - status) + after = self.width - before + progress_bar = f"[{'█' * before}{'░' * after}]" + + # Update the status message + sublime.status_message(f"{self.label} {progress_bar}") + + # Schedule the next update + if not self._done: + sublime.set_timeout(lambda: self._update(status + 1), self.update_interval) From 0f28c99c3af47b795914d147a0a31cae51edb4bb Mon Sep 17 00:00:00 2001 From: Bishoy Hany <167128903+Bishoy-at-pieces@users.noreply.github.com> Date: Wed, 9 Oct 2024 17:01:44 +0300 Subject: [PATCH 2/2] Update auth_command.py --- auth/auth_command.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/auth/auth_command.py b/auth/auth_command.py index 65a84dc..2f4e89d 100644 --- a/auth/auth_command.py +++ b/auth/auth_command.py @@ -9,7 +9,7 @@ class PiecesLoginCommand(sublime_plugin.WindowCommand): @check_pieces_os() def run(self): - sublime.set_timeout_async(lambda:PiecesSettings.api_client.user.login(False)) + sublime.set_timeout_async(lambda:PiecesSettings.api_client.user.login(True)) # Lets connect to cloud after login class PiecesLogoutCommand(sublime_plugin.WindowCommand):