mirror of
https://github.com/denisidoro/navi.git
synced 2026-07-18 00:54:58 +00:00
58 lines
1.3 KiB
Rust
58 lines
1.3 KiB
Rust
use crate::prelude::*;
|
|
use crossterm::terminal;
|
|
|
|
use std::process::Command;
|
|
|
|
const FALLBACK_WIDTH: u16 = 80;
|
|
|
|
fn width_with_shell_out() -> Result<u16> {
|
|
let output = if cfg!(target_os = "macos") {
|
|
Command::new("stty")
|
|
.arg("-f")
|
|
.arg("/dev/stderr")
|
|
.arg("size")
|
|
.stderr(Stdio::inherit())
|
|
.output()?
|
|
} else {
|
|
Command::new("stty")
|
|
.arg("size")
|
|
.arg("-F")
|
|
.arg("/dev/stderr")
|
|
.stderr(Stdio::inherit())
|
|
.output()?
|
|
};
|
|
|
|
if let Some(0) = output.status.code() {
|
|
let stdout = String::from_utf8(output.stdout).expect("Invalid utf8 output from stty");
|
|
let mut data = stdout.split_whitespace();
|
|
data.next();
|
|
return data
|
|
.next()
|
|
.expect("Not enough data")
|
|
.parse::<u16>()
|
|
.map_err(|_| anyhow!("Invalid width"));
|
|
}
|
|
|
|
Err(anyhow!("Invalid status code"))
|
|
}
|
|
|
|
pub fn width() -> u16 {
|
|
if let Ok((w, _)) = terminal::size() {
|
|
w
|
|
} else {
|
|
width_with_shell_out().unwrap_or(FALLBACK_WIDTH)
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn test_width() {
|
|
let result = width();
|
|
let is_ok = if result == 0 { false } else { true };
|
|
|
|
assert!(is_ok);
|
|
}
|
|
}
|