use diffs_rs::engine::SplitRatio;
use crate::config::review::{Layout, ReviewConfig};
use super::super::Screen;
use super::super::files::fit;
use super::Settings;
const VIEWER_AUTO_WIDTH: u16 = 120;
const PANE_MIN_WIDTH: u16 = 2 * SplitRatio::MIN_SIDE_WIDTH + 1;
pub(in crate::review) fn splits(terminal: u16, listed: bool, pane: u16) -> bool {
let list = if listed { fit::MIN_WIDTH + 1 } else { 0 };
terminal >= VIEWER_AUTO_WIDTH + list && pane >= PANE_MIN_WIDTH
}
impl Settings {
pub(in crate::review) fn for_viewer(&self) -> ReviewConfig {
let mut settings = self.now;
if settings.layout == Layout::Auto
&& let Some(split) = self.auto_split
{
settings.layout = if split { Layout::Split } else { Layout::Unified };
}
settings
}
}
impl Screen {
pub(in crate::review) fn decide_auto(&mut self, terminal: u16, pane: u16) -> anyhow::Result<()> {
let split = splits(terminal, self.capture.files.len() > 1, pane);
if self.settings.auto_split == Some(split) {
return Ok(());
}
self.settings.auto_split = Some(split);
self.apply_settings()
}
}
#[cfg(test)]
mod tests {
use super::{PANE_MIN_WIDTH, splits};
#[test]
fn auto_splits_from_141_columns_or_from_120_with_one_file() {
assert!(!splits(140, true, 119) && splits(141, true, 120));
assert!(!splits(119, false, 119) && splits(120, false, 120));
assert!(splits(160, true, 30 + PANE_MIN_WIDTH), "the pane's width is not the terminal's");
assert!(!splits(200, true, PANE_MIN_WIDTH - 1), "a pane too narrow for both sides");
}
}