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 on select, fuzzy_select, multi_select and sort #308

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion examples/select.rs
@@ -1,3 +1,4 @@
use console::style;
use dialoguer::{theme::ColorfulTheme, Select};

fn main() {
Expand Down Expand Up @@ -30,7 +31,12 @@ fn main() {
println!("You didn't select anything!");
}

let selection = Select::with_theme(&ColorfulTheme::default())
let theme = ColorfulTheme {
header: style("-----------------------".to_string()).for_stderr(),
footer: style("-----------------------".to_string()).for_stderr(),
..ColorfulTheme::default()
};
let selection = Select::with_theme(&theme)
.with_prompt("Pick your flavor, hint it might be on the second page")
.default(0)
.max_length(2)
Expand Down
5 changes: 5 additions & 0 deletions src/prompts/fuzzy_select.rs
Expand Up @@ -238,6 +238,8 @@ impl FuzzySelect<'_> {
render.clear()?;
render.fuzzy_select_prompt(self.prompt.as_str(), &search_term, byte_indices[cursor])?;

render.header()?;

// Maps all items to a tuple of item and its match score.
let mut filtered_list = self
.items
Expand All @@ -263,6 +265,9 @@ impl FuzzySelect<'_> {
&search_term,
)?;
}

render.footer()?;

term.flush()?;

match (term.read_key()?, sel, vim_mode) {
Expand Down
4 changes: 4 additions & 0 deletions src/prompts/multi_select.rs
Expand Up @@ -238,6 +238,8 @@ impl MultiSelect<'_> {
.render_prompt(|paging_info| render.multi_select_prompt(prompt, paging_info))?;
}

render.header()?;

for (idx, item) in self
.items
.iter()
Expand All @@ -248,6 +250,8 @@ impl MultiSelect<'_> {
render.multi_select_prompt_item(item, checked[idx], sel == idx)?;
}

render.footer()?;

term.flush()?;

match term.read_key()? {
Expand Down
7 changes: 5 additions & 2 deletions src/prompts/select.rs
@@ -1,6 +1,5 @@
use std::{io, ops::Rem};

use console::{Key, Term};
use std::{io, ops::Rem};

use crate::{
theme::{render::TermThemeRenderer, SimpleTheme, Theme},
Expand Down Expand Up @@ -226,6 +225,8 @@ impl Select<'_> {
paging.render_prompt(|paging_info| render.select_prompt(prompt, paging_info))?;
}

render.header()?;

for (idx, item) in self
.items
.iter()
Expand All @@ -236,6 +237,8 @@ impl Select<'_> {
render.select_prompt_item(item, sel == idx)?;
}

render.footer()?;

term.flush()?;

match term.read_key()? {
Expand Down
4 changes: 4 additions & 0 deletions src/prompts/sort.rs
Expand Up @@ -205,6 +205,8 @@ impl Sort<'_> {
paging.render_prompt(|paging_info| render.sort_prompt(prompt, paging_info))?;
}

render.header()?;

for (idx, item) in order
.iter()
.enumerate()
Expand All @@ -214,6 +216,8 @@ impl Sort<'_> {
render.sort_prompt_item(&self.items[*item], checked, sel == idx)?;
}

render.footer()?;

term.flush()?;

match term.read_key()? {
Expand Down
16 changes: 16 additions & 0 deletions src/theme/colorful.rs
Expand Up @@ -50,6 +50,10 @@ pub struct ColorfulTheme {
// Formats the highlighting if matched characters
#[cfg(feature = "fuzzy-select")]
pub fuzzy_match_highlight_style: Style,
// Header for multiple items components
pub header: StyledObject<String>,
// Footer for multiple items components
pub footer: StyledObject<String>,
}

impl Default for ColorfulTheme {
Expand Down Expand Up @@ -77,6 +81,8 @@ impl Default for ColorfulTheme {
fuzzy_cursor_style: Style::new().for_stderr().black().on_white(),
#[cfg(feature = "fuzzy-select")]
fuzzy_match_highlight_style: Style::new().for_stderr().bold(),
header: style("".to_string()).for_stderr(),
footer: style("".to_string()).for_stderr(),
}
}
}
Expand Down Expand Up @@ -423,4 +429,14 @@ impl Theme for ColorfulTheme {
let prompt_suffix = &self.prompt_suffix;
write!(f, "{prompt_suffix} {st_head}{st_cursor}{st_tail}",)
}

/// Format header.
fn format_header(&self, f: &mut dyn fmt::Write) -> fmt::Result {
write!(f, "{}\n", &self.header)
}

/// Format footer.
fn format_footer(&self, f: &mut dyn fmt::Write) -> fmt::Result {
write!(f, "{}", &self.footer)
}
}
10 changes: 10 additions & 0 deletions src/theme/mod.rs
Expand Up @@ -262,4 +262,14 @@ pub trait Theme {
let (st_head, st_tail) = search_term.split_at(bytes_pos);
write!(f, "{st_head}|{st_tail}")
}

/// Format header.
fn format_header(&self, _f: &mut dyn fmt::Write) -> fmt::Result {
Ok(())
}

/// Format footer.
fn format_footer(&self, _f: &mut dyn fmt::Write) -> fmt::Result {
Ok(())
}
}
8 changes: 8 additions & 0 deletions src/theme/render.rs
Expand Up @@ -259,4 +259,12 @@ impl<'a> TermThemeRenderer<'a> {
self.height = 0;
Ok(())
}

pub fn header(&mut self) -> Result<usize> {
self.write_formatted_str(|this, buf| this.theme.format_header(buf))
}

pub fn footer(&mut self) -> Result<usize> {
self.write_formatted_str(|this, buf| this.theme.format_footer(buf))
}
}