use crossterm::terminal;
use std::sync::atomic::{AtomicU16, Ordering};
static LAST_TERMINAL_WIDTH: AtomicU16 = AtomicU16::new(0);
const MIN_R_WIDTH: u16 = 20;
const MAX_R_WIDTH: u16 = 10000;
pub(super) fn sync_r_width() {
let prev = LAST_TERMINAL_WIDTH.load(Ordering::Relaxed);
let (cols, _) = match terminal::size() {
Ok(size) => size,
Err(e) => {
if prev != 0 {
log::debug!(
"Failed to read terminal size (transient); keeping previous width: {:?}",
e
);
return;
}
log::debug!(
"Failed to read terminal size; falling back to default width: {:?}",
e
);
(80, 24)
}
};
let clamped = cols.clamp(MIN_R_WIDTH, MAX_R_WIDTH);
if prev != clamped {
let code = format!("options(width = {})", clamped);
match arf_harp::eval_string_with_visibility(&code) {
Ok(_) => {
LAST_TERMINAL_WIDTH.store(clamped, Ordering::Relaxed);
}
Err(e) => log::debug!("Failed to set R width option: {:?}", e),
}
}
}