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

Support for tabs and hanging indent #211

Closed
cmcqueen opened this issue Sep 21, 2020 · 4 comments · Fixed by #281
Closed

Support for tabs and hanging indent #211

cmcqueen opened this issue Sep 21, 2020 · 4 comments · Fixed by #281

Comments

@cmcqueen
Copy link

The use case I'm thinking of is for a command-line program describing its options. Eg

-i <filename>   Input file. ELF, PE (portable executable)
                object file formats are accepted.
-o <filename>   Output file. Binary, Intel hex or Motorola
                hex file formats are accepted.
@mgeisler
Copy link
Owner

Hi @cmcqueen That's a great use case! We do have some support for this already via the initial_indent and subsequent_indent fields on Wrapper.

That is, you can do this kind of indentation by first constructing the "-i <filename> " part, set that as initial_indent and then construct a string with spaces of the same length for the subsequent_indent, Something like this:

pub fn main() {
    let options = vec![
        (
            'i',
            "filename",
            "Input file. ELF, PE (portable executable) object file formats are accepted.",
        ),
        (
            'o',
            "filename",
            "Output file. Binary, Intel hex or Motorola hex file formats are accepted.",
        ),
        (
            'v',
            "int",
            "Verbosity level. Set to 42 to receive more output than you ever wanted.",
        ),
    ];

    for (short, param, help) in &options {
        let left_column = format!("-{} <{}>", short, param);
        // Adjust 20 as needed, perhaps compute it from the longest parameter name
        let initial_indent = format!("{:1$}", left_column, 20); 
        let subsequent_indent = " ".repeat(20);
        let wrapper = textwrap::Wrapper::new(65)  // 65 is your line width
            .initial_indent(&initial_indent)
            .subsequent_indent(&subsequent_indent);
        println!("{}", wrapper.fill(help));
    }
}

Playground link.

If you want to be more careful, you should measure the length of the strings using the unicode-width crate, just like textwrap does internally.

Does this help? By the way, crates like clap already do this kind of formatting for you.

@mgeisler
Copy link
Owner

Having written the code above, I realize that it could be useful to ship a utility function that does this kind of thing.

@cmcqueen
Copy link
Author

Thanks, initial_indent and subsequent_indent are great (as for that Python package).

I guess they're not so easy to find in the documentation, since one doesn't find them on the main page, but has to follow the Wrapper struct link to find them. Whereas in the Python textwrap documentation, they're described succinctly on the one documentation page.

@mgeisler
Copy link
Owner

Thanks, that's good feedback! I think I need a Features section somewhere on the first page you see in the documentation. Like this:

Features

  • Supports Unicode characters and emojis.
  • Support for hanging indentation via the initial_indent and subsequent_indent.
  • Automatic handling of ANSI color codes.
  • more stuff as I think of it :-)

I'll keep the issue open so I remember to update the documentation like this before the next release.

mgeisler added a commit that referenced this issue Jan 23, 2021
This gives examples of hanging indentation, as discussed in #211.

Fixes #211.
mgeisler added a commit that referenced this issue Jan 23, 2021
This gives examples of hanging indentation, as discussed in #211.

Fixes #211.
mgeisler added a commit that referenced this issue Jan 23, 2021
This gives examples of hanging indentation, as discussed in #211.

Fixes #211.
mgeisler added a commit that referenced this issue Jan 24, 2021
This gives examples of hanging indentation, as discussed in #211.

Fixes #211.
mgeisler added a commit that referenced this issue Jan 24, 2021
This gives examples of hanging indentation, as discussed in #211.

Fixes #211.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants