mirror of
https://github.com/clap-rs/clap
synced 2025-03-04 23:37:32 +00:00
fix(Help Message): fixes a bug where small terminal sizes causing a loop
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
This commit is contained in:
parent
cc127f9825
commit
1d73b03552
1 changed files with 26 additions and 10 deletions
|
@ -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) {
|
||||
|
@ -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 {
|
||||
|
@ -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() {
|
||||
|
|
Loading…
Add table
Reference in a new issue