-
Notifications
You must be signed in to change notification settings - Fork 613
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #288 from NexaAI/perry-debug
Several general modifications
- Loading branch information
Showing
9 changed files
with
145 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,79 @@ | ||
# For similar spinner animation implementation, refer to: nexa/utils.py | ||
|
||
import sys | ||
import threading | ||
import time | ||
import os | ||
import itertools | ||
from contextlib import contextmanager | ||
|
||
def get_spinner_style(style="default"): | ||
spinners = { | ||
"default": '⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏' | ||
"default": ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"] | ||
} | ||
return spinners.get(style, spinners["default"]) | ||
|
||
def spinning_cursor(style="default"): | ||
while True: | ||
for cursor in get_spinner_style(style): | ||
yield cursor | ||
def _get_output_stream(): | ||
"""Get the appropriate output stream based on platform.""" | ||
if sys.platform == "win32": | ||
return open('CONOUT$', 'wb') | ||
else: | ||
try: | ||
return os.open('/dev/tty', os.O_WRONLY) | ||
except (FileNotFoundError, OSError): | ||
return os.open('/dev/stdout', os.O_WRONLY) | ||
|
||
def show_spinner(stop_event, style="default", message=""): | ||
spinner = spinning_cursor(style) | ||
|
||
fd = os.open('/dev/tty', os.O_WRONLY) | ||
spinner = itertools.cycle(get_spinner_style(style)) | ||
fd = _get_output_stream() | ||
is_windows = sys.platform == "win32" | ||
|
||
while not stop_event.is_set(): | ||
display = f"\r{message} {next(spinner)}" if message else f"\r{next(spinner)}" | ||
os.write(fd, display.encode()) | ||
time.sleep(0.1) | ||
|
||
os.write(fd, b"\r" + b" " * (len(message) + 2)) | ||
os.write(fd, b"\r") | ||
os.close(fd) | ||
try: | ||
while not stop_event.is_set(): | ||
display = f"\r{message} {next(spinner)}" if message else f"\r{next(spinner)} " | ||
|
||
if is_windows: | ||
fd.write(display.encode()) | ||
fd.flush() | ||
else: | ||
os.write(fd, display.encode()) | ||
time.sleep(0.1) | ||
|
||
# Clear the spinner | ||
clear_msg = b"\r" + b" " * (len(message) + 2) + b"\r" | ||
if is_windows: | ||
fd.write(clear_msg) | ||
fd.flush() | ||
else: | ||
os.write(fd, clear_msg) | ||
|
||
finally: | ||
if is_windows: | ||
fd.close() | ||
else: | ||
os.close(fd) | ||
|
||
def start_spinner(style="default", message=""): | ||
stop_event = threading.Event() | ||
spinner_thread = threading.Thread(target=show_spinner, args=(stop_event, style, message)) | ||
spinner_thread.daemon = True | ||
spinner_thread = threading.Thread( | ||
target=show_spinner, | ||
args=(stop_event, style, message), | ||
daemon=True | ||
) | ||
spinner_thread.start() | ||
return stop_event, spinner_thread | ||
|
||
def stop_spinner(stop_event, spinner_thread): | ||
stop_event.set() | ||
spinner_thread.join() | ||
if stop_event and not stop_event.is_set(): | ||
stop_event.set() | ||
if spinner_thread and spinner_thread.is_alive(): | ||
spinner_thread.join() | ||
|
||
@contextmanager | ||
def spinning_cursor(message="", style="default"): | ||
"""Context manager for spinner animation.""" | ||
stop_event, thread = start_spinner(style, message) | ||
try: | ||
yield | ||
finally: | ||
stop_spinner(stop_event, thread) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters