Skip to content

Commit 88f26e0

Browse files
mgeislerkbknapp
authored andcommitted
refactor: let textwrap handle long words
The textwrap crate can handle long words fine. By default they're broken to avoid lines longer than the specified width, but this can be disabled.
1 parent f89a012 commit 88f26e0

File tree

1 file changed

+11
-30
lines changed

1 file changed

+11
-30
lines changed

src/app/help.rs

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,6 @@ mod term_size {
2525
pub fn dimensions() -> Option<(usize, usize)> { None }
2626
}
2727

28-
macro_rules! find_longest {
29-
($help:expr) => {{
30-
let mut lw = 0;
31-
for l in $help.split(' ').map(|s| str_width(s)) {
32-
if l > lw {
33-
lw = l;
34-
}
35-
}
36-
lw
37-
}};
38-
}
39-
4028
fn str_width(s: &str) -> usize { UnicodeWidthStr::width(s) }
4129

4230
const TAB: &'static str = " ";
@@ -407,9 +395,8 @@ impl<'a> Help<'a> {
407395
// Determine how many newlines we need to insert
408396
debugln!("Help::write_before_after_help: Usable space: {}",
409397
self.term_w);
410-
let longest_w = find_longest!(help);
411398
help = help.replace("{n}", "\n");
412-
wrap_help(&mut help, longest_w, self.term_w);
399+
wrap_help(&mut help, self.term_w);
413400
} else {
414401
sdebugln!("No");
415402
}
@@ -450,9 +437,8 @@ impl<'a> Help<'a> {
450437
// Determine how many newlines we need to insert
451438
let avail_chars = self.term_w - spcs;
452439
debugln!("Help::help: Usable space...{}", avail_chars);
453-
let longest_w = find_longest!(help);
454440
help = help.replace("{n}", "\n");
455-
wrap_help(&mut help, longest_w, avail_chars);
441+
wrap_help(&mut help, avail_chars);
456442
} else {
457443
sdebugln!("No");
458444
}
@@ -629,9 +615,8 @@ impl<'a> Help<'a> {
629615
macro_rules! write_name {
630616
() => {{
631617
let mut name = parser.meta.name.clone();
632-
let longest_w = find_longest!(name);
633618
name = name.replace("{n}", "\n");
634-
wrap_help(&mut name, longest_w, self.term_w);
619+
wrap_help(&mut name, self.term_w);
635620
try!(color!(self, &*name, good));
636621
}};
637622
}
@@ -659,9 +644,8 @@ impl<'a> Help<'a> {
659644
macro_rules! write_thing {
660645
($thing:expr) => {{
661646
let mut owned_thing = $thing.to_owned();
662-
let longest_w = find_longest!(owned_thing);
663647
owned_thing = owned_thing.replace("{n}", "\n");
664-
wrap_help(&mut owned_thing, longest_w, self.term_w);
648+
wrap_help(&mut owned_thing, self.term_w);
665649
try!(write!(self.writer, "{}\n", &*owned_thing))
666650
}};
667651
}
@@ -923,15 +907,12 @@ impl<'a> Help<'a> {
923907
}
924908
}
925909

926-
fn wrap_help(help: &mut String, longest_w: usize, avail_chars: usize) {
927-
// Keep previous behavior of not wrapping at all if one of the
928-
// words would overflow the line.
929-
if longest_w < avail_chars {
930-
*help = help.lines()
931-
.map(|line| textwrap::fill(line, avail_chars))
932-
.collect::<Vec<String>>()
933-
.join("\n");
934-
}
910+
fn wrap_help(help: &mut String, avail_chars: usize) {
911+
let wrapper = textwrap::Wrapper::new(avail_chars).break_words(false);
912+
*help = help.lines()
913+
.map(|line| wrapper.fill(line))
914+
.collect::<Vec<String>>()
915+
.join("\n");
935916
}
936917

937918
#[cfg(test)]
@@ -941,7 +922,7 @@ mod test {
941922
#[test]
942923
fn wrap_help_last_word() {
943924
let mut help = String::from("foo bar baz");
944-
wrap_help(&mut help, 3, 5);
925+
wrap_help(&mut help, 5);
945926
assert_eq!(help, "foo\nbar\nbaz");
946927
}
947928
}

0 commit comments

Comments
 (0)