There are multiple crates able to determine terminal width. Which one to use might depend on the exact relevant requirements for each situation. A good starting point might be to check out termion, which also provides a bunch of other useful low-level terminal handling functionality.
As can be seen on the linked page above, it supports multiple platforms. Yet anyone on Microsoft Windows should note that their platform is not mentioned:
There is also the termsize crate, which intends to no nothing more than determining the terminal size. This crate has an even wider platform support, and does work with Microsoft Windows.
After updating Cargo.toml
dependencies, retrieving the terminal width (and height) with these two crates are as trivial as:
fn main() {
let (x, y) = termion::terminal_size().unwrap();
let termsize::Size {rows, cols} = termsize::get().unwrap();
println!(" width height");
println!("termion: {:4} {:4}", x, y);
println!("termsize: {:4} {:4}", cols, rows);
}