1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
//! Customize line editor
use crate::Result;
use std::default::Default;
/// User preferences
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Config {
/// Maximum number of entries in History.
max_history_size: usize, // history_max_entries
history_duplicates: HistoryDuplicates,
history_ignore_space: bool,
completion_type: CompletionType,
/// When listing completion alternatives, only display
/// one screen of possibilities at a time.
completion_prompt_limit: usize,
/// Duration (milliseconds) Rustyline will wait for a character when
/// reading an ambiguous key sequence.
keyseq_timeout: Option<u16>,
/// Emacs or Vi mode
edit_mode: EditMode,
/// If true, each nonblank line returned by `readline` will be
/// automatically added to the history.
auto_add_history: bool,
/// Beep or Flash or nothing
bell_style: BellStyle,
/// if colors should be enabled.
color_mode: ColorMode,
/// Whether to use stdio or not
behavior: Behavior,
/// Horizontal space taken by a tab.
tab_stop: usize,
/// Indentation size for indent/dedent commands
indent_size: usize,
/// Check if cursor position is at leftmost before displaying prompt
check_cursor_position: bool,
/// Bracketed paste on unix platform
enable_bracketed_paste: bool,
/// Whether to disable or not the signals in termios
enable_signals: bool,
}
impl Config {
/// Returns a `Config` builder.
#[must_use]
pub fn builder() -> Builder {
Builder::new()
}
/// Tell the maximum length (i.e. number of entries) for the history.
#[must_use]
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;
}
/// Tell if lines which match the previous history entry are saved or not
/// in the history list.
///
/// By default, they are ignored.
#[must_use]
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
};
}
/// Tell if lines which begin with a space character are saved or not in
/// the history list.
///
/// By default, they are saved.
#[must_use]
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;
}
/// Completion behaviour.
///
/// By default, `CompletionType::Circular`.
#[must_use]
pub fn completion_type(&self) -> CompletionType {
self.completion_type
}
/// When listing completion alternatives, only display
/// one screen of possibilities at a time (used for `CompletionType::List`
/// mode).
#[must_use]
pub fn completion_prompt_limit(&self) -> usize {
self.completion_prompt_limit
}
/// Duration (milliseconds) Rustyline will wait for a character when
/// reading an ambiguous key sequence (used for `EditMode::Vi` mode on unix
/// platform).
///
/// By default, no timeout (-1) or 500ms if `EditMode::Vi` is activated.
#[must_use]
pub fn keyseq_timeout(&self) -> Option<u16> {
self.keyseq_timeout
}
/// Emacs or Vi mode
#[must_use]
pub fn edit_mode(&self) -> EditMode {
self.edit_mode
}
/// Tell if lines are automatically added to the history.
///
/// By default, they are not.
#[must_use]
pub fn auto_add_history(&self) -> bool {
self.auto_add_history
}
/// Bell style: beep, flash or nothing.
#[must_use]
pub fn bell_style(&self) -> BellStyle {
self.bell_style
}
/// Tell if colors should be enabled.
///
/// By default, they are except if stdout is not a TTY.
#[must_use]
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;
}
/// Whether to use stdio or not
///
/// By default, stdio is used.
#[must_use]
pub fn behavior(&self) -> Behavior {
self.behavior
}
pub(crate) fn set_behavior(&mut self, behavior: Behavior) {
self.behavior = behavior;
}
/// Horizontal space taken by a tab.
///
/// By default, 8.
#[must_use]
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;
}
/// Check if cursor position is at leftmost before displaying prompt.
///
/// By default, we don't check.
#[must_use]
pub fn check_cursor_position(&self) -> bool {
self.check_cursor_position
}
/// Indentation size used by indentation commands
///
/// By default, 2.
#[must_use]
pub fn indent_size(&self) -> usize {
self.indent_size
}
pub(crate) fn set_indent_size(&mut self, indent_size: usize) {
self.indent_size = indent_size;
}
/// Bracketed paste on unix platform
///
/// By default, it's enabled.
#[must_use]
pub fn enable_bracketed_paste(&self) -> bool {
self.enable_bracketed_paste
}
/// Enable or disable signals in termios
///
/// By default, it's disabled.
#[must_use]
pub fn enable_signals(&self) -> bool {
self.enable_signals
}
pub(crate) fn set_enable_signals(&mut self, enable_signals: bool) {
self.enable_signals = enable_signals;
}
}
impl Default for Config {
fn default() -> Self {
Self {
max_history_size: 100,
history_duplicates: HistoryDuplicates::IgnoreConsecutive,
history_ignore_space: false,
completion_type: CompletionType::Circular, // TODO Validate
completion_prompt_limit: 100,
keyseq_timeout: None,
edit_mode: EditMode::Emacs,
auto_add_history: false,
bell_style: BellStyle::default(),
color_mode: ColorMode::Enabled,
behavior: Behavior::default(),
tab_stop: 8,
indent_size: 2,
check_cursor_position: false,
enable_bracketed_paste: true,
enable_signals: false,
}
}
}
/// Beep or flash or nothing
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BellStyle {
/// Beep
Audible,
/// Silent
None,
/// Flash screen (not supported)
Visible,
}
/// `Audible` by default on unix (overridden by current Terminal settings).
/// `None` on windows.
impl Default for BellStyle {
#[cfg(any(windows, target_arch = "wasm32"))]
fn default() -> Self {
Self::None
}
#[cfg(unix)]
fn default() -> Self {
Self::Audible
}
}
/// History filter
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum HistoryDuplicates {
/// No filter
AlwaysAdd,
/// a line will not be added to the history if it matches the previous entry
IgnoreConsecutive,
}
/// Tab completion style
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum CompletionType {
/// Complete the next full match (like in Vim by default)
Circular,
/// Complete till longest match.
/// When more than one match, list all matches
/// (like in Bash/Readline).
List,
/// Complete the match using fuzzy search and selection
/// (like fzf and plugins)
/// Currently only available for unix platforms as dependency on
/// skim->tuikit Compile with `--features=fuzzy` to enable
#[cfg(all(unix, feature = "with-fuzzy"))]
Fuzzy,
}
/// Style of editing / Standard keymaps
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum EditMode {
/// Emacs keymap
Emacs,
/// Vi keymap
Vi,
}
/// Colorization mode
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum ColorMode {
/// Activate highlighting if platform/terminal is supported.
Enabled,
/// Activate highlighting even if platform is not supported (windows < 10).
Forced,
/// Deactivate highlighting even if platform/terminal is supported.
Disabled,
}
/// Should the editor use stdio
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum Behavior {
/// Use stdin / stdout
#[default]
Stdio,
/// Use terminal-style interaction whenever possible, even if 'stdin' and/or
/// 'stdout' are not terminals.
PreferTerm,
// TODO
// Use file-style interaction, reading input from the given file.
// useFile
}
/// Configuration builder
#[derive(Clone, Debug, Default)]
pub struct Builder {
p: Config,
}
impl Builder {
/// Returns a `Config` builder.
#[must_use]
pub fn new() -> Self {
Self {
p: Config::default(),
}
}
/// Set the maximum length for the history.
pub fn max_history_size(mut self, max_size: usize) -> Result<Self> {
self.set_max_history_size(max_size)?;
Ok(self)
}
/// Tell if lines which match the previous history entry are saved or not
/// in the history list.
///
/// By default, they are ignored.
pub fn history_ignore_dups(mut self, yes: bool) -> Result<Self> {
self.set_history_ignore_dups(yes)?;
Ok(self)
}
/// Tell if lines which begin with a space character are saved or not in
/// the history list.
///
/// By default, they are saved.
#[must_use]
pub fn history_ignore_space(mut self, yes: bool) -> Self {
self.set_history_ignore_space(yes);
self
}
/// Set `completion_type`.
#[must_use]
pub fn completion_type(mut self, completion_type: CompletionType) -> Self {
self.set_completion_type(completion_type);
self
}
/// The number of possible completions that determines when the user is
/// asked whether the list of possibilities should be displayed.
#[must_use]
pub fn completion_prompt_limit(mut self, completion_prompt_limit: usize) -> Self {
self.set_completion_prompt_limit(completion_prompt_limit);
self
}
/// Timeout for ambiguous key sequences in milliseconds.
/// Currently, it is used only to distinguish a single ESC from an ESC
/// sequence.
/// After seeing an ESC key, wait at most `keyseq_timeout_ms` for another
/// byte.
#[must_use]
pub fn keyseq_timeout(mut self, keyseq_timeout_ms: Option<u16>) -> Self {
self.set_keyseq_timeout(keyseq_timeout_ms);
self
}
/// Choose between Emacs or Vi mode.
#[must_use]
pub fn edit_mode(mut self, edit_mode: EditMode) -> Self {
self.set_edit_mode(edit_mode);
self
}
/// Tell if lines are automatically added to the history.
///
/// By default, they are not.
#[must_use]
pub fn auto_add_history(mut self, yes: bool) -> Self {
self.set_auto_add_history(yes);
self
}
/// Set bell style: beep, flash or nothing.
#[must_use]
pub fn bell_style(mut self, bell_style: BellStyle) -> Self {
self.set_bell_style(bell_style);
self
}
/// Forces colorization on or off.
///
/// By default, colorization is on except if stdout is not a TTY.
#[must_use]
pub fn color_mode(mut self, color_mode: ColorMode) -> Self {
self.set_color_mode(color_mode);
self
}
/// Whether to use stdio or not
///
/// By default, stdio is used.
#[must_use]
pub fn behavior(mut self, behavior: Behavior) -> Self {
self.set_behavior(behavior);
self
}
/// Horizontal space taken by a tab.
///
/// By default, `8`
#[must_use]
pub fn tab_stop(mut self, tab_stop: usize) -> Self {
self.set_tab_stop(tab_stop);
self
}
/// Check if cursor position is at leftmost before displaying prompt.
///
/// By default, we don't check.
#[must_use]
pub fn check_cursor_position(mut self, yes: bool) -> Self {
self.set_check_cursor_position(yes);
self
}
/// Indentation size
///
/// By default, `2`
#[must_use]
pub fn indent_size(mut self, indent_size: usize) -> Self {
self.set_indent_size(indent_size);
self
}
/// Enable or disable bracketed paste on unix platform
///
/// By default, it's enabled.
#[must_use]
pub fn bracketed_paste(mut self, enabled: bool) -> Self {
self.enable_bracketed_paste(enabled);
self
}
/// Enable or disable signals in termios
///
/// By default, it's disabled.
#[must_use]
pub fn enable_signals(mut self, enable_signals: bool) -> Self {
self.p.set_enable_signals(enable_signals);
self
}
/// Builds a `Config` with the settings specified so far.
#[must_use]
pub fn build(self) -> Config {
self.p
}
}
impl Configurer for Builder {
fn config_mut(&mut self) -> &mut Config {
&mut self.p
}
}
/// Trait for component that holds a `Config`.
pub trait Configurer {
/// `Config` accessor.
fn config_mut(&mut self) -> &mut Config;
/// Set the maximum length for the history.
fn set_max_history_size(&mut self, max_size: usize) -> Result<()> {
self.config_mut().set_max_history_size(max_size);
Ok(())
}
/// Tell if lines which match the previous history entry are saved or not
/// in the history list.
///
/// By default, they are ignored.
fn set_history_ignore_dups(&mut self, yes: bool) -> Result<()> {
self.config_mut().set_history_ignore_dups(yes);
Ok(())
}
/// Tell if lines which begin with a space character are saved or not in
/// the history list.
///
/// By default, they are saved.
fn set_history_ignore_space(&mut self, yes: bool) {
self.config_mut().set_history_ignore_space(yes);
}
/// Set `completion_type`.
fn set_completion_type(&mut self, completion_type: CompletionType) {
self.config_mut().completion_type = completion_type;
}
/// The number of possible completions that determines when the user is
/// asked whether the list of possibilities should be displayed.
fn set_completion_prompt_limit(&mut self, completion_prompt_limit: usize) {
self.config_mut().completion_prompt_limit = completion_prompt_limit;
}
/// Timeout for ambiguous key sequences in milliseconds.
fn set_keyseq_timeout(&mut self, keyseq_timeout_ms: Option<u16>) {
self.config_mut().keyseq_timeout = keyseq_timeout_ms;
}
/// Choose between Emacs or Vi mode.
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(None), // no timeout
EditMode::Vi => self.set_keyseq_timeout(Some(500)),
}
}
/// Tell if lines are automatically added to the history.
///
/// By default, they are not.
fn set_auto_add_history(&mut self, yes: bool) {
self.config_mut().auto_add_history = yes;
}
/// Set bell style: beep, flash or nothing.
fn set_bell_style(&mut self, bell_style: BellStyle) {
self.config_mut().bell_style = bell_style;
}
/// Forces colorization on or off.
///
/// By default, colorization is on except if stdout is not a TTY.
fn set_color_mode(&mut self, color_mode: ColorMode) {
self.config_mut().set_color_mode(color_mode);
}
/// Whether to use stdio or not
///
/// By default, stdio is used.
fn set_behavior(&mut self, behavior: Behavior) {
self.config_mut().set_behavior(behavior);
}
/// Horizontal space taken by a tab.
///
/// By default, `8`
fn set_tab_stop(&mut self, tab_stop: usize) {
self.config_mut().set_tab_stop(tab_stop);
}
/// Check if cursor position is at leftmost before displaying prompt.
///
/// By default, we don't check.
fn set_check_cursor_position(&mut self, yes: bool) {
self.config_mut().check_cursor_position = yes;
}
/// Indentation size for indent/dedent commands
///
/// By default, `2`
fn set_indent_size(&mut self, size: usize) {
self.config_mut().set_indent_size(size);
}
/// Enable or disable bracketed paste on unix platform
///
/// By default, it's enabled.
fn enable_bracketed_paste(&mut self, enabled: bool) {
self.config_mut().enable_bracketed_paste = enabled;
}
/// Enable or disable signals in termios
///
/// By default, it's disabled.
fn set_enable_signals(&mut self, enable_signals: bool) {
self.config_mut().set_enable_signals(enable_signals);
}
}