Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions crates/jcode-terminal-image/src/display.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Terminal image display support
//!
//! Supports Kitty graphics protocol (Kitty, Ghostty), iTerm2 inline images,
//! Supports Kitty graphics protocol (Kitty, Ghostty, Handterm), iTerm2 inline images,
//! and Sixel graphics (xterm, foot, mlterm, WezTerm).
//! Falls back to a simple placeholder if no image protocol is available.

Expand Down Expand Up @@ -40,16 +40,16 @@ impl ImageProtocol {
return Self::Kitty;
}

// Check TERM for kitty or ghostty
// Check TERM for Kitty-compatible terminals.
if let Ok(term) = std::env::var("TERM")
&& (term.contains("kitty") || term.contains("ghostty"))
&& is_kitty_terminal_name(&term)
{
return Self::Kitty;
}

// Check TERM_PROGRAM for Ghostty
// Check TERM_PROGRAM for Kitty-compatible terminals.
if let Ok(term_program) = std::env::var("TERM_PROGRAM") {
if term_program == "ghostty" {
if is_kitty_terminal_name(&term_program) {
return Self::Kitty;
}
if term_program == "iTerm.app" {
Expand Down Expand Up @@ -113,6 +113,11 @@ impl ImageProtocol {
}
}

fn is_kitty_terminal_name(value: &str) -> bool {
let value = value.to_ascii_lowercase();
value.contains("kitty") || value.contains("ghostty") || value.contains("handterm")
}

/// Display parameters for terminal images
#[derive(Debug, Clone)]
pub struct ImageDisplayParams {
Expand Down Expand Up @@ -452,6 +457,12 @@ mod tests {
assert!(!can_display_to_stdout(ImageProtocol::None, true));
}

#[test]
fn handterm_uses_kitty_graphics_protocol() {
assert!(is_kitty_terminal_name("handterm"));
assert!(is_kitty_terminal_name("HandTerm"));
}

#[test]
fn test_calculate_display_size() {
// Wide image
Expand Down
5 changes: 5 additions & 0 deletions crates/jcode-tui-mermaid/src/mermaid_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ pub(super) fn infer_protocol_from_env(
|| term_program.contains("kitty")
|| term_program.contains("wezterm")
|| term_program.contains("ghostty")
|| term_program.contains("handterm")
{
return Some(ProtocolType::Kitty);
}
Expand Down Expand Up @@ -479,6 +480,10 @@ mod tests {
infer_protocol_from_env(None, Some("ghostty"), None, None),
Some(ProtocolType::Kitty)
);
assert_eq!(
infer_protocol_from_env(None, Some("HandTerm"), None, None),
Some(ProtocolType::Kitty)
);
assert_eq!(
infer_protocol_from_env(None, Some("WezTerm"), None, None),
Some(ProtocolType::Kitty)
Expand Down
Loading
Loading