Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extract printing flags to a function #56

Merged
merged 1 commit into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 1 addition & 30 deletions derive/src/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,36 +75,7 @@ pub fn help_string(
}

let options = if !options.is_empty() {
let options = quote!([#(#options),*]);
quote!(
writeln!(w, "\nOptions:")?;
for (flags, help_string) in #options {
let indent = " ".repeat(#indent);

let mut help_lines = help_string.lines();
write!(w, "{}", &indent)?;
write!(w, "{}", &flags)?;

if flags.len() <= #width {
let line = match help_lines.next() {
Some(line) => line,
None => {
writeln!(w)?;
continue;
},
};
let help_indent = " ".repeat(#width-flags.len()+2);
writeln!(w, "{}{}", help_indent, line)?;
} else {
writeln!(w, "\n")?;
}

let help_indent = " ".repeat(#width+#indent+2);
for line in help_lines {
writeln!(w, "{}{}", help_indent, line)?;
}
}
)
quote!(::uutils_args::print_flags(&mut w, #indent, #width, [#(#options),*])?;)
} else {
quote!()
};
Expand Down
35 changes: 35 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub use value::{Value, ValueError, ValueResult};

use std::{
ffi::{OsStr, OsString},
io::Write,
marker::PhantomData,
};

Expand Down Expand Up @@ -316,6 +317,40 @@ pub fn filter_suggestions(input: &str, long_options: &[&str], prefix: &str) -> V
.collect()
}

pub fn print_flags(
mut w: impl Write,
indent_size: usize,
width: usize,
options: impl IntoIterator<Item = (&'static str, &'static str)>,
) -> std::io::Result<()> {
let indent = " ".repeat(indent_size);
writeln!(w, "\nOptions:")?;
for (flags, help_string) in options {
let mut help_lines = help_string.lines();
write!(w, "{}{}", &indent, &flags)?;

if flags.len() <= width {
let line = match help_lines.next() {
Some(line) => line,
None => {
writeln!(w)?;
continue;
}
};
let help_indent = " ".repeat(width - flags.len() + 2);
writeln!(w, "{}{}", help_indent, line)?;
} else {
writeln!(w)?;
}

let help_indent = " ".repeat(width + indent_size + 2);
for line in help_lines {
writeln!(w, "{}{}", help_indent, line)?;
}
}
Ok(())
}

#[cfg(test)]
mod test {
use std::ffi::OsStr;
Expand Down
Loading