1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//! Measurement module for tracking minimum and maximum rendering widths.
//!
//! Also re-exports the measurement-protocol free functions from
//! `console_render` (`measurement_get`, `measure_renderables`) so callers
//! can write `crate::measure::measurement_get(...)` without worrying about
//! where they're physically defined. The functions live in `console_render`
//! rather than here to avoid a circular dependency: this module is imported
//! by `console.rs`, so putting `Console`/`Renderable` references here would
//! create a cycle.
pub use crate::console::{measure_renderables, measurement_get};
use std::fmt;
use std::ops::Add;
/// Stores the minimum and maximum widths (in cells) required to render an object.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Measurement {
/// Minimum width in cells required to render without loss.
pub minimum: usize,
/// Maximum width in cells the content can meaningfully fill.
pub maximum: usize,
}
impl Measurement {
/// Create a new `Measurement` with the given minimum and maximum widths.
pub fn new(minimum: usize, maximum: usize) -> Self {
Self { minimum, maximum }
}
/// The difference between the maximum and minimum widths.
pub fn span(&self) -> usize {
self.maximum.saturating_sub(self.minimum)
}
/// Normalize the measurement so that minimum is never greater than maximum.
pub fn normalize(&self) -> Measurement {
let min = self.minimum.min(self.maximum);
let max = self.minimum.max(self.maximum);
Measurement {
minimum: min,
maximum: max,
}
}
/// Clamp the maximum width to at most `width`, clamping minimum as well if needed.
pub fn with_maximum(&self, width: usize) -> Measurement {
Measurement {
minimum: self.minimum.min(width),
maximum: self.maximum.min(width),
}
}
/// Ensure both minimum and maximum are at least `width`.
pub fn with_minimum(&self, width: usize) -> Measurement {
Measurement {
minimum: self.minimum.max(width),
maximum: self.maximum.max(width),
}
}
/// Apply optional minimum and maximum width constraints.
pub fn clamp(&self, min_width: Option<usize>, max_width: Option<usize>) -> Measurement {
let mut m = *self;
if let Some(min_w) = min_width {
m = m.with_minimum(min_w);
}
if let Some(max_w) = max_width {
m = m.with_maximum(max_w);
}
m
}
}
impl fmt::Display for Measurement {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Measurement({}, {})", self.minimum, self.maximum)
}
}
impl Add for Measurement {
type Output = Measurement;
/// Combine two measurements by taking the maximum of each field.
fn add(self, rhs: Self) -> Self::Output {
Measurement {
minimum: self.minimum.max(rhs.minimum),
maximum: self.maximum.max(rhs.maximum),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new() {
let m = Measurement::new(10, 100);
assert_eq!(m.minimum, 10);
assert_eq!(m.maximum, 100);
}
#[test]
fn test_span() {
assert_eq!(Measurement::new(10, 100).span(), 90);
}
#[test]
fn test_clamp() {
assert_eq!(
Measurement::new(20, 100).clamp(Some(10), Some(50)),
Measurement::new(20, 50)
);
assert_eq!(
Measurement::new(20, 100).clamp(Some(30), Some(50)),
Measurement::new(30, 50)
);
assert_eq!(
Measurement::new(20, 100).clamp(None, Some(50)),
Measurement::new(20, 50)
);
assert_eq!(
Measurement::new(20, 100).clamp(Some(30), None),
Measurement::new(30, 100)
);
assert_eq!(
Measurement::new(20, 100).clamp(None, None),
Measurement::new(20, 100)
);
}
#[test]
fn test_normalize() {
// When minimum > maximum, normalize swaps so minimum = smaller, maximum = larger.
// The larger value (100) must be preserved as the new maximum.
assert_eq!(
Measurement::new(100, 50).normalize(),
Measurement::new(50, 100)
);
// When already normalized, no change.
assert_eq!(
Measurement::new(10, 50).normalize(),
Measurement::new(10, 50)
);
}
#[test]
fn test_with_maximum() {
assert_eq!(
Measurement::new(10, 100).with_maximum(50),
Measurement::new(10, 50)
);
}
#[test]
fn test_with_minimum() {
assert_eq!(
Measurement::new(10, 100).with_minimum(50),
Measurement::new(50, 100)
);
}
#[test]
fn test_display() {
let m = Measurement::new(10, 100);
assert_eq!(format!("{m}"), "Measurement(10, 100)");
}
#[test]
fn test_add() {
let a = Measurement::new(10, 50);
let b = Measurement::new(20, 40);
let result = a + b;
assert_eq!(result, Measurement::new(20, 50));
}
}