Skip to content

Commit 4ce7757

Browse files
authored
merge pull request #7
`trim_to_height()`
2 parents ae84993 + cf7ed28 commit 4ce7757

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

src/str.rs

+20
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use self::ellipsis::{Ascii, Contd, Horizontal};
1111
/// see [`Limited`] for more information.
1212
pub mod ellipsis;
1313

14+
mod trim_to_height;
1415
mod trim_to_length;
1516
mod trim_to_width;
1617

@@ -72,6 +73,9 @@ pub trait Limited {
7273

7374
/// returns a string limited by width.
7475
fn trim_to_width<E: Ellipsis>(&self, length: usize) -> String;
76+
77+
/// returns a string limited by height.
78+
fn trim_to_height<E: Ellipsis>(&self, height: usize) -> String;
7579
}
7680

7781
// === impl s: asref<str> ===
@@ -115,4 +119,20 @@ where
115119
.limited(width)
116120
.collect()
117121
}
122+
123+
fn trim_to_height<E: Ellipsis>(&self, height: usize) -> String {
124+
use {self::trim_to_height::TrimToHeightIter, crate::iter::Limited};
125+
126+
let value: &'_ str = self.as_ref();
127+
128+
if value.is_empty() || height == 0 {
129+
return String::default();
130+
}
131+
132+
TrimToHeightIter::<E>::new(value)
133+
.limited(height)
134+
.collect::<Vec<&str>>()
135+
.as_slice()
136+
.join("\n")
137+
}
118138
}

src/str/ellipsis.rs

+11
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ pub struct Ascii;
1212
/// a more verbose ellipsis.
1313
pub struct Contd;
1414

15+
/// an empty ellipsis.
16+
pub struct Empty;
17+
1518
/// a horizontal utf-8 ellipsis.
1619
pub struct Horizontal;
1720

@@ -31,6 +34,14 @@ impl Ellipsis for Contd {
3134
}
3235
}
3336

37+
// === impl empty ===
38+
39+
impl Ellipsis for Empty {
40+
fn ellipsis() -> &'static str {
41+
""
42+
}
43+
}
44+
3445
// === impl horizontal ===
3546

3647
impl Ellipsis for Horizontal {

tests/str.rs

+69
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,72 @@ mod strs_can_be_truncated {
189189
});
190190
}
191191
}
192+
193+
mod strs_can_be_truncated_by_height {
194+
use super::*;
195+
196+
const INPUT: &str = "\
197+
first\n\
198+
second\n\
199+
third\
200+
";
201+
202+
#[test]
203+
fn zero_lines_is_empty() {
204+
INPUT
205+
.trim_to_height::<ellipsis::Ascii>(0)
206+
.pipe(|s| assert!(s.is_empty(), "{s} is not empty"))
207+
}
208+
209+
#[test]
210+
fn one_line_can_be_passed_through() {
211+
"value"
212+
.trim_to_height::<ellipsis::Empty>(1)
213+
.pipe(|s| assert_eq!(s, "value"))
214+
}
215+
216+
#[test]
217+
fn one_line_with_a_trailing_newlines_can_be_passed_through() {
218+
"value\n"
219+
.trim_to_height::<ellipsis::Empty>(1)
220+
.pipe(|s| assert_eq!(s, "value"))
221+
}
222+
223+
#[test]
224+
fn one_line_with_multiple_consecutibe_newlines_can_be_trimmed_to_a_height() {
225+
"top\n\
226+
\n\
227+
\n\
228+
bottom\n"
229+
.trim_to_height::<ellipsis::Empty>(4)
230+
.pipe(|s| {
231+
assert_eq!(
232+
s,
233+
"\
234+
top\n\
235+
\n\
236+
\n\
237+
bottom"
238+
)
239+
})
240+
}
241+
242+
#[test]
243+
fn two_lines_tall() {
244+
"\
245+
first\n\
246+
second\n\
247+
third\
248+
"
249+
.trim_to_height::<ellipsis::Ascii>(2)
250+
.pipe(|s| {
251+
assert_eq!(
252+
s,
253+
"\
254+
first\n\
255+
...\
256+
",
257+
)
258+
});
259+
}
260+
}

0 commit comments

Comments
 (0)