Skip to content

Commit

Permalink
fix never picking halfblocks
Browse files Browse the repository at this point in the history
When no capabilities at all could be queried (including missing
fontsize), which is likely for terminals that don't support any graphics
protocol, then resort to halfblocks with some arbitrary font size in 1:2
ratio.

It's not super important to get those halfblocks images to show with the
same size as their pixel counterpart. Also some terminals scale graphics
by DPI and some don't, so there is no uniform scale anyway.
  • Loading branch information
benjajaja committed Dec 8, 2024
1 parent e36a5bd commit f2c68ca
Showing 1 changed file with 30 additions and 19 deletions.
49 changes: 30 additions & 19 deletions src/picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,26 +80,37 @@ impl Picker {
let (is_tmux, tmux_proto) = detect_tmux_and_outer_protocol_from_env();

// Write and read to stdin to query protocol capabilities and font-size.
let (capability_proto, font_size) = query_with_timeout(is_tmux, Duration::from_secs(1))?;

// If some env var says that we should try iTerm2, then disregard protocol-from-capabilities.
let iterm2_proto = iterm2_from_env();

let protocol_type = tmux_proto
.or(iterm2_proto)
.or(capability_proto)
.unwrap_or(ProtocolType::Halfblocks);

if let Some(font_size) = font_size {
Ok(Picker {
font_size,
match query_with_timeout(is_tmux, Duration::from_secs(1)) {
Ok((capability_proto, font_size)) => {
// If some env var says that we should try iTerm2, then disregard protocol-from-capabilities.
let iterm2_proto = iterm2_from_env();

let protocol_type = tmux_proto
.or(iterm2_proto)
.or(capability_proto)
.unwrap_or(ProtocolType::Halfblocks);

if let Some(font_size) = font_size {
Ok(Picker {
font_size,
background_color: DEFAULT_BACKGROUND,
protocol_type,
is_tmux,
kitty_counter: rand::random(),
})
} else {
Err(Errors::NoFontSize)
}
}
Err(Errors::NoCap) => Ok(Picker {
// This is completely arbitrary, but it doesn't matter much for halfblocks.
font_size: (4, 8),
background_color: DEFAULT_BACKGROUND,
protocol_type,
protocol_type: ProtocolType::Halfblocks,
is_tmux,
kitty_counter: rand::random(),
})
} else {
Err(Errors::NoFontSize)
}),
Err(err) => Err(err),
}
}

Expand Down Expand Up @@ -429,13 +440,13 @@ fn query_with_timeout(
thread::spawn(move || {
let _ = tx.send(
enable_raw_mode()
.map_err(Errors::into)
.and_then(|disable_raw_mode| {
let result = query_stdio_capabilities(is_tmux);
// Always try to return to raw_mode.
disable_raw_mode()?;
result
})
.map_err(|dyn_err| io::Error::new(io::ErrorKind::Other, dyn_err.to_string())),
}),
);
});

Expand Down

0 comments on commit f2c68ca

Please sign in to comment.