Skip to content
/ termbg Public

A Rust library for terminal background color detection

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

dalance/termbg

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

78 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

termbg

A Rust library for terminal background color detection. The detected color is provided by RGB or theme ( dark or light ).

Actions Status Crates.io Docs.rs

Verified terminals

If you check other terminals, please report through issue.

Unsupported terminals

"Windows Terminal" may be supported in a future release: microsoft/terminal#3718.

Usage

[dependencies]
termbg = "0.6.0"

Example

fn main() {
    let timeout = std::time::Duration::from_millis(100);

    println!("Check terminal background color");
    let term = termbg::terminal();
    let rgb = termbg::rgb(timeout);
    let theme = termbg::theme(timeout);

    println!("  Term : {:?}", term);

    match rgb {
        Ok(rgb) => {
            println!("  Color: R={:x}, G={:x}, B={:x}", rgb.r, rgb.g, rgb.b);
        }
        Err(e) => {
            println!("  Color: detection failed {:?}", e);
        }
    }

    match theme {
        Ok(theme) => {
            println!("  Theme: {:?}", theme);
        }
        Err(e) => {
            println!("  Theme: detection failed {:?}", e);
        }
    }
}

Check program

This crate provides a simple program to check.

$ cargo run
Check terminal background color
  Term : Tmux
  Color: R=0, G=0, B=0
  Theme: Dark

Detecting mechanism

If the terminal is win32 console, WIN32API is used for detection. If the terminal is xterm compatible, "Xterm Control Sequences" is used. When these method was failed, COLORFGBG environment variable is used.

The detected RGB is converted to YCbCr. If Y > 0.5, the theme is detected as "light", otherwise "dark".

About

A Rust library for terminal background color detection

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Languages