1use std::default::Default;
3
4#[derive(Clone, Copy, Debug, PartialEq, Eq)]
6pub struct Config {
7 max_history_size: usize, history_duplicates: HistoryDuplicates,
10 history_ignore_space: bool,
11 completion_type: CompletionType,
12 completion_prompt_limit: usize,
15 keyseq_timeout: i32,
18 edit_mode: EditMode,
20 auto_add_history: bool,
23 bell_style: BellStyle,
25 color_mode: ColorMode,
27 behavior: Behavior,
29 tab_stop: usize,
31 indent_size: usize,
33 check_cursor_position: bool,
35 enable_bracketed_paste: bool,
37}
38
39impl Config {
40 #[must_use]
42 pub fn builder() -> Builder {
43 Builder::new()
44 }
45
46 #[must_use]
48 pub fn max_history_size(&self) -> usize {
49 self.max_history_size
50 }
51
52 pub(crate) fn set_max_history_size(&mut self, max_size: usize) {
53 self.max_history_size = max_size;
54 }
55
56 #[must_use]
61 pub fn history_duplicates(&self) -> HistoryDuplicates {
62 self.history_duplicates
63 }
64
65 pub(crate) fn set_history_ignore_dups(&mut self, yes: bool) {
66 self.history_duplicates = if yes {
67 HistoryDuplicates::IgnoreConsecutive
68 } else {
69 HistoryDuplicates::AlwaysAdd
70 };
71 }
72
73 #[must_use]
78 pub fn history_ignore_space(&self) -> bool {
79 self.history_ignore_space
80 }
81
82 pub(crate) fn set_history_ignore_space(&mut self, yes: bool) {
83 self.history_ignore_space = yes;
84 }
85
86 #[must_use]
90 pub fn completion_type(&self) -> CompletionType {
91 self.completion_type
92 }
93
94 #[must_use]
98 pub fn completion_prompt_limit(&self) -> usize {
99 self.completion_prompt_limit
100 }
101
102 #[must_use]
108 pub fn keyseq_timeout(&self) -> i32 {
109 self.keyseq_timeout
110 }
111
112 #[must_use]
114 pub fn edit_mode(&self) -> EditMode {
115 self.edit_mode
116 }
117
118 #[must_use]
122 pub fn auto_add_history(&self) -> bool {
123 self.auto_add_history
124 }
125
126 #[must_use]
128 pub fn bell_style(&self) -> BellStyle {
129 self.bell_style
130 }
131
132 #[must_use]
136 pub fn color_mode(&self) -> ColorMode {
137 self.color_mode
138 }
139
140 pub(crate) fn set_color_mode(&mut self, color_mode: ColorMode) {
141 self.color_mode = color_mode;
142 }
143
144 #[must_use]
148 pub fn behavior(&self) -> Behavior {
149 self.behavior
150 }
151
152 pub(crate) fn set_behavior(&mut self, behavior: Behavior) {
153 self.behavior = behavior;
154 }
155
156 #[must_use]
160 pub fn tab_stop(&self) -> usize {
161 self.tab_stop
162 }
163
164 pub(crate) fn set_tab_stop(&mut self, tab_stop: usize) {
165 self.tab_stop = tab_stop;
166 }
167
168 #[must_use]
172 pub fn check_cursor_position(&self) -> bool {
173 self.check_cursor_position
174 }
175
176 #[must_use]
180 pub fn indent_size(&self) -> usize {
181 self.indent_size
182 }
183
184 pub(crate) fn set_indent_size(&mut self, indent_size: usize) {
185 self.indent_size = indent_size;
186 }
187
188 #[must_use]
192 pub fn enable_bracketed_paste(&self) -> bool {
193 self.enable_bracketed_paste
194 }
195}
196
197impl Default for Config {
198 fn default() -> Self {
199 Self {
200 max_history_size: 100,
201 history_duplicates: HistoryDuplicates::IgnoreConsecutive,
202 history_ignore_space: false,
203 completion_type: CompletionType::Circular, completion_prompt_limit: 100,
205 keyseq_timeout: -1,
206 edit_mode: EditMode::Emacs,
207 auto_add_history: false,
208 bell_style: BellStyle::default(),
209 color_mode: ColorMode::Enabled,
210 behavior: Behavior::default(),
211 tab_stop: 8,
212 indent_size: 2,
213 check_cursor_position: false,
214 enable_bracketed_paste: true,
215 }
216 }
217}
218
219#[derive(Clone, Copy, Debug, PartialEq, Eq)]
221pub enum BellStyle {
222 Audible,
224 None,
226 Visible,
228}
229
230impl Default for BellStyle {
233 #[cfg(any(windows, target_arch = "wasm32"))]
234 fn default() -> Self {
235 BellStyle::None
236 }
237
238 #[cfg(unix)]
239 fn default() -> Self {
240 BellStyle::Audible
241 }
242}
243
244#[derive(Clone, Copy, Debug, PartialEq, Eq)]
246pub enum HistoryDuplicates {
247 AlwaysAdd,
249 IgnoreConsecutive,
251}
252
253#[derive(Clone, Copy, Debug, PartialEq, Eq)]
255#[non_exhaustive]
256pub enum CompletionType {
257 Circular,
259 List,
263
264 #[cfg(all(unix, feature = "with-fuzzy"))]
269 Fuzzy,
270}
271
272#[derive(Clone, Copy, Debug, PartialEq, Eq)]
274#[non_exhaustive]
275pub enum EditMode {
276 Emacs,
278 Vi,
280}
281
282#[derive(Clone, Copy, Debug, PartialEq, Eq)]
284#[non_exhaustive]
285pub enum ColorMode {
286 Enabled,
288 Forced,
290 Disabled,
292}
293
294#[derive(Clone, Copy, Debug, PartialEq, Eq)]
296#[non_exhaustive]
297pub enum Behavior {
298 Stdio,
300 PreferTerm,
303 }
307
308impl Default for Behavior {
309 fn default() -> Self {
310 Behavior::Stdio
311 }
312}
313
314#[derive(Clone, Debug, Default)]
316pub struct Builder {
317 p: Config,
318}
319
320impl Builder {
321 #[must_use]
323 pub fn new() -> Self {
324 Self {
325 p: Config::default(),
326 }
327 }
328
329 #[must_use]
331 pub fn max_history_size(mut self, max_size: usize) -> Self {
332 self.set_max_history_size(max_size);
333 self
334 }
335
336 #[must_use]
341 pub fn history_ignore_dups(mut self, yes: bool) -> Self {
342 self.set_history_ignore_dups(yes);
343 self
344 }
345
346 #[must_use]
351 pub fn history_ignore_space(mut self, yes: bool) -> Self {
352 self.set_history_ignore_space(yes);
353 self
354 }
355
356 #[must_use]
358 pub fn completion_type(mut self, completion_type: CompletionType) -> Self {
359 self.set_completion_type(completion_type);
360 self
361 }
362
363 #[must_use]
366 pub fn completion_prompt_limit(mut self, completion_prompt_limit: usize) -> Self {
367 self.set_completion_prompt_limit(completion_prompt_limit);
368 self
369 }
370
371 #[must_use]
377 pub fn keyseq_timeout(mut self, keyseq_timeout_ms: i32) -> Self {
378 self.set_keyseq_timeout(keyseq_timeout_ms);
379 self
380 }
381
382 #[must_use]
384 pub fn edit_mode(mut self, edit_mode: EditMode) -> Self {
385 self.set_edit_mode(edit_mode);
386 self
387 }
388
389 #[must_use]
393 pub fn auto_add_history(mut self, yes: bool) -> Self {
394 self.set_auto_add_history(yes);
395 self
396 }
397
398 #[must_use]
400 pub fn bell_style(mut self, bell_style: BellStyle) -> Self {
401 self.set_bell_style(bell_style);
402 self
403 }
404
405 #[must_use]
409 pub fn color_mode(mut self, color_mode: ColorMode) -> Self {
410 self.set_color_mode(color_mode);
411 self
412 }
413
414 #[must_use]
418 pub fn behavior(mut self, behavior: Behavior) -> Self {
419 self.set_behavior(behavior);
420 self
421 }
422
423 #[must_use]
427 pub fn tab_stop(mut self, tab_stop: usize) -> Self {
428 self.set_tab_stop(tab_stop);
429 self
430 }
431
432 #[must_use]
436 pub fn check_cursor_position(mut self, yes: bool) -> Self {
437 self.set_check_cursor_position(yes);
438 self
439 }
440
441 #[must_use]
445 pub fn indent_size(mut self, indent_size: usize) -> Self {
446 self.set_indent_size(indent_size);
447 self
448 }
449
450 #[must_use]
454 pub fn bracketed_paste(mut self, enabled: bool) -> Self {
455 self.enable_bracketed_paste(enabled);
456 self
457 }
458
459 #[must_use]
461 pub fn build(self) -> Config {
462 self.p
463 }
464}
465
466impl Configurer for Builder {
467 fn config_mut(&mut self) -> &mut Config {
468 &mut self.p
469 }
470}
471
472pub trait Configurer {
474 fn config_mut(&mut self) -> &mut Config;
476
477 fn set_max_history_size(&mut self, max_size: usize) {
479 self.config_mut().set_max_history_size(max_size);
480 }
481
482 fn set_history_ignore_dups(&mut self, yes: bool) {
487 self.config_mut().set_history_ignore_dups(yes);
488 }
489
490 fn set_history_ignore_space(&mut self, yes: bool) {
495 self.config_mut().set_history_ignore_space(yes);
496 }
497 fn set_completion_type(&mut self, completion_type: CompletionType) {
499 self.config_mut().completion_type = completion_type;
500 }
501
502 fn set_completion_prompt_limit(&mut self, completion_prompt_limit: usize) {
505 self.config_mut().completion_prompt_limit = completion_prompt_limit;
506 }
507
508 fn set_keyseq_timeout(&mut self, keyseq_timeout_ms: i32) {
510 self.config_mut().keyseq_timeout = keyseq_timeout_ms;
511 }
512
513 fn set_edit_mode(&mut self, edit_mode: EditMode) {
515 self.config_mut().edit_mode = edit_mode;
516 match edit_mode {
517 EditMode::Emacs => self.set_keyseq_timeout(-1), EditMode::Vi => self.set_keyseq_timeout(500),
519 }
520 }
521
522 fn set_auto_add_history(&mut self, yes: bool) {
526 self.config_mut().auto_add_history = yes;
527 }
528
529 fn set_bell_style(&mut self, bell_style: BellStyle) {
531 self.config_mut().bell_style = bell_style;
532 }
533
534 fn set_color_mode(&mut self, color_mode: ColorMode) {
538 self.config_mut().set_color_mode(color_mode);
539 }
540
541 fn set_behavior(&mut self, behavior: Behavior) {
545 self.config_mut().set_behavior(behavior);
546 }
547
548 fn set_tab_stop(&mut self, tab_stop: usize) {
552 self.config_mut().set_tab_stop(tab_stop);
553 }
554
555 fn set_check_cursor_position(&mut self, yes: bool) {
559 self.config_mut().check_cursor_position = yes;
560 }
561 fn set_indent_size(&mut self, size: usize) {
565 self.config_mut().set_indent_size(size);
566 }
567
568 fn enable_bracketed_paste(&mut self, enabled: bool) {
572 self.config_mut().enable_bracketed_paste = enabled;
573 }
574}