diff --git a/README.md b/README.md index 73c802c..f5da7ca 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,7 @@ Serie - A rich git commit graph in your terminal, like magic 📚 Usage: serie [OPTIONS] Options: - -p, --protocol Image protocol to render graph [default: iterm] [possible values: iterm, kitty] + -p, --protocol Image protocol to render graph [default: auto] [possible values: auto, iterm, kitty] -o, --order Commit ordering algorithm [default: chrono] [possible values: chrono, topo] --no-cache Do not use graph image cache -h, --help Print help @@ -101,6 +101,8 @@ Options: #### -p, --protocol \ A protocol type for rendering images of commit graphs. +By default `auto` will guess the best supported protocal for the current terminal. +Kitty terminals are detected as `kitty` via an environment variable, and all others are assumed to support `iterm`. Refer to [Compatibility](#compatibility) for details. diff --git a/src/lib.rs b/src/lib.rs index a85e535..9dfcaed 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,6 +12,7 @@ mod view; mod widget; use std::{ + env, io::{stdout, Stdout}, panic, path::Path, @@ -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 @@ -47,6 +48,7 @@ struct Args { #[derive(Debug, Clone, ValueEnum)] enum ImageProtocolType { + Auto, Iterm, Kitty, } @@ -54,6 +56,7 @@ enum ImageProtocolType { impl From 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, } @@ -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();