Fix python visualizer script #2

Closed
opened 2025-09-14 19:19:46 +02:00 by assembly · 3 comments

here is a fix :)

diff --git a/scripts/punchcard_visualizer.py b/scripts/punchcard_visualizer.py
index 483012f..a1617f3 100644
--- a/scripts/punchcard_visualizer.py
+++ b/scripts/punchcard_visualizer.py
@@ -3,6 +3,7 @@ import argparse
 import sys
 import os
 import pygame
+import signal
 
 # -----------------------------
 # Layout + Style configuration
@@ -12,16 +13,16 @@ STRIP_WHITE = (255, 255, 255)
 HOLE_GRAY = BG_GRAY
 FONT_BLACK = (0, 0, 0)
 
-DEFAULT_CELL_W = 22        # width of one column cell
-DEFAULT_CELL_H = 28        # height of one row cell
-DEFAULT_COLS = 32          # nibble columns per strip (0..cols-1)
+DEFAULT_CELL_W = 22  # width of one column cell
+DEFAULT_CELL_H = 28  # height of one row cell
+DEFAULT_COLS = 32  # nibble columns per strip (0..cols-1)
 DEFAULT_STRIPS_PER_PAGE = 4
 DEFAULT_L_MARGIN = 30
 DEFAULT_R_MARGIN = 30
 DEFAULT_T_MARGIN = 40
 DEFAULT_B_MARGIN = 30
-DEFAULT_GAP_X = 6          # horizontal gap between cells
-DEFAULT_GAP_Y = 10         # vertical gap between strips
+DEFAULT_GAP_X = 6  # horizontal gap between cells
+DEFAULT_GAP_Y = 10  # vertical gap between strips
 DEFAULT_HOLE_RADIUS = 7
 
 HELP_TEXT = [
@@ -36,6 +37,7 @@ HELP_TEXT = [
     "  Q or Esc: quit",
 ]
 
+
 def bytes_to_nibbles(data_bytes):
     nibbles = []
     for b in data_bytes:
@@ -45,41 +47,136 @@ def bytes_to_nibbles(data_bytes):
         nibbles.append(lo)
     return nibbles
 
+
 def load_file(path):
     with open(path, "rb") as f:
         data = f.read()
     return bytes_to_nibbles(data)
 
-def compute_layout(cols, strips_per_page, cell_w, cell_h, gap_x, gap_y, lm, rm, tm, bm, label_h, extra_top):
+
+def compute_layout(
+    cols,
+    strips_per_page,
+    cell_w,
+    cell_h,
+    gap_x,
+    gap_y,
+    lm,
+    rm,
+    tm,
+    bm,
+    label_h,
+    extra_top,
+):
     """Compute total surface size and strip height, accounting for an extra top area (help/hint)."""
     strip_height = 5 * cell_h  # 4 data rows + 1 reference row
-    total_h = tm + extra_top + (strip_height * strips_per_page) + (gap_y * (strips_per_page - 1)) + bm + label_h * strips_per_page
+    total_h = (
+        tm
+        + extra_top
+        + (strip_height * strips_per_page)
+        + (gap_y * (strips_per_page - 1))
+        + bm
+        + label_h * strips_per_page
+    )
     total_w = lm + (cols * cell_w) + ((cols - 1) * gap_x) + rm
     return total_w, total_h, strip_height
 
+
 def draw_help(surface, font, x, y):
     for line in HELP_TEXT:
         img = font.render(line, True, FONT_BLACK)
         surface.blit(img, (x, y))
         y += img.get_height() + 2
 
+
 def main():
-    parser = argparse.ArgumentParser(description="Punch-card style visualizer for binary data (nibbles) using pygame.")
+    parser = argparse.ArgumentParser(
+        description="Punch-card style visualizer for binary data (nibbles) using pygame."
+    )
     parser.add_argument("file", help="Path to the binary file to visualize.")
-    parser.add_argument("--cols", type=int, default=DEFAULT_COLS, help="Nibble columns per strip (default: %(default)s).")
-    parser.add_argument("--strips", type=int, default=DEFAULT_STRIPS_PER_PAGE, help="Strips per page (default: %(default)s).")
-    parser.add_argument("--cellw", type=int, default=DEFAULT_CELL_W, help="Cell width (default: %(default)s).")
-    parser.add_argument("--cellh", type=int, default=DEFAULT_CELL_H, help="Cell height (default: %(default)s).")
-    parser.add_argument("--gapx", type=int, default=DEFAULT_GAP_X, help="Horizontal gap between cells (default: %(default)s).")
-    parser.add_argument("--gapy", type=int, default=DEFAULT_GAP_Y, help="Vertical gap between strips (default: %(default)s).")
-    parser.add_argument("--lm", type=int, default=DEFAULT_L_MARGIN, help="Left margin (default: %(default)s).")
-    parser.add_argument("--rm", type=int, default=DEFAULT_R_MARGIN, help="Right margin (default: %(default)s).")
-    parser.add_argument("--tm", type=int, default=DEFAULT_T_MARGIN, help="Top margin (default: %(default)s).")
-    parser.add_argument("--bm", type=int, default=DEFAULT_B_MARGIN, help="Bottom margin (default: %(default)s).")
-    parser.add_argument("--radius", type=int, default=DEFAULT_HOLE_RADIUS, help="Hole radius (default: %(default)s).")
-    parser.add_argument("--lsb-first", action="store_true", help="Draw bit rows as LSB at the top (default is MSB at the top).")
-    parser.add_argument("--font", type=str, default=None, help="Font name for labels (default: pygame default).")
-    parser.add_argument("--fontsize", type=int, default=16, help="Font size for labels (default: %(default)s).")
+    parser.add_argument(
+        "--cols",
+        type=int,
+        default=DEFAULT_COLS,
+        help="Nibble columns per strip (default: %(default)s).",
+    )
+    parser.add_argument(
+        "--strips",
+        type=int,
+        default=DEFAULT_STRIPS_PER_PAGE,
+        help="Strips per page (default: %(default)s).",
+    )
+    parser.add_argument(
+        "--cellw",
+        type=int,
+        default=DEFAULT_CELL_W,
+        help="Cell width (default: %(default)s).",
+    )
+    parser.add_argument(
+        "--cellh",
+        type=int,
+        default=DEFAULT_CELL_H,
+        help="Cell height (default: %(default)s).",
+    )
+    parser.add_argument(
+        "--gapx",
+        type=int,
+        default=DEFAULT_GAP_X,
+        help="Horizontal gap between cells (default: %(default)s).",
+    )
+    parser.add_argument(
+        "--gapy",
+        type=int,
+        default=DEFAULT_GAP_Y,
+        help="Vertical gap between strips (default: %(default)s).",
+    )
+    parser.add_argument(
+        "--lm",
+        type=int,
+        default=DEFAULT_L_MARGIN,
+        help="Left margin (default: %(default)s).",
+    )
+    parser.add_argument(
+        "--rm",
+        type=int,
+        default=DEFAULT_R_MARGIN,
+        help="Right margin (default: %(default)s).",
+    )
+    parser.add_argument(
+        "--tm",
+        type=int,
+        default=DEFAULT_T_MARGIN,
+        help="Top margin (default: %(default)s).",
+    )
+    parser.add_argument(
+        "--bm",
+        type=int,
+        default=DEFAULT_B_MARGIN,
+        help="Bottom margin (default: %(default)s).",
+    )
+    parser.add_argument(
+        "--radius",
+        type=int,
+        default=DEFAULT_HOLE_RADIUS,
+        help="Hole radius (default: %(default)s).",
+    )
+    parser.add_argument(
+        "--lsb-first",
+        action="store_true",
+        help="Draw bit rows as LSB at the top (default is MSB at the top).",
+    )
+    parser.add_argument(
+        "--font",
+        type=str,
+        default=None,
+        help="Font name for labels (default: pygame default).",
+    )
+    parser.add_argument(
+        "--fontsize",
+        type=int,
+        default=16,
+        help="Font size for labels (default: %(default)s).",
+    )
     args = parser.parse_args()
 
     if not os.path.isfile(args.file):
@@ -94,7 +191,9 @@ def main():
     controls_font = pygame.font.SysFont(args.font, args.fontsize + 3)
     hint_font = pygame.font.SysFont(args.font, args.fontsize)
 
-    label_height = label_font.get_height() + 6  # space for column indices above each strip
+    label_height = (
+        label_font.get_height() + 6
+    )  # space for column indices above each strip
 
     nibbles = load_file(args.file)
 
@@ -118,12 +217,24 @@ def main():
     recalc_paging()
 
     # Initial layout
-    extra_top = (controls_font.get_height() + 2) * len(HELP_TEXT) if show_controls else (hint_font.get_height() + 4)
+    extra_top = (
+        (controls_font.get_height() + 2) * len(HELP_TEXT)
+        if show_controls
+        else (hint_font.get_height() + 4)
+    )
     total_w, total_h, strip_height = compute_layout(
-        cols, strips_per_page,
-        args.cellw, args.cellh, args.gapx, args.gapy,
-        args.lm, args.rm, args.tm, args.bm, label_height,
-        extra_top
+        cols,
+        strips_per_page,
+        args.cellw,
+        args.cellh,
+        args.gapx,
+        args.gapy,
+        args.lm,
+        args.rm,
+        args.tm,
+        args.bm,
+        label_height,
+        extra_top,
     )
     screen = pygame.display.set_mode((total_w, total_h))
 
@@ -133,14 +244,26 @@ def main():
         nonlocal total_w, total_h, strip_height, screen
 
         # Determine extra space needed on top depending on help visibility
-        extra_top = (controls_font.get_height() + 2) * len(HELP_TEXT) if show_controls else (hint_font.get_height() + 4)
+        extra_top = (
+            (controls_font.get_height() + 2) * len(HELP_TEXT)
+            if show_controls
+            else (hint_font.get_height() + 4)
+        )
 
         # Recompute layout and resize the display surface if needed
         new_w, new_h, strip_height = compute_layout(
-            cols, strips_per_page,
-            args.cellw, args.cellh, args.gapx, args.gapy,
-            args.lm, args.rm, args.tm, args.bm, label_height,
-            extra_top
+            cols,
+            strips_per_page,
+            args.cellw,
+            args.cellh,
+            args.gapx,
+            args.gapy,
+            args.lm,
+            args.rm,
+            args.tm,
+            args.bm,
+            label_height,
+            extra_top,
         )
         if (new_w, new_h) != (total_w, total_h):
             total_w, total_h = new_w, new_h
@@ -175,7 +298,12 @@ def main():
         strip_top = args.tm + extra_top
         for s in range(strips_per_page):
             # Strip background (white)
-            strip_rect = pygame.Rect(args.lm, strip_top + label_height, total_w - args.lm - args.rm, strip_height)
+            strip_rect = pygame.Rect(
+                args.lm,
+                strip_top + label_height,
+                total_w - args.lm - args.rm,
+                strip_height,
+            )
             pygame.draw.rect(screen, STRIP_WHITE, strip_rect, border_radius=6)
 
             # Column indices above the strip — globally unique per column
@@ -247,7 +375,10 @@ def main():
                 elif event.key == pygame.K_END:
                     page = total_pages - 1
                     draw_page()
-                elif event.key in (pygame.K_PLUS, pygame.K_EQUALS):  # '+' (may require shift) or '='
+                elif event.key in (
+                    pygame.K_PLUS,
+                    pygame.K_EQUALS,
+                ):  # '+' (may require shift) or '='
                     cols = min(256, cols + 1)
                     recalc_paging()
                     draw_page()
@@ -255,7 +386,7 @@ def main():
                     cols = max(1, cols - 1)
                     recalc_paging()
                     draw_page()
-                elif event.key == pygame.K_LEFTBRACKET:   # '['
+                elif event.key == pygame.K_LEFTBRACKET:  # '['
                     strips_per_page = max(1, strips_per_page - 1)
                     recalc_paging()
                     draw_page()
@@ -279,7 +410,10 @@ def main():
 
         clock.tick(60)
 
-    pygame.quit()
+    # pygame.quit()
+    os.system(f"kill -9 {os.getpid()}")
+    exit("fuck off")
+
 
 if __name__ == "__main__":
     try:

here is a fix :) ```diff diff --git a/scripts/punchcard_visualizer.py b/scripts/punchcard_visualizer.py index 483012f..a1617f3 100644 --- a/scripts/punchcard_visualizer.py +++ b/scripts/punchcard_visualizer.py @@ -3,6 +3,7 @@ import argparse import sys import os import pygame +import signal # ----------------------------- # Layout + Style configuration @@ -12,16 +13,16 @@ STRIP_WHITE = (255, 255, 255) HOLE_GRAY = BG_GRAY FONT_BLACK = (0, 0, 0) -DEFAULT_CELL_W = 22 # width of one column cell -DEFAULT_CELL_H = 28 # height of one row cell -DEFAULT_COLS = 32 # nibble columns per strip (0..cols-1) +DEFAULT_CELL_W = 22 # width of one column cell +DEFAULT_CELL_H = 28 # height of one row cell +DEFAULT_COLS = 32 # nibble columns per strip (0..cols-1) DEFAULT_STRIPS_PER_PAGE = 4 DEFAULT_L_MARGIN = 30 DEFAULT_R_MARGIN = 30 DEFAULT_T_MARGIN = 40 DEFAULT_B_MARGIN = 30 -DEFAULT_GAP_X = 6 # horizontal gap between cells -DEFAULT_GAP_Y = 10 # vertical gap between strips +DEFAULT_GAP_X = 6 # horizontal gap between cells +DEFAULT_GAP_Y = 10 # vertical gap between strips DEFAULT_HOLE_RADIUS = 7 HELP_TEXT = [ @@ -36,6 +37,7 @@ HELP_TEXT = [ " Q or Esc: quit", ] + def bytes_to_nibbles(data_bytes): nibbles = [] for b in data_bytes: @@ -45,41 +47,136 @@ def bytes_to_nibbles(data_bytes): nibbles.append(lo) return nibbles + def load_file(path): with open(path, "rb") as f: data = f.read() return bytes_to_nibbles(data) -def compute_layout(cols, strips_per_page, cell_w, cell_h, gap_x, gap_y, lm, rm, tm, bm, label_h, extra_top): + +def compute_layout( + cols, + strips_per_page, + cell_w, + cell_h, + gap_x, + gap_y, + lm, + rm, + tm, + bm, + label_h, + extra_top, +): """Compute total surface size and strip height, accounting for an extra top area (help/hint).""" strip_height = 5 * cell_h # 4 data rows + 1 reference row - total_h = tm + extra_top + (strip_height * strips_per_page) + (gap_y * (strips_per_page - 1)) + bm + label_h * strips_per_page + total_h = ( + tm + + extra_top + + (strip_height * strips_per_page) + + (gap_y * (strips_per_page - 1)) + + bm + + label_h * strips_per_page + ) total_w = lm + (cols * cell_w) + ((cols - 1) * gap_x) + rm return total_w, total_h, strip_height + def draw_help(surface, font, x, y): for line in HELP_TEXT: img = font.render(line, True, FONT_BLACK) surface.blit(img, (x, y)) y += img.get_height() + 2 + def main(): - parser = argparse.ArgumentParser(description="Punch-card style visualizer for binary data (nibbles) using pygame.") + parser = argparse.ArgumentParser( + description="Punch-card style visualizer for binary data (nibbles) using pygame." + ) parser.add_argument("file", help="Path to the binary file to visualize.") - parser.add_argument("--cols", type=int, default=DEFAULT_COLS, help="Nibble columns per strip (default: %(default)s).") - parser.add_argument("--strips", type=int, default=DEFAULT_STRIPS_PER_PAGE, help="Strips per page (default: %(default)s).") - parser.add_argument("--cellw", type=int, default=DEFAULT_CELL_W, help="Cell width (default: %(default)s).") - parser.add_argument("--cellh", type=int, default=DEFAULT_CELL_H, help="Cell height (default: %(default)s).") - parser.add_argument("--gapx", type=int, default=DEFAULT_GAP_X, help="Horizontal gap between cells (default: %(default)s).") - parser.add_argument("--gapy", type=int, default=DEFAULT_GAP_Y, help="Vertical gap between strips (default: %(default)s).") - parser.add_argument("--lm", type=int, default=DEFAULT_L_MARGIN, help="Left margin (default: %(default)s).") - parser.add_argument("--rm", type=int, default=DEFAULT_R_MARGIN, help="Right margin (default: %(default)s).") - parser.add_argument("--tm", type=int, default=DEFAULT_T_MARGIN, help="Top margin (default: %(default)s).") - parser.add_argument("--bm", type=int, default=DEFAULT_B_MARGIN, help="Bottom margin (default: %(default)s).") - parser.add_argument("--radius", type=int, default=DEFAULT_HOLE_RADIUS, help="Hole radius (default: %(default)s).") - parser.add_argument("--lsb-first", action="store_true", help="Draw bit rows as LSB at the top (default is MSB at the top).") - parser.add_argument("--font", type=str, default=None, help="Font name for labels (default: pygame default).") - parser.add_argument("--fontsize", type=int, default=16, help="Font size for labels (default: %(default)s).") + parser.add_argument( + "--cols", + type=int, + default=DEFAULT_COLS, + help="Nibble columns per strip (default: %(default)s).", + ) + parser.add_argument( + "--strips", + type=int, + default=DEFAULT_STRIPS_PER_PAGE, + help="Strips per page (default: %(default)s).", + ) + parser.add_argument( + "--cellw", + type=int, + default=DEFAULT_CELL_W, + help="Cell width (default: %(default)s).", + ) + parser.add_argument( + "--cellh", + type=int, + default=DEFAULT_CELL_H, + help="Cell height (default: %(default)s).", + ) + parser.add_argument( + "--gapx", + type=int, + default=DEFAULT_GAP_X, + help="Horizontal gap between cells (default: %(default)s).", + ) + parser.add_argument( + "--gapy", + type=int, + default=DEFAULT_GAP_Y, + help="Vertical gap between strips (default: %(default)s).", + ) + parser.add_argument( + "--lm", + type=int, + default=DEFAULT_L_MARGIN, + help="Left margin (default: %(default)s).", + ) + parser.add_argument( + "--rm", + type=int, + default=DEFAULT_R_MARGIN, + help="Right margin (default: %(default)s).", + ) + parser.add_argument( + "--tm", + type=int, + default=DEFAULT_T_MARGIN, + help="Top margin (default: %(default)s).", + ) + parser.add_argument( + "--bm", + type=int, + default=DEFAULT_B_MARGIN, + help="Bottom margin (default: %(default)s).", + ) + parser.add_argument( + "--radius", + type=int, + default=DEFAULT_HOLE_RADIUS, + help="Hole radius (default: %(default)s).", + ) + parser.add_argument( + "--lsb-first", + action="store_true", + help="Draw bit rows as LSB at the top (default is MSB at the top).", + ) + parser.add_argument( + "--font", + type=str, + default=None, + help="Font name for labels (default: pygame default).", + ) + parser.add_argument( + "--fontsize", + type=int, + default=16, + help="Font size for labels (default: %(default)s).", + ) args = parser.parse_args() if not os.path.isfile(args.file): @@ -94,7 +191,9 @@ def main(): controls_font = pygame.font.SysFont(args.font, args.fontsize + 3) hint_font = pygame.font.SysFont(args.font, args.fontsize) - label_height = label_font.get_height() + 6 # space for column indices above each strip + label_height = ( + label_font.get_height() + 6 + ) # space for column indices above each strip nibbles = load_file(args.file) @@ -118,12 +217,24 @@ def main(): recalc_paging() # Initial layout - extra_top = (controls_font.get_height() + 2) * len(HELP_TEXT) if show_controls else (hint_font.get_height() + 4) + extra_top = ( + (controls_font.get_height() + 2) * len(HELP_TEXT) + if show_controls + else (hint_font.get_height() + 4) + ) total_w, total_h, strip_height = compute_layout( - cols, strips_per_page, - args.cellw, args.cellh, args.gapx, args.gapy, - args.lm, args.rm, args.tm, args.bm, label_height, - extra_top + cols, + strips_per_page, + args.cellw, + args.cellh, + args.gapx, + args.gapy, + args.lm, + args.rm, + args.tm, + args.bm, + label_height, + extra_top, ) screen = pygame.display.set_mode((total_w, total_h)) @@ -133,14 +244,26 @@ def main(): nonlocal total_w, total_h, strip_height, screen # Determine extra space needed on top depending on help visibility - extra_top = (controls_font.get_height() + 2) * len(HELP_TEXT) if show_controls else (hint_font.get_height() + 4) + extra_top = ( + (controls_font.get_height() + 2) * len(HELP_TEXT) + if show_controls + else (hint_font.get_height() + 4) + ) # Recompute layout and resize the display surface if needed new_w, new_h, strip_height = compute_layout( - cols, strips_per_page, - args.cellw, args.cellh, args.gapx, args.gapy, - args.lm, args.rm, args.tm, args.bm, label_height, - extra_top + cols, + strips_per_page, + args.cellw, + args.cellh, + args.gapx, + args.gapy, + args.lm, + args.rm, + args.tm, + args.bm, + label_height, + extra_top, ) if (new_w, new_h) != (total_w, total_h): total_w, total_h = new_w, new_h @@ -175,7 +298,12 @@ def main(): strip_top = args.tm + extra_top for s in range(strips_per_page): # Strip background (white) - strip_rect = pygame.Rect(args.lm, strip_top + label_height, total_w - args.lm - args.rm, strip_height) + strip_rect = pygame.Rect( + args.lm, + strip_top + label_height, + total_w - args.lm - args.rm, + strip_height, + ) pygame.draw.rect(screen, STRIP_WHITE, strip_rect, border_radius=6) # Column indices above the strip — globally unique per column @@ -247,7 +375,10 @@ def main(): elif event.key == pygame.K_END: page = total_pages - 1 draw_page() - elif event.key in (pygame.K_PLUS, pygame.K_EQUALS): # '+' (may require shift) or '=' + elif event.key in ( + pygame.K_PLUS, + pygame.K_EQUALS, + ): # '+' (may require shift) or '=' cols = min(256, cols + 1) recalc_paging() draw_page() @@ -255,7 +386,7 @@ def main(): cols = max(1, cols - 1) recalc_paging() draw_page() - elif event.key == pygame.K_LEFTBRACKET: # '[' + elif event.key == pygame.K_LEFTBRACKET: # '[' strips_per_page = max(1, strips_per_page - 1) recalc_paging() draw_page() @@ -279,7 +410,10 @@ def main(): clock.tick(60) - pygame.quit() + # pygame.quit() + os.system(f"kill -9 {os.getpid()}") + exit("fuck off") + if __name__ == "__main__": try: ```
Author

10/10 should merge

10/10 should merge
Author

lgtm

lgtm
Author

@barnii77 pls merge

@barnii77 pls merge
assembly canceled time tracking 2025-09-18 19:09:23 +02:00
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
barnii77/tapeworm#2
No description provided.