Skip to content

Commit

Permalink
feat: Auto-detect Kitty terminals
Browse files Browse the repository at this point in the history
  • Loading branch information
alerque committed Aug 2, 2024
1 parent dc4f527 commit fe88a43
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod view;
mod widget;

use std::{
env,
io::{stdout, Stdout},
panic,
path::Path,
Expand All @@ -33,7 +34,7 @@ use ratatui::{
#[command(version)]
struct Args {
/// Image protocol to render graph
#[arg(short, long, value_name = "TYPE", default_value = "iterm")]
#[arg(short, long, value_name = "TYPE", default_value = "auto")]
protocol: ImageProtocolType,

/// Commit ordering algorithm
Expand All @@ -47,13 +48,15 @@ struct Args {

#[derive(Debug, Clone, ValueEnum)]
enum ImageProtocolType {
Auto,
Iterm,
Kitty,
}

impl From<ImageProtocolType> for protocol::ImageProtocol {
fn from(protocol: ImageProtocolType) -> Self {
match protocol {
ImageProtocolType::Auto => auto_detect_best_protocol(),
ImageProtocolType::Iterm => protocol::ImageProtocol::Iterm2,
ImageProtocolType::Kitty => protocol::ImageProtocol::Kitty,
}
Expand Down Expand Up @@ -96,6 +99,16 @@ fn initialize_panic_handler() {
}));
}

// By default assume the Iterm2 is the best protocol to use for all terminals *unless* an env
// variable is set that suggests the terminal is probably Kitty.
fn auto_detect_best_protocol() -> protocol::ImageProtocol {
if env::var("KITTY_WINDOW_ID").is_ok() {
protocol::ImageProtocol::Kitty
} else {
protocol::ImageProtocol::Iterm2
}
}

pub fn run() -> std::io::Result<()> {
color_eyre::install().unwrap();
let args = Args::parse();
Expand Down

0 comments on commit fe88a43

Please sign in to comment.