Skip to content

Commit 49f9dc1

Browse files
mgeislerkbknapp
authored andcommitted
refactor: let wrap_help return the wrapped string
Earlier, wrap_help was doing in-place modification on the help text. With the user of the textwrap crate, this is no longer the case and we can return the string directly.
1 parent 88f26e0 commit 49f9dc1

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

src/app/help.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,7 @@ impl<'a> Help<'a> {
395395
// Determine how many newlines we need to insert
396396
debugln!("Help::write_before_after_help: Usable space: {}",
397397
self.term_w);
398-
help = help.replace("{n}", "\n");
399-
wrap_help(&mut help, self.term_w);
398+
help = wrap_help(&help.replace("{n}", "\n"), self.term_w);
400399
} else {
401400
sdebugln!("No");
402401
}
@@ -437,8 +436,7 @@ impl<'a> Help<'a> {
437436
// Determine how many newlines we need to insert
438437
let avail_chars = self.term_w - spcs;
439438
debugln!("Help::help: Usable space...{}", avail_chars);
440-
help = help.replace("{n}", "\n");
441-
wrap_help(&mut help, avail_chars);
439+
help = wrap_help(&help.replace("{n}", "\n"), avail_chars);
442440
} else {
443441
sdebugln!("No");
444442
}
@@ -616,8 +614,7 @@ impl<'a> Help<'a> {
616614
() => {{
617615
let mut name = parser.meta.name.clone();
618616
name = name.replace("{n}", "\n");
619-
wrap_help(&mut name, self.term_w);
620-
try!(color!(self, &*name, good));
617+
try!(color!(self, wrap_help(&name, self.term_w), good));
621618
}};
622619
}
623620
if let Some(bn) = parser.meta.bin_name.as_ref() {
@@ -645,8 +642,8 @@ impl<'a> Help<'a> {
645642
($thing:expr) => {{
646643
let mut owned_thing = $thing.to_owned();
647644
owned_thing = owned_thing.replace("{n}", "\n");
648-
wrap_help(&mut owned_thing, self.term_w);
649-
try!(write!(self.writer, "{}\n", &*owned_thing))
645+
try!(write!(self.writer, "{}\n",
646+
wrap_help(&owned_thing, self.term_w)))
650647
}};
651648
}
652649
// Print the version
@@ -907,12 +904,12 @@ impl<'a> Help<'a> {
907904
}
908905
}
909906

910-
fn wrap_help(help: &mut String, avail_chars: usize) {
907+
fn wrap_help(help: &str, avail_chars: usize) -> String {
911908
let wrapper = textwrap::Wrapper::new(avail_chars).break_words(false);
912-
*help = help.lines()
909+
help.lines()
913910
.map(|line| wrapper.fill(line))
914911
.collect::<Vec<String>>()
915-
.join("\n");
912+
.join("\n")
916913
}
917914

918915
#[cfg(test)]
@@ -921,8 +918,7 @@ mod test {
921918

922919
#[test]
923920
fn wrap_help_last_word() {
924-
let mut help = String::from("foo bar baz");
925-
wrap_help(&mut help, 5);
926-
assert_eq!(help, "foo\nbar\nbaz");
921+
let help = String::from("foo bar baz");
922+
assert_eq!(wrap_help(&help, 5), "foo\nbar\nbaz");
927923
}
928924
}

0 commit comments

Comments
 (0)