tcalc_rustyline/
config.rs

1//! Customize line editor
2use std::default::Default;
3
4#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5pub struct Config {
6    /// Maximum number of entries in History.
7    max_history_size: usize,
8    history_duplicates: HistoryDuplicates,
9    history_ignore_space: bool,
10    completion_type: CompletionType,
11    /// When listing completion alternatives, only display
12    /// one screen of possibilities at a time.
13    completion_prompt_limit: usize,
14    /// Duration (milliseconds) Rustyline will wait for a character when reading an ambiguous key sequence.
15    keyseq_timeout: i32,
16}
17
18impl Config {
19    pub fn builder() -> Builder {
20        Builder::new()
21    }
22
23    /// Tell the maximum length (i.e. number of entries) for the history.
24    pub fn max_history_size(&self) -> usize {
25        self.max_history_size
26    }
27
28    /// Tell if lines which match the previous history entry are saved or not in the history list.
29    /// By default, they are ignored.
30    pub fn history_duplicates(&self) -> HistoryDuplicates {
31        self.history_duplicates
32    }
33
34    /// Tell if lines which begin with a space character are saved or not in the history list.
35    /// By default, they are saved.
36    pub fn history_ignore_space(&self) -> bool {
37        self.history_ignore_space
38    }
39
40    pub fn completion_type(&self) -> CompletionType {
41        self.completion_type
42    }
43
44    pub fn completion_prompt_limit(&self) -> usize {
45        self.completion_prompt_limit
46    }
47
48    pub fn keyseq_timeout(&self) -> i32 {
49        self.keyseq_timeout
50    }
51}
52
53impl Default for Config {
54    fn default() -> Config {
55        Config {
56            max_history_size: 100,
57            history_duplicates: HistoryDuplicates::IgnoreConsecutive,
58            history_ignore_space: false,
59            completion_type: CompletionType::Circular, // TODO Validate
60            completion_prompt_limit: 100,
61            keyseq_timeout: 500,
62        }
63    }
64}
65
66#[derive(Clone, Copy, Debug, PartialEq, Eq)]
67pub enum HistoryDuplicates {
68    AlwaysAdd,
69    IgnoreConsecutive,
70}
71
72#[derive(Clone, Copy, Debug, PartialEq, Eq)]
73pub enum CompletionType {
74    /// Complete the next full match (like in Vim by default)
75    Circular,
76    /// Complete till longest match.
77    /// When more than one match, list all matches
78    /// (like in Bash/Readline).
79    List,
80}
81
82#[derive(Debug)]
83pub struct Builder {
84    p: Config,
85}
86
87impl Builder {
88    pub fn new() -> Builder {
89        Builder { p: Config::default() }
90    }
91
92    /// Set the maximum length for the history.
93    pub fn max_history_size(mut self, max_size: usize) -> Builder {
94        self.p.max_history_size = max_size;
95        self
96    }
97
98    /// Tell if lines which match the previous history entry are saved or not in the history list.
99    /// By default, they are ignored.
100    pub fn history_ignore_dups(mut self, yes: bool) -> Builder {
101        self.p.history_duplicates = if yes {
102            HistoryDuplicates::IgnoreConsecutive
103        } else {
104            HistoryDuplicates::AlwaysAdd
105        };
106        self
107    }
108
109    /// Tell if lines which begin with a space character are saved or not in the history list.
110    /// By default, they are saved.
111    pub fn history_ignore_space(mut self, yes: bool) -> Builder {
112        self.p.history_ignore_space = yes;
113        self
114    }
115
116    /// Set `completion_type`.
117    pub fn completion_type(mut self, completion_type: CompletionType) -> Builder {
118        self.p.completion_type = completion_type;
119        self
120    }
121
122    pub fn completion_prompt_limit(mut self, completion_prompt_limit: usize) -> Builder {
123        self.p.completion_prompt_limit = completion_prompt_limit;
124        self
125    }
126
127    /// Set `keyseq_timeout` in milliseconds.
128    pub fn keyseq_timeout(mut self, keyseq_timeout_ms: i32) -> Builder {
129        self.p.keyseq_timeout = keyseq_timeout_ms;
130        self
131    }
132
133    pub fn build(self) -> Config {
134        self.p
135    }
136}