1extern crate text_layout;
2use std::fmt::{self, Write};
3use text_layout::{FirstFit, Item, ParagraphLayout};
4
5fn layout_paragraph<'a, P: ParagraphLayout>(
6 paragraph: &'a str,
7 layout: &P,
8 max_width: usize,
9) -> Vec<&'a str> {
10 let mut items = Vec::new();
12 for c in paragraph.chars() {
13 items.push(if c.is_whitespace() && items.len() != 0 {
14 Item::Glue {
15 width: 1.0,
16 stretch: 1.0,
17 shrink: 0.0,
18 data: (),
19 }
20 } else {
21 Item::Box {
22 width: 1.0,
23 data: (),
24 }
25 });
26 }
27 items.push(Item::Glue {
28 width: 0.0,
29 stretch: 100000.0,
30 shrink: 0.0,
31 data: (),
32 });
33 items.push(Item::Penalty {
34 width: 0.0,
35 cost: f32::NEG_INFINITY,
36 flagged: true,
37 data: (),
38 });
39
40 let breaks = layout.layout_paragraph(&items, max_width as f32);
42
43 let mut cursor = 0;
45 let mut lines = Vec::new();
46 let mut start = 0;
47 for (i, _) in paragraph.chars().enumerate() {
48 if i == breaks[cursor].break_at {
49 lines.push(¶graph[start..i]);
50 start = i + 1;
51 cursor += 1;
52 }
53 }
54 lines.push(¶graph[start..]);
55 lines
56}
57
58fn layout_text() -> Result<String, fmt::Error> {
59 let text = " Far out in the uncharted backwaters of the unfashionable end of the western spiral arm of the Galaxy lies a small unregarded yellow sun. Orbiting this at a distance of roughly ninety-two million miles is an utterly insignificant little blue-green planet whose ape-descended life forms are so amazingly primitive that they still think digital watches are a pretty neat idea.";
60 let first_fit = FirstFit::new().with_threshold(f32::INFINITY);
61 let lines = layout_paragraph(&text, &first_fit, 80);
62 let mut result = String::new();
63 writeln!(&mut result, "┏{}┓", "━".repeat(80))?;
64 for l in lines {
65 let pad = 80 - l.chars().count();
66 writeln!(&mut result, "┃{}{}┃", l, " ".repeat(pad))?;
67 }
68 writeln!(&mut result, "┗{}┛", "━".repeat(80))?;
69 Ok(result)
70}
71
72fn main() -> Result<(), fmt::Error> {
73 print!("{}", layout_text()?);
74 Ok(())
75}
76
77#[cfg(test)]
78mod tests {
79 use super::*;
80
81 #[test]
82 fn readme() {
83 let expected = r#"┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
84┃ Far out in the uncharted backwaters of the unfashionable end of the western ┃
85┃spiral arm of the Galaxy lies a small unregarded yellow sun. Orbiting this at a ┃
86┃distance of roughly ninety-two million miles is an utterly insignificant little ┃
87┃blue-green planet whose ape-descended life forms are so amazingly primitive ┃
88┃that they still think digital watches are a pretty neat idea. ┃
89┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
90"#;
91 let actual = layout_text().unwrap();
92 assert!(actual == expected);
93 }
94}