Skip to content

Commit

Permalink
Merge pull request #199 from pieces-app/add-progress-bar
Browse files Browse the repository at this point in the history
add progress bar for better user experience
  • Loading branch information
bishoy-at-pieces authored Oct 9, 2024
2 parents 8cc66f3 + 0f28c99 commit 4bf4abe
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
2 changes: 1 addition & 1 deletion auth/auth_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
5 changes: 5 additions & 0 deletions copilot/ask_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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:
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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):
Expand Down
62 changes: 62 additions & 0 deletions progress_bar.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 4bf4abe

Please sign in to comment.