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

Add header and footer options to (Fuzzy)Select (for making borders/tables) #305

Open
Andrew15-5 opened this issue Mar 6, 2024 · 0 comments

Comments

@Andrew15-5
Copy link

Right now I only was able to make something like this:

image

But I had to redefine the style:

    let theme = ColorfulTheme {
        prompt_style: Style::new().for_stderr().white(),
        prompt_suffix: style(format!(
            "\n  {}\n  {:11}{:8}{:7}{:15}{}\n  {}",
            "-".repeat(58),
            "name",
            "fstype",
            "size",
            "label",
            "partlabel",
            "-".repeat(58),
        )).for_stderr().bold(),
        ..Default::default()
    };
FuzzySelect::with_theme(&theme)
.with_prompt("Which partition do you want to mount?")

And separately also define the item's Display trait implementation:

impl Display for Partition {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let map = |x| format!("{}", x);
        write!(
            f,
            "{:11}{:8}{:7}{:15}{}",
            self.name,
            self.fstype,
            self.size,
            self.label.as_ref().map_or("".into(), map).as_str(),
            self.partlabel.as_ref().map_or("".into(), map).as_str(),
        )
    }
}

I want to use this:

    let selected_index = FuzzySelect::with_theme(&theme)
        .with_prompt("Which partition do you want to mount?")
        .header(format!(
            "  {}\n  {:11}{:8}{:7}{:15}{}\n  {}",
            "-".repeat(58),
            "name",
            "fstype",
            "size",
            "label",
            "partlabel",
            "-".repeat(58),
        ))
        .footer(format!("  {}", "-".repeat(58)))
        .items(&items)
        .default(0)
        .interact_opt()
        .context("Failed to start interactive question.")?
        .unwrap_or_else(|| process::exit(1));

Where header will go under the prompt/cursor and before the first item. Footer goes after the first item. By default, there should be no header/footer. The only way I see right now fixing the implicit LF being inserted when header/footer is set is to use enum. So if they are set, then you would need to wrap your string in Some() and by default it is None.

Then also add the header_style and footer_style to the ColorfulTheme.

So basically I want this library to provide a nice way to create "tables" or just frames/borders to make prompts look even prettier.

Header can't go above the prompt, because this can be achieved by redefining the prompt_prefex (which is also not ideal if it has to be more than a few chars, but a good first middle ground).

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

No branches or pull requests

1 participant