Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modularization of the main struct #134

Merged
merged 21 commits into from
Aug 28, 2021
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
472 changes: 245 additions & 227 deletions src/engine.rs

Large diffs are not rendered by default.

25 changes: 13 additions & 12 deletions src/enums.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};

/// Valid ways how [`crate::Reedline::read_line()`] can return
/// Valid ways how [`Reedline::read_line()`] can return
pub enum Signal {
/// Entry succeeded with the provided content
Success(String),
Expand Down Expand Up @@ -101,12 +101,6 @@ pub enum EditCommand {
/// Swap the current grapheme/character with the one to the right
SwapGraphemes,

/// Enter the normal vi mode
EnterViNormal,

/// Enter the insertion vi mode
EnterViInsert,

/// Send a code fragment to the vi handler
ViCommandFragment(char),

Expand All @@ -122,10 +116,17 @@ pub enum EditCommand {
pub enum EditMode {
/// Emacs mode, the default
Emacs,
}

/// Vi view/normal mode
ViNormal,

/// Vi insertion mode
ViInsert,
pub enum ReedlineEvent {
HandleTab,
CtrlD, // Don't know a better name for this
CtrlC, // Don't know a better name for this
ClearScreen,
Enter,
Mouse, // Fill in details later
Resize(u16, u16),
EditInsert(EditCommand), // HACK: Special handling for insert
Edit(Vec<EditCommand>),
Repaint,
}
11 changes: 11 additions & 0 deletions src/input_parsing/base.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use crossterm::event::Event;

use crate::{enums::ReedlineEvent, EditMode};

use super::keybindings::Keybindings;

pub trait InputParser {
fn parse_event(&self, event: Event) -> ReedlineEvent;
fn update_keybindings(&mut self, keybindings: Keybindings);
fn edit_mode(&self) -> EditMode;
}
57 changes: 57 additions & 0 deletions src/input_parsing/emacs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};

use crate::{
default_emacs_keybindings,
enums::{EditCommand, EditMode, ReedlineEvent},
};

use super::{keybindings::Keybindings, InputParser};

pub struct EmacsInputParser {
keybindings: Keybindings,
}

impl Default for EmacsInputParser {
fn default() -> Self {
EmacsInputParser {
keybindings: default_emacs_keybindings(),
}
}
}

impl InputParser for EmacsInputParser {
fn parse_event(&self, event: Event) -> ReedlineEvent {
match event {
Event::Key(KeyEvent { code, modifiers }) => match (modifiers, code) {
(KeyModifiers::NONE, KeyCode::Tab) => ReedlineEvent::HandleTab,
(KeyModifiers::CONTROL, KeyCode::Char('d')) => ReedlineEvent::CtrlD,
(KeyModifiers::CONTROL, KeyCode::Char('c')) => ReedlineEvent::CtrlC,
(KeyModifiers::CONTROL, KeyCode::Char('l')) => ReedlineEvent::ClearScreen,
(KeyModifiers::NONE, KeyCode::Char(c))
| (KeyModifiers::SHIFT, KeyCode::Char(c)) => {
ReedlineEvent::EditInsert(EditCommand::InsertChar(c))
}
(KeyModifiers::NONE, KeyCode::Enter) => ReedlineEvent::Enter,
_ => {
if let Some(binding) = self.keybindings.find_binding(modifiers, code) {
ReedlineEvent::Edit(binding)
} else {
ReedlineEvent::Edit(vec![])
}
}
},

Event::Mouse(_) => ReedlineEvent::Mouse,
Event::Resize(width, height) => ReedlineEvent::Resize(width, height),
}
}

// HACK: This about this interface more
fn update_keybindings(&mut self, keybindings: Keybindings) {
self.keybindings = keybindings;
}

fn edit_mode(&self) -> EditMode {
EditMode::Emacs
}
}
45 changes: 0 additions & 45 deletions src/keybindings.rs → src/input_parsing/keybindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,51 +55,6 @@ impl Keybindings {
}
}

pub fn default_vi_normal_keybindings() -> Keybindings {
use KeyCode::*;

let mut keybindings = Keybindings::new();

keybindings.add_binding(
KeyModifiers::NONE,
Up,
vec![EditCommand::ViCommandFragment('k')],
);
keybindings.add_binding(
KeyModifiers::NONE,
Down,
vec![EditCommand::ViCommandFragment('j')],
);
keybindings.add_binding(
KeyModifiers::NONE,
Left,
vec![EditCommand::ViCommandFragment('h')],
);
keybindings.add_binding(
KeyModifiers::NONE,
Right,
vec![EditCommand::ViCommandFragment('l')],
);

keybindings
}

pub fn default_vi_insert_keybindings() -> Keybindings {
use KeyCode::*;

let mut keybindings = Keybindings::new();

keybindings.add_binding(KeyModifiers::NONE, Esc, vec![EditCommand::EnterViNormal]);
keybindings.add_binding(KeyModifiers::NONE, Up, vec![EditCommand::PreviousHistory]);
keybindings.add_binding(KeyModifiers::NONE, Down, vec![EditCommand::NextHistory]);
keybindings.add_binding(KeyModifiers::NONE, Left, vec![EditCommand::MoveLeft]);
keybindings.add_binding(KeyModifiers::NONE, Right, vec![EditCommand::MoveRight]);
keybindings.add_binding(KeyModifiers::NONE, Backspace, vec![EditCommand::Backspace]);
keybindings.add_binding(KeyModifiers::NONE, Delete, vec![EditCommand::Delete]);

keybindings
}

/// Returns the current default emacs keybindings
pub fn default_emacs_keybindings() -> Keybindings {
use KeyCode::*;
Expand Down
8 changes: 8 additions & 0 deletions src/input_parsing/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
mod base;
mod emacs;
mod keybindings;
mod vi;

pub use base::InputParser;
pub use emacs::EmacsInputParser;
pub use keybindings::{default_emacs_keybindings, Keybindings};
2 changes: 1 addition & 1 deletion src/vi_engine.rs → src/input_parsing/vi/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl ViEngine {
output.push(EditCommand::NextHistory);
}
'i' => {
output.push(EditCommand::EnterViInsert);
// output.push(EditCommand::EnterViInsert);
}
_ => {}
},
Expand Down
2 changes: 2 additions & 0 deletions src/input_parsing/vi/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod engine;
mod parser;
File renamed without changes.
45 changes: 21 additions & 24 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
//! // Create a default reedline object to handle user input
//!
//! use reedline::{DefaultPrompt, Reedline, Signal};
//! use std::io;
//!
//! let mut line_editor = Reedline::new();
//! let mut line_editor = Reedline::new()?;
//! let prompt = DefaultPrompt::default();
//!
//! loop {
Expand All @@ -26,6 +27,7 @@
//! }
//! }
//! }
//! # Ok::<(), io::Error>(())
//! ```
//! ## Integrate with custom Keybindings
//!
Expand All @@ -36,6 +38,7 @@
//! // [dependencies]
//! // crossterm = "*"
//!
//! use std::io;
//! use {
//! crossterm::event::{KeyCode, KeyModifiers},
//! reedline::{default_emacs_keybindings, EditCommand, Reedline},
Expand All @@ -48,30 +51,34 @@
//! vec![EditCommand::BackspaceWord],
//! );
//!
//! let mut line_editor = Reedline::new().with_keybindings(keybindings);
//! let mut line_editor = Reedline::new()?.with_keybindings(keybindings);
//! # Ok::<(), io::Error>(())
//! ```
//!
//! ## Integrate with custom History
//!
//! ```rust,no_run
//! // Create a reedline object with history support, including history size limits
//!
//! use std::io;
//! use reedline::{FileBackedHistory, Reedline};
//!
//! let history = Box::new(
//! FileBackedHistory::with_file(5, "history.txt".into())
//! .expect("Error configuring history with file"),
//! );
//! let mut line_editor = Reedline::new()
//! let mut line_editor = Reedline::new()?
//! .with_history(history)
//! .expect("Error configuring reedline with history");
//! # Ok::<(), io::Error>(())
//! ```
//!
//! ## Integrate with custom Highlighter
//!
//! ```rust
//! // Create a reedline object with highlighter support
//!
//! use std::io;
//! use reedline::{DefaultHighlighter, Reedline};
//!
//! let commands = vec![
Expand All @@ -81,14 +88,16 @@
//! "this is the reedline crate".into(),
//! ];
//! let mut line_editor =
//! Reedline::new().with_highlighter(Box::new(DefaultHighlighter::new(commands)));
//! Reedline::new()?.with_highlighter(Box::new(DefaultHighlighter::new(commands)));
//! # Ok::<(), io::Error>(())
//! ```
//!
//! ## Integrate with custom Tab-Handler
//!
//! ```rust
//! // Create a reedline object with tab completions support
//!
//! use std::io;
//! use reedline::{DefaultCompleter, DefaultCompletionActionHandler, Reedline};
//!
//! let commands = vec![
Expand All @@ -99,9 +108,10 @@
//! ];
//! let completer = Box::new(DefaultCompleter::new_with_wordlen(commands.clone(), 2));
//!
//! let mut line_editor = Reedline::new().with_completion_action_handler(Box::new(
//! let mut line_editor = Reedline::new()?.with_completion_action_handler(Box::new(
//! DefaultCompletionActionHandler::default().with_completer(completer),
//! ));
//! # Ok::<(), io::Error>(())
//! ```
//!
//! ## Integrate with custom Hinter
Expand All @@ -113,6 +123,7 @@
//! // [dependencies]
//! // nu-ansi-term = "*"
//!
//! use std::io;
//! use {
//! nu_ansi_term::{Color, Style},
//! reedline::{DefaultCompleter, DefaultHinter, Reedline},
Expand All @@ -126,24 +137,13 @@
//! ];
//! let completer = Box::new(DefaultCompleter::new_with_wordlen(commands.clone(), 2));
//!
//! let mut line_editor = Reedline::new().with_hinter(Box::new(
//! let mut line_editor = Reedline::new()?.with_hinter(Box::new(
//! DefaultHinter::default()
//! .with_completer(completer) // or .with_history()
//! // .with_inside_line()
//! .with_style(Style::new().italic().fg(Color::LightGray)),
//! ));
//! ```
//!
//! ## Integrate with custom Edit Mode
//!
//! ```rust
//! // Create a reedline object with custom edit mode
//!
//! use reedline::{EditMode, Reedline};
//!
//! let mut line_editor = Reedline::new().with_edit_mode(
//! EditMode::ViNormal, // or EditMode::Emacs or EditMode::ViInsert
//! );
//! # Ok::<(), io::Error>(())
//! ```
//!
//! ## Are we prompt yet? (Development status)
Expand Down Expand Up @@ -175,7 +175,7 @@
#![warn(rustdoc::missing_crate_level_docs)]
#![warn(rustdoc::missing_doc_code_examples)]
#![warn(missing_docs)]
#![deny(warnings)]
// #![deny(warnings)]
mod core_editor;

mod text_manipulation;
Expand All @@ -197,14 +197,11 @@ pub use prompt::{
PromptViMode, DEFAULT_PROMPT_COLOR, DEFAULT_PROMPT_INDICATOR,
};

mod keybindings;
pub use keybindings::default_emacs_keybindings;
mod input_parsing;
pub use input_parsing::default_emacs_keybindings;

mod syntax_highlighting_fileio;

mod vi_engine;
pub use vi_engine::ViEngine;

mod highlighter;
pub use highlighter::{DefaultHighlighter, Highlighter};

Expand Down
9 changes: 2 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use {
};

fn main() -> Result<()> {
let vi_mode = matches!(std::env::args().nth(1), Some(x) if x == "--vi");
// quick command like parameter handling
let args: Vec<String> = std::env::args().collect();
// if -k is passed, show the events
Expand Down Expand Up @@ -48,13 +47,9 @@ fn main() -> Result<()> {

let completer = Box::new(DefaultCompleter::new_with_wordlen(commands.clone(), 2));

let mut line_editor = Reedline::new()
let mut line_editor = Reedline::new()?
.with_history(history)?
.with_edit_mode(if vi_mode {
reedline::EditMode::ViNormal
} else {
reedline::EditMode::Emacs
})
.with_edit_mode(reedline::EditMode::Emacs)
.with_keybindings(keybindings)
.with_highlighter(Box::new(DefaultHighlighter::new(commands)))
.with_completion_action_handler(Box::new(
Expand Down