use std::default::Default;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Config {
max_history_size: usize,
history_duplicates: HistoryDuplicates,
history_ignore_space: bool,
completion_type: CompletionType,
completion_prompt_limit: usize,
keyseq_timeout: i32,
edit_mode: EditMode,
auto_add_history: bool,
bell_style: BellStyle,
color_mode: ColorMode,
output_stream: OutputStreamType,
tab_stop: usize,
}
impl Config {
pub fn builder() -> Builder {
Builder::new()
}
pub fn max_history_size(&self) -> usize {
self.max_history_size
}
pub(crate) fn set_max_history_size(&mut self, max_size: usize) {
self.max_history_size = max_size;
}
pub fn history_duplicates(&self) -> HistoryDuplicates {
self.history_duplicates
}
pub(crate) fn set_history_ignore_dups(&mut self, yes: bool) {
self.history_duplicates = if yes {
HistoryDuplicates::IgnoreConsecutive
} else {
HistoryDuplicates::AlwaysAdd
};
}
pub fn history_ignore_space(&self) -> bool {
self.history_ignore_space
}
pub(crate) fn set_history_ignore_space(&mut self, yes: bool) {
self.history_ignore_space = yes;
}
pub fn completion_type(&self) -> CompletionType {
self.completion_type
}
pub fn completion_prompt_limit(&self) -> usize {
self.completion_prompt_limit
}
pub fn keyseq_timeout(&self) -> i32 {
self.keyseq_timeout
}
pub fn edit_mode(&self) -> EditMode {
self.edit_mode
}
pub fn auto_add_history(&self) -> bool {
self.auto_add_history
}
pub fn bell_style(&self) -> BellStyle {
self.bell_style
}
pub fn color_mode(&self) -> ColorMode {
self.color_mode
}
pub(crate) fn set_color_mode(&mut self, color_mode: ColorMode) {
self.color_mode = color_mode;
}
pub fn output_stream(&self) -> OutputStreamType {
self.output_stream
}
pub(crate) fn set_output_stream(&mut self, stream: OutputStreamType) {
self.output_stream = stream;
}
pub fn tab_stop(&self) -> usize {
self.tab_stop
}
pub(crate) fn set_tab_stop(&mut self, tab_stop: usize) {
self.tab_stop = tab_stop;
}
}
impl Default for Config {
fn default() -> Self {
Self {
max_history_size: 100,
history_duplicates: HistoryDuplicates::IgnoreConsecutive,
history_ignore_space: false,
completion_type: CompletionType::Circular,
completion_prompt_limit: 100,
keyseq_timeout: -1,
edit_mode: EditMode::Emacs,
auto_add_history: false,
bell_style: BellStyle::default(),
color_mode: ColorMode::Enabled,
output_stream: OutputStreamType::Stdout,
tab_stop: 8,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BellStyle {
Audible,
None,
Visible,
}
impl Default for BellStyle {
#[cfg(any(windows, target_arch = "wasm32"))]
fn default() -> Self {
BellStyle::None
}
#[cfg(unix)]
fn default() -> Self {
BellStyle::Audible
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum HistoryDuplicates {
AlwaysAdd,
IgnoreConsecutive,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CompletionType {
Circular,
List,
#[cfg(all(unix, feature = "with-fuzzy"))]
Fuzzy,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum EditMode {
Emacs,
Vi,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ColorMode {
Enabled,
Forced,
Disabled,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum OutputStreamType {
Stderr,
Stdout,
}
#[derive(Clone, Debug, Default)]
pub struct Builder {
p: Config,
}
impl Builder {
pub fn new() -> Self {
Self {
p: Config::default(),
}
}
pub fn max_history_size(mut self, max_size: usize) -> Self {
self.set_max_history_size(max_size);
self
}
pub fn history_ignore_dups(mut self, yes: bool) -> Self {
self.set_history_ignore_dups(yes);
self
}
pub fn history_ignore_space(mut self, yes: bool) -> Self {
self.set_history_ignore_space(yes);
self
}
pub fn completion_type(mut self, completion_type: CompletionType) -> Self {
self.set_completion_type(completion_type);
self
}
pub fn completion_prompt_limit(mut self, completion_prompt_limit: usize) -> Self {
self.set_completion_prompt_limit(completion_prompt_limit);
self
}
pub fn keyseq_timeout(mut self, keyseq_timeout_ms: i32) -> Self {
self.set_keyseq_timeout(keyseq_timeout_ms);
self
}
pub fn edit_mode(mut self, edit_mode: EditMode) -> Self {
self.set_edit_mode(edit_mode);
self
}
pub fn auto_add_history(mut self, yes: bool) -> Self {
self.set_auto_add_history(yes);
self
}
pub fn bell_style(mut self, bell_style: BellStyle) -> Self {
self.set_bell_style(bell_style);
self
}
pub fn color_mode(mut self, color_mode: ColorMode) -> Self {
self.set_color_mode(color_mode);
self
}
pub fn output_stream(mut self, stream: OutputStreamType) -> Self {
self.set_output_stream(stream);
self
}
pub fn tab_stop(mut self, tab_stop: usize) -> Self {
self.set_tab_stop(tab_stop);
self
}
pub fn build(self) -> Config {
self.p
}
}
impl Configurer for Builder {
fn config_mut(&mut self) -> &mut Config {
&mut self.p
}
}
pub trait Configurer {
fn config_mut(&mut self) -> &mut Config;
fn set_max_history_size(&mut self, max_size: usize) {
self.config_mut().set_max_history_size(max_size);
}
fn set_history_ignore_dups(&mut self, yes: bool) {
self.config_mut().set_history_ignore_dups(yes);
}
fn set_history_ignore_space(&mut self, yes: bool) {
self.config_mut().set_history_ignore_space(yes);
}
fn set_completion_type(&mut self, completion_type: CompletionType) {
self.config_mut().completion_type = completion_type;
}
fn set_completion_prompt_limit(&mut self, completion_prompt_limit: usize) {
self.config_mut().completion_prompt_limit = completion_prompt_limit;
}
fn set_keyseq_timeout(&mut self, keyseq_timeout_ms: i32) {
self.config_mut().keyseq_timeout = keyseq_timeout_ms;
}
fn set_edit_mode(&mut self, edit_mode: EditMode) {
self.config_mut().edit_mode = edit_mode;
match edit_mode {
EditMode::Emacs => self.set_keyseq_timeout(-1),
EditMode::Vi => self.set_keyseq_timeout(500),
}
}
fn set_auto_add_history(&mut self, yes: bool) {
self.config_mut().auto_add_history = yes;
}
fn set_bell_style(&mut self, bell_style: BellStyle) {
self.config_mut().bell_style = bell_style;
}
fn set_color_mode(&mut self, color_mode: ColorMode) {
self.config_mut().set_color_mode(color_mode);
}
fn set_output_stream(&mut self, stream: OutputStreamType) {
self.config_mut().set_output_stream(stream);
}
fn set_tab_stop(&mut self, tab_stop: usize) {
self.config_mut().set_tab_stop(tab_stop);
}
}