Skip to content

Commit

Permalink
fix(Help Message): fixes a bug where small terminal sizes causing a loop
Browse files Browse the repository at this point in the history
Now if the terminal size is too small to properly wrap the help text
lines, it will default to just wrapping normalling as it should.

This is determined on a per help text basis, so because the terminal
size is too small for a single argument's help to wrap properly, all
other arguments will still wrap and align correctly. Only the one
affected argument will not align properly.

Closes #453
  • Loading branch information
kbknapp committed Mar 16, 2016
1 parent cc127f9 commit 1d73b03
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions src/args/help_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,10 @@ impl<'a, 'n, 'e, A> HelpWriter<'a, A> where A: AnyArg<'n, 'e> {
self.l + 12
};
// determine if our help fits or needs to wrap
let too_long = self.term_w.is_some() && (spcs + h.len() + spec_vals.len() >= self.term_w.unwrap_or(0));
let width = self.term_w.unwrap_or(0);
debugln!("Term width...{}", width);
let too_long = self.term_w.is_some() && (spcs + h.len() + spec_vals.len() >= width);
debugln!("Too long...{:?}", too_long);

// Is help on next line, if so newline + 2x tab
if self.nlh || self.a.is_set(ArgSettings::NextLineHelp) {
Expand All @@ -148,15 +151,26 @@ impl<'a, 'n, 'e, A> HelpWriter<'a, A> where A: AnyArg<'n, 'e> {
debug!("Too long...");
if too_long {
sdebugln!("Yes");
if let Some(width) = self.term_w {
help.push_str(h);
help.push_str(&*spec_vals);
debugln!("term width: {}", width);
debugln!("help: {}", help);
debugln!("help len: {}", help.len());
// Determine how many newlines we need to insert
let avail_chars = width - spcs;
debugln!("Usable space: {}", avail_chars);
help.push_str(h);
help.push_str(&*spec_vals);
debugln!("help: {}", help);
debugln!("help len: {}", help.len());
// Determine how many newlines we need to insert
let avail_chars = width - spcs;
debugln!("Usable space: {}", avail_chars);
let longest_w = {
let mut lw = 0;
for l in help.split(' ').map(|s| s.len()) {
if l > lw {
lw = l;
}
}
lw
};
debugln!("Longest word...{}", longest_w);
debug!("Enough space...");
if longest_w < avail_chars {
sdebugln!("Yes");
let mut indices = vec![];
let mut idx = 0;
loop {
Expand All @@ -183,6 +197,8 @@ impl<'a, 'n, 'e, A> HelpWriter<'a, A> where A: AnyArg<'n, 'e> {
help.insert(j + 1 , 'n');
help.insert(j + 2, '}');
}
} else {
sdebugln!("No");
}
} else { sdebugln!("No"); }
let help = if !help.is_empty() {
Expand Down

0 comments on commit 1d73b03

Please sign in to comment.