-
Notifications
You must be signed in to change notification settings - Fork 239
feat: Add comprehensive terminal graphics protocol support with runtime detection #1378
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
f9bb0c9 to
1a656b6
Compare
775c6a3 to
6c6319f
Compare
| // Look for code "4" which indicates Sixel graphics support | ||
| // Response format: ESC[?1;2;4;6;9;15;18;21;22c | ||
| // Code "4" = Sixel graphics support | ||
| return response.contains(";4;") || response.contains(";4c") || response.endsWith("4c"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That last test - endsWith("4c") - will match extensions 14, 24, 34, etc. none of which are Sixel. It's not needed here.
| // Check for XTerm with sixel support | ||
| if (termEnv != null && termEnv.startsWith("xterm")) { | ||
| // XTerm supports sixel since patch #359 | ||
| // Unfortunately, there's no reliable way to detect the patch level | ||
| // from environment variables alone | ||
| // For now, assume modern xterm installations support sixel | ||
| return true; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that there are lots of terminals that set their TERM environment variable to some variant of xterm, and they won't all support sixel. Even xterm doesn't support it by default as far as I can recall (you need to launch it with -ti vt340, or set an appropriate resource setting). So this test is likely to produce false positives in a lot of cases. I don't know if that's a concern or not.
| // Start with DCS sequence and Sixel introduction | ||
| // Parameters: 0;1;q means: | ||
| // - 0: Pixel aspect ratio 1:1 | ||
| // - 1: Background color is set to black | ||
| // - q: Sixel mode | ||
| baos.write((DCS + SIXEL_INTRO).getBytes(StandardCharsets.US_ASCII)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That first 0 is actually requesting a 2:1 aspect ratio, unless you're overriding it elsewhere with a raster attributes command. So if you want 1:1, the SIXEL_INTRO should be something like 9;1q. Most terminals ignore aspect ratios, so it may seem like it's working correctly, but the images will end up stretched on terminals that do support them.
This commit introduces complete support for displaying images in modern terminals
through multiple graphics protocols with intelligent runtime detection.
## Features
### Multi-Protocol Graphics Support
- **Kitty Graphics Protocol** (Priority: 90) - Modern, feature-rich protocol
- **iTerm2 Inline Images** (Priority: 70) - iTerm2's proprietary protocol
- **Sixel Graphics** (Priority: 10) - Widely supported legacy protocol
### Intelligent Runtime Detection
- **Sixel Detection**: Device Attributes query (ESC[c) - same method as lsix command
- **Kitty Detection**: Static detection for known supported terminals
- **Automatic Fallback**: Falls back to static detection if runtime detection fails
- **Performance Optimized**: Skips runtime detection for known non-supporting terminals
### Comprehensive Terminal Support
| Terminal | Sixel | Kitty | iTerm2 | Detection Method |
|----------|-------|-------|--------|------------------|
| **Kitty** | ❌ No | ✅ Yes | ❌ No | Runtime + Static |
| **Ghostty** | ❌ No | ✅ Yes | ❌ No | Static (confirmed by maintainer) |
| **iTerm2** | ✅ Yes | ✅ Yes | ✅ Yes | Static + Environment |
| **WezTerm** | ✅ Yes | ✅ Yes | ❌ No | Static + Environment |
| **xterm** | ✅ Yes | ❌ No | ❌ No | Runtime + Static |
| **foot** | ✅ Yes | ❌ No | ❌ No | Runtime + Static |
| **Konsole** | ✅ Yes | ❌ No | ❌ No | Runtime + Static |
## Implementation
### Core Classes
- **TerminalGraphicsManager** - Unified interface for graphics operations
- **SixelGraphics** - Sixel protocol implementation with runtime detection
- **KittyGraphics** - Kitty protocol implementation
- **ITerm2Graphics** - iTerm2 protocol implementation
- **TerminalGraphics** - Common interface for all protocols
### Usage Examples
```java
// Automatic protocol selection
TerminalGraphicsManager.displayImage(terminal, bufferedImage);
// With options
TerminalGraphics.ImageOptions options = new TerminalGraphics.ImageOptions()
.width(100).height(100).preserveAspectRatio(true);
TerminalGraphicsManager.displayImage(terminal, image, options);
// Force specific protocol
TerminalGraphicsManager.forceProtocol(TerminalGraphics.Protocol.KITTY);
// Check support
boolean supported = TerminalGraphicsManager.isGraphicsSupported(terminal);
Optional<TerminalGraphics> best = TerminalGraphicsManager.getBestProtocol(terminal);
```
## Key Benefits
1. **Future-Proof**: Works with unknown terminals that support protocols
2. **Accurate Detection**: Runtime queries provide definitive answers
3. **Performance**: Smart detection avoids unnecessary queries
4. **Robust**: Comprehensive error handling and fallbacks
5. **Compatible**: Maintains full backward compatibility
6. **Extensible**: Easy to add new protocols via ServiceLoader
## Documentation
- Complete JavaDoc documentation for all classes
- Usage examples in TerminalGraphicsExample
- Comprehensive terminal-graphics.md guide in website/docs/advanced/
## Testing
- Comprehensive test coverage for all protocols
- Runtime detection testing with mocked responses
- Error handling and edge case testing
- Integration testing across different scenarios
## Build Requirements
- Upgraded to JDK 24 and Maven 4.0.0-rc-4
- All builds pass with mvn clean install
- Spotless formatting applied consistently
This implementation brings JLine's graphics capabilities to modern standards
while maintaining the library's reliability and performance characteristics.
6c6319f to
1d3df80
Compare
91d89d2 to
eb6f2f2
Compare
Address PR review comments:
1. Fix Sixel detection logic: Remove endsWith('4c') check that caused
false positives with extensions like '14c', '24c', etc. Now only
checks for proper ';4;' and ';4c' patterns.
2. Remove overly broad XTerm detection: The previous logic assumed all
terminals with TERM starting with 'xterm' support Sixel, causing
many false positives. Replaced with comment explaining the issue
and relying on runtime detection for real xterm support.
3. Fix Sixel aspect ratio: Change SIXEL_INTRO from '0;1;q' (2:1 ratio)
to '9;1;q' (1:1 ratio) for correct pixel aspect ratio. Updated
documentation comments to reflect the correct parameter meanings.
These changes make Sixel detection more accurate and reliable by
eliminating false positives and ensuring proper image rendering.
eb6f2f2 to
3921cd4
Compare
|
augment review |
|
Sorry, Augment does not have access to the org this pull request's branch is from. |
|
augment review |
|
Sorry, Augment does not have access to the org this pull request's branch is from. |
Changes: - Add Kitty graphics timeout constants (GRAPHICS_KITTY_TIMEOUT, GRAPHICS_KITTY_SUBSEQUENT_TIMEOUT) in TerminalBuilder for consistency with Sixel timeout constants - Make forcedProtocol volatile in TerminalGraphicsManager for thread-safety - Make sixelSupportOverride volatile in SixelGraphics for thread-safety - Add explicit null check in convertToSixel() with clear error message - Clarify Sixel INTRO parameter documentation (aspect ratio and background color handling)
🎨 Terminal Graphics Protocol Support
This PR adds comprehensive support for terminal graphics protocols with intelligent runtime detection, making JLine capable of displaying images in modern terminals.
🚀 Major Features
1. Multi-Protocol Graphics Support
2. Intelligent Runtime Detection
ESC[c) - same method aslsixcommand3. Comprehensive Terminal Support
🔧 Implementation Details
Core Classes
TerminalGraphicsManager- Unified interface for graphics operationsSixelGraphics- Sixel protocol implementation with runtime detectionKittyGraphics- Kitty protocol implementationITerm2Graphics- iTerm2 protocol implementationTerminalGraphics- Common interface for all protocolsRuntime Detection Logic
Usage Examples
📚 Documentation
website/docs/advanced/terminal-graphics.mdTerminalGraphicsExamplewith practical examplesSIXEL_GRAPHICS.mdfrom root (moved to proper website docs)🔧 Build & Development
Upgraded Build Tools
mvn clean installpasses successfullySingle Clean Commit
🧪 Testing
Comprehensive Test Coverage
SixelGraphicsTest.java- Sixel protocol testingTerminalGraphicsTest.java- Manager and integration testingDoubleSizeCharactersTest.java- Character rendering testing🎯 Key Benefits
🔍 Testing
🐛 Fixes
🔄 Backward Compatibility
This PR maintains full backward compatibility. All existing functionality continues to work unchanged, with graphics support being purely additive.
Ready for review! This implementation brings JLine's graphics capabilities to modern standards with a clean, single commit and comprehensive documentation. 🚀✨