askrobots / dbbasic-manx
Public
manx.py: man pages in BBS style, with ANSI colours and syntax highlighting, using only the Python standard library.
test_width.py · 1.5 KB · 59 lines
| 1 | #!/usr/bin/env python3 |
| 2 | """Test script to demonstrate terminal width adaptation.""" |
| 3 | |
| 4 | import sys |
| 5 | import os |
| 6 | |
| 7 | # Mock different terminal widths |
| 8 | test_cases = [ |
| 9 | (200, 120, "Very wide terminal"), |
| 10 | (100, 100, "Standard terminal"), |
| 11 | (80, 80, "Narrow terminal"), |
| 12 | (50, 60, "Very narrow terminal (min 60)"), |
| 13 | ] |
| 14 | |
| 15 | print("Terminal Width Adaptation Test") |
| 16 | print("=" * 60) |
| 17 | print() |
| 18 | |
| 19 | for mock_width, expected, description in test_cases: |
| 20 | # Temporarily mock the terminal size |
| 21 | old_get = None |
| 22 | try: |
| 23 | import shutil |
| 24 | |
| 25 | class MockSize: |
| 26 | def __init__(self, cols, lines): |
| 27 | self.columns = cols |
| 28 | self.lines = lines |
| 29 | |
| 30 | # Save original |
| 31 | import manx |
| 32 | original_get_terminal_size = shutil.get_terminal_size |
| 33 | |
| 34 | # Mock it |
| 35 | shutil.get_terminal_size = lambda fallback=None: MockSize(mock_width, 24) |
| 36 | |
| 37 | # Also mock isatty to return True |
| 38 | original_isatty = sys.stdout.isatty |
| 39 | sys.stdout.isatty = lambda: True |
| 40 | |
| 41 | # Get width |
| 42 | from manx import get_term_width, h1 |
| 43 | width = get_term_width() |
| 44 | |
| 45 | print(f"{description}:") |
| 46 | print(f" Input: {mock_width} cols → Output: {width} cols (expected: {expected})") |
| 47 | print(f" {h1('Test Header')}") |
| 48 | print() |
| 49 | |
| 50 | # Restore |
| 51 | shutil.get_terminal_size = original_get_terminal_size |
| 52 | sys.stdout.isatty = original_isatty |
| 53 | |
| 54 | except Exception as e: |
| 55 | print(f" Error: {e}") |
| 56 | print() |
| 57 | |
| 58 | print("=" * 60) |
| 59 | print("✓ All terminal width tests completed!") |