pub mod backend;
use std::{borrow::Cow, io};
use crate::backend::{default_backend, Backend};
pub const DEFAULT_TITLE: &str = "Input";
pub const DEFAULT_OK_LABEL: &str = "OK";
pub const DEFAULT_CANCEL_LABEL: &str = "Cancel";
#[derive(Clone, Debug, PartialEq, Eq, Default)]
pub enum InputMode {
#[default]
Text,
Password,
Multiline,
}
impl InputMode {
#[allow(dead_code)]
fn as_str(&self) -> &'static str {
match self {
InputMode::Text => "text",
InputMode::Password => "password",
InputMode::Multiline => "multiline",
}
}
}
#[derive(Clone, Debug)]
pub struct InputBox<'a> {
pub title: Option<Cow<'a, str>>,
pub prompt: Option<Cow<'a, str>>,
pub default: Cow<'a, str>,
pub mode: InputMode,
pub width: Option<u32>,
pub height: Option<u32>,
pub cancel_label: Option<Cow<'a, str>>,
pub ok_label: Option<Cow<'a, str>>,
pub auto_wrap: bool,
pub scroll_to_end: bool,
pub quiet: bool,
}
impl Default for InputBox<'_> {
fn default() -> Self {
Self {
title: None,
prompt: None,
default: "".into(),
mode: InputMode::default(),
width: None,
height: None,
cancel_label: None,
ok_label: None,
auto_wrap: true,
scroll_to_end: false,
quiet: false,
}
}
}
impl<'a> InputBox<'a> {
pub fn new() -> Self {
Self::default()
}
pub fn title(mut self, title: impl Into<Cow<'a, str>>) -> Self {
self.title = Some(title.into());
self
}
pub fn prompt(mut self, prompt: impl Into<Cow<'a, str>>) -> Self {
self.prompt = Some(prompt.into());
self
}
pub fn default_text(mut self, default: impl Into<Cow<'a, str>>) -> Self {
self.default = default.into();
self
}
pub fn mode(mut self, mode: InputMode) -> Self {
self.mode = mode;
self
}
pub fn width(mut self, width: u32) -> Self {
self.width = Some(width);
self
}
pub fn height(mut self, height: u32) -> Self {
self.height = Some(height);
self
}
pub fn cancel_label(mut self, label: impl Into<Cow<'a, str>>) -> Self {
self.cancel_label = Some(label.into());
self
}
pub fn ok_label(mut self, label: impl Into<Cow<'a, str>>) -> Self {
self.ok_label = Some(label.into());
self
}
pub fn auto_wrap(mut self, auto_wrap: bool) -> Self {
self.auto_wrap = auto_wrap;
self
}
pub fn scroll_to_end(mut self, scroll_to_end: bool) -> Self {
self.scroll_to_end = scroll_to_end;
self
}
pub fn quiet(mut self, quiet: bool) -> Self {
self.quiet = quiet;
self
}
pub fn show_async(
&self,
callback: impl FnOnce(io::Result<Option<String>>) + Send + 'static,
) -> io::Result<()> {
default_backend().execute_async(self, Box::new(callback))
}
pub fn show_with_async(
&self,
backend: &dyn Backend,
callback: impl FnOnce(io::Result<Option<String>>) + Send + 'static,
) -> io::Result<()> {
backend.execute_async(self, Box::new(callback))
}
pub fn show(&self) -> io::Result<Option<String>> {
default_backend().execute(self)
}
pub fn show_with(&self, backend: &dyn Backend) -> io::Result<Option<String>> {
backend.execute(self)
}
}