Optimize GUI rendering and add shift+tab navigation#22
Merged
Conversation
- Optimize fill_rect using slice assignment instead of nested loops
- Optimize pixels_to_iterm2 using PIL palette mode ('P') and fast bytes()
- Add _pixels_to_iterm2_slow fallback for >256 colors edge case
The previous implementation iterated pixel-by-pixel in Python which caused
~200ms+ latency at demo resolution. The new implementation:
1. Uses slice assignment for fill_rect (much faster in CPython)
2. Uses bytes() + b"".join() for fast palette index conversion
3. Uses PIL 'P' mode to avoid manual RGB conversion loops
Performance impact: Buffer operations now take ~3.5ms vs much higher before.
- Add shift-tab detection in unix.py (ESC[Z sequence) - Add shift-tab detection in windows.py (ESC[Z for modern Windows Terminal, plus legacy \x00\x0f for older terminals) - Add shift-tab handling in app_loop.py to call focus_previous() - Add test for shift-tab navigation Users can now use Shift+Tab to navigate backwards through focusable components, complementing the existing Tab forward navigation. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Optimizations ported from iTerm2 improvements: 1. Palette caching - generate_palette() now caches the result and only regenerates when image colors change, avoiding string building every frame 2. Fast color detection - replaced nested loops with set.union(*band_rows) which is significantly faster for finding unique colors in each band 3. Local variable caching - cache list.append methods and precompute row/mask pairs to reduce attribute lookups in tight inner loops 4. Tuple for bit_masks - tuples have faster indexing than lists These optimizations reduce per-frame encoding time, improving UI responsiveness on Windows terminals using Sixel graphics. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Tests now properly handle platform differences: - Renderer tests accept both sixel and iTerm2 image formats - Config tests account for PLATFORM_SCALE (2x on iTerm2/macOS) - All 186 tests pass on both Windows and macOS/iTerm2 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
6815547 to
d64678f
Compare
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR improves GUI rendering performance on both macOS (iTerm2) and Windows, and adds shift+tab navigation support.
Performance Optimizations
iTerm2/macOS (<100ms latency target achieved):
fill_rectusing slice assignment instead of nested loopspixels_to_iterm2using PIL palette mode ('P') and fastbytes()conversion_pixels_to_iterm2_slowfallback for >256 colors edge caseWindows/Sixel:
set.union(*band_rows)for fast color detection in each bandlist.appendmethods and precompute row/mask pairs for tight loopsNew Feature: Shift+Tab Navigation
unix.py(ESC[Z sequence)windows.py(ESC[Z for modern Windows Terminal, plus legacy\x00\x0ffor older terminals)app_loop.pyto callfocus_previous()Test Fixes
PLATFORM_SCALE(2x on iTerm2/macOS)Test plan
🤖 Generated with Claude Code