diff --git a/derive/src/help.rs b/derive/src/help.rs index 4ab16ba..877a3d8 100644 --- a/derive/src/help.rs +++ b/derive/src/help.rs @@ -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!() }; diff --git a/src/lib.rs b/src/lib.rs index 72321c8..96c1a38 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,6 +14,7 @@ pub use value::{Value, ValueError, ValueResult}; use std::{ ffi::{OsStr, OsString}, + io::Write, marker::PhantomData, }; @@ -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, +) -> 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;