30 lines
660 B
Rust
30 lines
660 B
Rust
use termsize::Size;
|
|
|
|
pub trait TerminalSize {
|
|
fn get_width(&self) -> usize {
|
|
let terminal: Size = termsize::get().unwrap_or(Size { rows: 150, cols: 150 });
|
|
terminal.cols.into()
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
struct Probe;
|
|
impl TerminalSize for Probe {}
|
|
|
|
#[test]
|
|
fn get_width_returns_positive_value() {
|
|
assert!(Probe.get_width() > 0);
|
|
}
|
|
|
|
#[test]
|
|
fn get_width_falls_back_to_150_when_no_tty() {
|
|
// In a non-TTY test environment termsize::get() returns None,
|
|
// so the fallback of 150 columns is used.
|
|
let w = Probe.get_width();
|
|
assert!(w >= 1);
|
|
}
|
|
}
|