From ac709f99254c945044b25d9e609ac2a6b63d1bdd Mon Sep 17 00:00:00 2001 From: Marcus Nordstrom Date: Sun, 13 Apr 2025 12:35:09 +0200 Subject: [PATCH] feat(visualizer): add status print to terminal --- requirements.txt | 3 ++- visualizer.py | 26 +++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index 7f9941a..7a7c6a7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ pygame==2.6.1 -pygcode==0.2.1 \ No newline at end of file +pygcode==0.2.1 +colorama==0.4.6 \ No newline at end of file diff --git a/visualizer.py b/visualizer.py index d4ace48..f699da4 100644 --- a/visualizer.py +++ b/visualizer.py @@ -1,4 +1,6 @@ import pygame +import sys +from colorama import Fore, Style class Visualizer: def __init__(self, positions : list[tuple[float, float, float]], screen_dimensions : tuple[int, int], gcode_size : tuple[float, float]): @@ -30,6 +32,26 @@ class Visualizer: self.screen = pygame.display.set_mode(self.screen_dimensions) self.clock = pygame.time.Clock() + def _print_status(self) -> None: + normalized_x = self.prev_x / self.screen.get_width() + normalized_y = self.prev_y / self.screen.get_height() + pen_active = self.prev_z < 0 + + # Clear the terminal with ANSI code + print("\033c", end="") + + # X-axis bar + x_bar = f"{Fore.GREEN}{'#'*int(normalized_x*100)}{Style.RESET_ALL}{' '*int((1.0-normalized_x)*100)}" + print(f"X: |{x_bar}| {(normalized_x*100):.2f}%") + + # Y-axis bar + y_bar = f"{Fore.BLUE}{'#'*int(normalized_y*100)}{Style.RESET_ALL}{' '*int((1.0-normalized_y)*100)}" + print(f"Y: |{y_bar}| {(normalized_y*100):.2f}%") + + # Pen status + pen_status = f"{Fore.GREEN}Active{Style.RESET_ALL}" if pen_active else f"{Fore.RED}Inactive{Style.RESET_ALL}" + print(f"Pen: {pen_status}") + def visualize(self) -> None: running = True while running: @@ -61,5 +83,7 @@ class Visualizer: x1, y1, x2, y2 = drawn_line pygame.draw.line(self.screen, (255, 255, 255), (int(x1), int(y1)), (int(x2), int(y2)), 2) + self._print_status() + pygame.display.flip() - self.clock.tick(60) \ No newline at end of file + self.clock.tick(10) \ No newline at end of file