mod prompt;
#[cfg(test)]
mod test;
use std::fmt::Display;
use crate::{
config::get_configuration,
error::{InquireError, InquireResult},
prompts::prompt::Prompt,
terminal::get_default_terminal,
ui::{Backend, Key, KeyModifiers, RenderConfig, ReorderBackend},
InnerAction, InputAction,
};
pub use prompt::ReorderPrompt;
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ReorderAction {
FilterInput(InputAction),
MoveUp,
MoveDown,
PageUp,
PageDown,
MoveToStart,
MoveToEnd,
MoveItemUp,
MoveItemDown,
}
impl InnerAction for ReorderAction {
type Config = ReorderConfig;
fn from_key(key: Key, config: &ReorderConfig) -> Option<Self> {
if config.vim_mode {
let action = match key {
Key::Char('k', KeyModifiers::NONE) => Some(Self::MoveUp),
Key::Char('j', KeyModifiers::NONE) => Some(Self::MoveDown),
Key::Char('K', KeyModifiers::SHIFT) => Some(Self::MoveItemUp),
Key::Char('J', KeyModifiers::SHIFT) => Some(Self::MoveItemDown),
_ => None,
};
if action.is_some() {
return action;
}
}
let action = match key {
Key::Up(KeyModifiers::NONE) | Key::Char('p', KeyModifiers::CONTROL) => Self::MoveUp,
Key::Down(KeyModifiers::NONE) | Key::Char('n', KeyModifiers::CONTROL) => Self::MoveDown,
Key::PageUp(_) => Self::PageUp,
Key::PageDown(_) => Self::PageDown,
Key::Home => Self::MoveToStart,
Key::End => Self::MoveToEnd,
Key::Up(KeyModifiers::CONTROL) => Self::MoveItemUp,
Key::Down(KeyModifiers::CONTROL) => Self::MoveItemDown,
key => match InputAction::from_key(key, &()) {
Some(action) => Self::FilterInput(action),
None => return None,
},
};
Some(action)
}
}
#[derive(Copy, Clone, Debug)]
pub struct ReorderConfig {
pub vim_mode: bool,
pub page_size: usize,
pub reset_cursor: bool,
}
#[derive(Clone)]
pub struct Reorder<'a, T> {
pub message: &'a str,
pub options: Vec<T>,
pub help_message: Option<&'a str>,
pub page_size: usize,
pub vim_mode: bool,
pub starting_cursor: usize,
pub starting_filter_input: Option<&'a str>,
pub reset_cursor: bool,
pub filter_input_enabled: bool,
pub formatter: &'a dyn Fn(&[T]) -> String,
pub render_config: RenderConfig<'a>,
}
impl<'a, T> Reorder<'a, T>
where
T: Display + Clone,
{
pub const DEFAULT_FORMATTER: &'a dyn Fn(&[T]) -> String = &|items| {
items
.iter()
.map(|item| item.to_string())
.collect::<Vec<String>>()
.join(", ")
};
pub const DEFAULT_HELP_MESSAGE: Option<&'a str> =
Some("↑↓ to move cursor, Ctrl+↑↓ to move item, type to filter");
pub const DEFAULT_PAGE_SIZE: usize = 7;
pub const DEFAULT_VIM_MODE: bool = false;
pub const DEFAULT_STARTING_CURSOR: usize = 0;
pub const DEFAULT_RESET_CURSOR: bool = true;
pub const DEFAULT_FILTER_INPUT_ENABLED: bool = true;
pub fn new(message: &'a str, options: Vec<T>) -> Self {
Self {
message,
options,
help_message: Self::DEFAULT_HELP_MESSAGE,
page_size: Self::DEFAULT_PAGE_SIZE,
vim_mode: Self::DEFAULT_VIM_MODE,
starting_cursor: Self::DEFAULT_STARTING_CURSOR,
starting_filter_input: None,
reset_cursor: Self::DEFAULT_RESET_CURSOR,
filter_input_enabled: Self::DEFAULT_FILTER_INPUT_ENABLED,
formatter: Self::DEFAULT_FORMATTER,
render_config: get_configuration(),
}
}
pub fn with_render_config(mut self, render_config: RenderConfig<'a>) -> Self {
self.render_config = render_config;
self
}
pub fn with_help_message(mut self, message: &'a str) -> Self {
self.help_message = Some(message);
self
}
pub fn without_help_message(mut self) -> Self {
self.help_message = None;
self
}
pub fn with_page_size(mut self, page_size: usize) -> Self {
self.page_size = page_size;
self
}
pub fn with_vim_mode(mut self, vim_mode: bool) -> Self {
self.vim_mode = vim_mode;
self
}
pub fn with_starting_cursor(mut self, starting_cursor: usize) -> Self {
self.starting_cursor = starting_cursor;
self
}
pub fn with_starting_filter_input(mut self, starting_filter_input: &'a str) -> Self {
self.starting_filter_input = Some(starting_filter_input);
self
}
pub fn with_reset_cursor(mut self, reset_cursor: bool) -> Self {
self.reset_cursor = reset_cursor;
self
}
pub fn prompt_with_backend<B: ReorderBackend>(self, backend: &mut B) -> InquireResult<Vec<T>> {
ReorderPrompt::new(self)?.prompt(backend)
}
pub fn without_filtering(mut self) -> Self {
self.filter_input_enabled = false;
self
}
pub fn with_formatter(mut self, formatter: &'a dyn Fn(&[T]) -> String) -> Self {
self.formatter = formatter;
self
}
pub fn prompt(self) -> InquireResult<Vec<T>> {
self.raw_prompt()
}
pub fn prompt_skippable(self) -> InquireResult<Option<Vec<T>>> {
match self.prompt() {
Ok(answer) => Ok(Some(answer)),
Err(InquireError::OperationCanceled) => Ok(None),
Err(err) => Err(err),
}
}
pub fn raw_prompt_skippable(self) -> InquireResult<Option<Vec<T>>> {
match self.raw_prompt() {
Ok(answer) => Ok(Some(answer)),
Err(InquireError::OperationCanceled) => Ok(None),
Err(err) => Err(err),
}
}
pub fn raw_prompt(self) -> InquireResult<Vec<T>> {
let (input_reader, terminal) = get_default_terminal()?;
let mut backend = Backend::new(input_reader, terminal, self.render_config)?;
self.prompt_with_backend(&mut backend)
}
}