-
Notifications
You must be signed in to change notification settings - Fork 162
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
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
9dc4baf
Remove vagueness from position method
nixypanda dbb1fd1
Pull terminal size into the main struct
nixypanda 780973d
WIP: prompt widget
nixypanda 622ddd0
Remove a bunch of stuff
nixypanda 360a6c6
Remove edit mode from event matcher
nixypanda cebe859
Extract out event handling into a function
nixypanda c05f98e
Add reedline specific enum
nixypanda 16a323c
Rebase fixes
nixypanda 8a7739d
Update docs
nixypanda ff9dd54
Re-add clock functionality
nixypanda 5aca7c8
Add event-parser
nixypanda 44ea63c
Pull out Input Parsing out of the main struct
nixypanda 4807bee
Move input parsing stuff into a directory
nixypanda 762c475
Move vi stuff into input parsing
nixypanda 371d7d2
Re-introduce Vi-Mode
nixypanda 9b003b7
Remove EditMode enum
nixypanda 9296047
Emacs mode uses non-default keybindings
nixypanda 07e4a54
Basic hygine for edit_mode
nixypanda 86a86bc
Remove update_keybindings interface
nixypanda 98daa0b
Vi sends Repaint events on mode switch
nixypanda 3bf185c
Rename new to create
nixypanda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,23 +65,25 @@ impl PromptWidget { | |
} | ||
} | ||
|
||
trait EventParser { | ||
trait InputParser { | ||
fn parse_event(&self, event: Event) -> ReedlineEvent; | ||
fn update_keybindings(&mut self, keybindings: Keybindings); | ||
fn edit_mode(&self) -> EditMode; | ||
} | ||
|
||
struct EmacsEventParser { | ||
struct EmacsInputParser { | ||
keybindings: Keybindings, | ||
} | ||
|
||
impl Default for EmacsEventParser { | ||
impl Default for EmacsInputParser { | ||
fn default() -> Self { | ||
EmacsEventParser { | ||
EmacsInputParser { | ||
keybindings: default_emacs_keybindings(), | ||
} | ||
} | ||
} | ||
|
||
impl EventParser for EmacsEventParser { | ||
impl InputParser for EmacsInputParser { | ||
fn parse_event(&self, event: Event) -> ReedlineEvent { | ||
match event { | ||
Event::Key(KeyEvent { code, modifiers }) => match (modifiers, code) { | ||
|
@@ -107,6 +109,15 @@ impl EventParser for EmacsEventParser { | |
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 | ||
} | ||
} | ||
|
||
/// Line editor engine | ||
|
@@ -139,11 +150,7 @@ pub struct Reedline { | |
// Stdout | ||
painter: Painter, | ||
|
||
// Keybindings | ||
keybindings: HashMap<EditMode, Keybindings>, | ||
|
||
// Edit mode | ||
edit_mode: EditMode, | ||
input_parser: Box<dyn InputParser>, | ||
|
||
tab_handler: Box<dyn ComplationActionHandler>, | ||
|
||
|
@@ -165,13 +172,14 @@ impl Reedline { | |
// Note: this is started with a garbage value | ||
let prompt_widget = Default::default(); | ||
|
||
let input_parser = Box::new(EmacsInputParser::default()); | ||
|
||
let reedline = Reedline { | ||
editor: Editor::default(), | ||
history, | ||
input_mode: InputMode::Regular, | ||
painter, | ||
keybindings: keybindings_hashmap, | ||
edit_mode: EditMode::Emacs, | ||
input_parser, | ||
tab_handler: Box::new(DefaultCompletionActionHandler::default()), | ||
terminal_size, | ||
prompt_widget, | ||
|
@@ -290,36 +298,22 @@ impl Reedline { | |
|
||
/// A builder which configures the keybindings for your instance of the Reedline engine | ||
pub fn with_keybindings(mut self, keybindings: Keybindings) -> Reedline { | ||
self.keybindings.insert(EditMode::Emacs, keybindings); | ||
self.input_parser.update_keybindings(keybindings); | ||
|
||
self | ||
} | ||
|
||
/// A builder which configures the edit mode for your instance of the Reedline engine | ||
pub fn with_edit_mode(mut self, edit_mode: EditMode) -> Reedline { | ||
self.edit_mode = edit_mode; | ||
match edit_mode { | ||
EditMode::Emacs => { | ||
self.input_parser = Box::new(EmacsInputParser::default()); | ||
} | ||
}; | ||
|
||
self | ||
} | ||
|
||
/// Gets the current keybindings for Emacs mode | ||
pub fn get_keybindings(&self) -> &Keybindings { | ||
self.keybindings | ||
.get(&EditMode::Emacs) | ||
.expect("Internal error: emacs should always be supported") | ||
} | ||
|
||
/// Sets the keybindings to the given keybindings | ||
/// Note: keybindings are set on the emacs mode. The vi mode is not configurable | ||
pub fn update_keybindings(&mut self, keybindings: Keybindings) { | ||
self.keybindings.insert(EditMode::Emacs, keybindings); | ||
} | ||
|
||
/// Get the current edit mode | ||
pub fn edit_mode(&self) -> EditMode { | ||
self.edit_mode | ||
} | ||
|
||
fn terminal_columns(&self) -> u16 { | ||
self.terminal_size.0 | ||
} | ||
|
@@ -330,22 +324,11 @@ impl Reedline { | |
|
||
/// Returns the corresponding expected prompt style for the given edit mode | ||
pub fn prompt_edit_mode(&self) -> PromptEditMode { | ||
match self.edit_mode { | ||
match self.input_parser.edit_mode() { | ||
EditMode::Emacs => PromptEditMode::Emacs, | ||
} | ||
} | ||
|
||
fn find_keybinding( | ||
&self, | ||
modifier: KeyModifiers, | ||
key_code: KeyCode, | ||
) -> Option<Vec<EditCommand>> { | ||
self.keybindings | ||
.get(&self.edit_mode) | ||
.expect("Internal error: expected to find keybindings for edit mode") | ||
.find_binding(modifier, key_code) | ||
} | ||
|
||
/// Output the complete [`History`] chronologically with numbering to the terminal | ||
pub fn print_history(&mut self) -> Result<()> { | ||
let history: Vec<_> = self | ||
|
@@ -883,31 +866,7 @@ impl Reedline { | |
|
||
fn event_gen(&self) -> io::Result<ReedlineEvent> { | ||
let event = read()?; | ||
let editor_event = 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.find_keybinding(modifiers, code) { | ||
ReedlineEvent::Edit(binding) | ||
} else { | ||
ReedlineEvent::Edit(vec![]) | ||
} | ||
} | ||
}, | ||
|
||
Event::Mouse(_) => ReedlineEvent::Mouse, | ||
Event::Resize(width, height) => ReedlineEvent::Resize(width, height), | ||
}; | ||
|
||
Ok(editor_event) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And finally, we achieve complete segregation of even parsing into a separate struct. |
||
Ok(self.input_parser.parse_event(event)) | ||
} | ||
|
||
/// Helper implemting the logic for [`Reedline::read_line()`] to be wrapped | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a makeshift interface for now. It should remain the same in spirit only (i.e. it parses
Event
s toReedlineEvent
s) but the interface will definitely need updating.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: Now the user of this lib can develop their own InputParser's without any help from the lib. All they need to do is adhere to this interface and we will be done. (prompt handling might pose a caveat in this though)