use crate::
{
editor::Editor,
render::RenderConfig,
terminal::{ RealTerminal, TerminalOps },
};
pub type ValidatorFn = Box< dyn Fn( &str ) -> Result< (), String > >;
pub struct Builder
{
pub prompt: String,
pub allow_empty: bool,
pub min_length: Option< usize >,
pub max_length: Option< usize >,
pub validator: Option< ValidatorFn >,
pub initial_text: Option< String >,
pub placeholder: Option< String >,
pub show_line_numbers: bool,
pub show_status: bool,
pub show_char_count: bool,
pub color: bool,
}
impl Builder
{
pub fn new() -> Self
{
Self
{
prompt: String::new(),
allow_empty: true,
min_length: None,
max_length: None,
validator: None,
initial_text: None,
placeholder: None,
show_line_numbers: false,
show_status: false,
show_char_count: false,
color: true,
}
}
pub fn prompt( mut self, prompt: impl Into< String > ) -> Self
{
self.prompt = prompt.into();
self
}
pub fn allow_empty( mut self, allow: bool ) -> Self
{
self.allow_empty = allow;
self
}
pub fn min_length( mut self, min: usize ) -> Self
{
self.min_length = Some( min );
self
}
pub fn max_length( mut self, max: usize ) -> Self
{
self.max_length = Some( max );
self
}
pub fn validator< F >( mut self, validator: F ) -> Self
where
F: Fn( &str ) -> Result< (), String > + 'static,
{
self.validator = Some( Box::new( validator ) );
self
}
pub fn initial_text( mut self, text: impl Into< String > ) -> Self
{
self.initial_text = Some( text.into() );
self
}
pub fn placeholder( mut self, text: impl Into< String > ) -> Self
{
self.placeholder = Some( text.into() );
self
}
pub fn show_line_numbers( mut self, show: bool ) -> Self
{
self.show_line_numbers = show;
self
}
pub fn show_status( mut self, show: bool ) -> Self
{
self.show_status = show;
self
}
pub fn show_char_count( mut self, show: bool ) -> Self
{
self.show_char_count = show;
self
}
pub fn color( mut self, enable: bool ) -> Self
{
self.color = enable;
self
}
pub fn build( self ) -> Editor< RealTerminal >
{
let terminal = RealTerminal::new();
self.build_with( terminal )
}
pub fn build_with< T >( self, terminal: T ) -> Editor< T >
where
T: TerminalOps,
{
let render_config = RenderConfig
{
show_line_numbers: self.show_line_numbers,
show_status: self.show_status,
show_char_count: self.show_char_count,
color: self.color,
prompt: self.prompt,
placeholder: self.placeholder,
};
Editor
{
allow_empty: self.allow_empty,
min_length: self.min_length,
max_length: self.max_length,
validator: self.validator,
initial_text: self.initial_text,
render_config,
terminal,
}
}
}
impl Default for Builder
{
fn default() -> Self
{
Self::new()
}
}