#[derive(Debug, Clone)]
pub struct WrapConfig {
pub first_line_width: usize,
pub continuation_line_width: usize,
pub gutter_width: usize,
pub hanging_indent: bool,
}
impl WrapConfig {
pub fn new(
content_area_width: usize,
gutter_width: usize,
has_scrollbar: bool,
hanging_indent: bool,
) -> Self {
let scrollbar_width = usize::from(has_scrollbar);
let text_area_width = content_area_width
.saturating_sub(scrollbar_width)
.saturating_sub(gutter_width);
Self {
first_line_width: text_area_width,
continuation_line_width: text_area_width,
gutter_width,
hanging_indent,
}
}
pub fn no_wrap(gutter_width: usize) -> Self {
Self {
first_line_width: usize::MAX,
continuation_line_width: usize::MAX,
gutter_width,
hanging_indent: false,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn new_subtracts_scrollbar_and_gutter() {
let cfg = WrapConfig::new(100, 6, true, false);
assert_eq!(cfg.first_line_width, 100 - 1 - 6);
assert_eq!(cfg.continuation_line_width, 100 - 1 - 6);
assert_eq!(cfg.gutter_width, 6);
assert!(!cfg.hanging_indent);
}
#[test]
fn new_without_scrollbar_omits_its_column() {
let cfg = WrapConfig::new(100, 6, false, true);
assert_eq!(cfg.first_line_width, 100 - 6);
assert!(cfg.hanging_indent);
}
#[test]
fn new_clamps_to_zero_on_oversize_deductions() {
let cfg = WrapConfig::new(3, 6, true, false);
assert_eq!(cfg.first_line_width, 0);
}
#[test]
fn no_wrap_returns_max_widths() {
let cfg = WrapConfig::no_wrap(6);
assert_eq!(cfg.first_line_width, usize::MAX);
assert_eq!(cfg.continuation_line_width, usize::MAX);
}
}