From d5b187fdd5c5bca857e493aa794c7eddbf1748ea Mon Sep 17 00:00:00 2001 From: Greg Hendrickson Date: Tue, 27 Jan 2026 18:55:43 +0000 Subject: [PATCH] Fix scope error + proper vertical/horizontal centering - Remove leftover console.print that caused scope error - Consolidate all board rendering into render_board function - Add vertical centering based on terminal height - Board now properly centers in any size terminal --- src/shellmate/ssh/server.py | 68 +++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/src/shellmate/ssh/server.py b/src/shellmate/ssh/server.py index 4e0fc9f..3591b55 100644 --- a/src/shellmate/ssh/server.py +++ b/src/shellmate/ssh/server.py @@ -275,12 +275,11 @@ async def run_chess_game(process, session: TerminalSession, username: str, oppon session.clear() # Simple text-based board that works reliably over SSH - lines = [] - lines.append("") - lines.append(" ♔ SHELLMATE CHESS ♔") - lines.append("") - lines.append(" a b c d e f g h") - lines.append(" +---+---+---+---+---+---+---+---+") + board_lines = [] + board_lines.append(" ♔ SHELLMATE CHESS ♔") + board_lines.append("") + board_lines.append(" a b c d e f g h") + board_lines.append(" +---+---+---+---+---+---+---+---+") for rank in range(7, -1, -1): row = f" {rank + 1} |" @@ -297,46 +296,49 @@ async def run_chess_game(process, session: TerminalSession, username: str, oppon row += " . |" if not is_light else " |" row += f" {rank + 1}" - lines.append(row) - lines.append(" +---+---+---+---+---+---+---+---+") + board_lines.append(row) + board_lines.append(" +---+---+---+---+---+---+---+---+") - lines.append(" a b c d e f g h") - lines.append("") + board_lines.append(" a b c d e f g h") - # Center the board - board_width = 42 - left_pad = max(0, (session.width - board_width) // 2) - pad = " " * left_pad - - for line in lines: - session.write(pad + line + "\r\n") - - # Use Rich only for the status panel (simpler) - - # Simple status display + # Add status info + board_lines.append("") turn = "White ♔" if board.turn == chess.WHITE else "Black ♚" - session.write(pad + f" {turn} to move\r\n") + board_lines.append(f" {turn} to move") if board.is_check(): - session.write(pad + " *** CHECK! ***\r\n") + board_lines.append(" *** CHECK! ***") if move_history: last_moves = move_history[-3:] - session.write(pad + f" Moves: {' '.join(last_moves)}\r\n") + board_lines.append(f" Moves: {' '.join(last_moves)}") if status_msg: - session.write(pad + f" {status_msg}\r\n") + board_lines.append(f" {status_msg}") status_msg = "" - session.write("\r\n") - session.write(pad + " Enter move (e.g. e2e4) | [q]uit | [r]esign\r\n") - session.write(pad + " Move: ") - session.show_cursor() + board_lines.append("") + board_lines.append(" Enter move (e.g. e2e4) | [q]uit | [r]esign") + board_lines.append(" Move: ") + + # Calculate centering + board_width = 42 + total_height = len(board_lines) + + # Horizontal centering + left_pad = max(0, (session.width - board_width) // 2) + pad = " " * left_pad + + # Vertical centering + top_pad = max(0, (session.height - total_height) // 2) + + # Output with centering + for _ in range(top_pad): + session.write("\r\n") + + for line in board_lines: + session.write(pad + line + "\r\n") - # Show cursor for input - session.show_cursor() - console.print(Align.center("[cyan]Move: [/cyan]"), end="") - render_board() move_buffer = ""