Skip to content
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

Auto-detect Kitty terminals, otherwise assume iTerm for everything else #23

Merged
merged 2 commits into from
Aug 2, 2024
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ Serie - A rich git commit graph in your terminal, like magic 📚
Usage: serie [OPTIONS]

Options:
-p, --protocol <TYPE> Image protocol to render graph [default: iterm] [possible values: iterm, kitty]
-p, --protocol <TYPE> Image protocol to render graph [default: auto] [possible values: auto, iterm, kitty]
-o, --order <TYPE> Commit ordering algorithm [default: chrono] [possible values: chrono, topo]
--no-cache Do not use graph image cache
-h, --help Print help
Expand All @@ -101,6 +101,8 @@ Options:
#### -p, --protocol \<TYPE\>

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.

Expand Down
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