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

feat: support external event queue #822

Draft
wants to merge 19 commits into
base: main
Choose a base branch
from
Draft
Changes from all 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
31 changes: 30 additions & 1 deletion src/engine.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::path::PathBuf;
use std::{
path::PathBuf,
sync::mpsc::{channel, Receiver, Sender},
};

use itertools::Itertools;
use nu_ansi_term::{Color, Style};
Expand Down Expand Up @@ -163,6 +166,10 @@ pub struct Reedline {

#[cfg(feature = "external_printer")]
external_printer: Option<ExternalPrinter<String>>,

/// A ReedlineEvent Sender, use this to submit events to the queue
pub reedline_event_sender: Sender<ReedlineEvent>,
reedline_event_receiver: Receiver<ReedlineEvent>,
}

struct BufferEditor {
Expand Down Expand Up @@ -202,6 +209,8 @@ impl Reedline {
let edit_mode = Box::<Emacs>::default();
let hist_session_id = None;

let (reedline_event_sender, reedline_event_receiver) = channel();

Reedline {
editor: Editor::default(),
history,
Expand Down Expand Up @@ -236,6 +245,8 @@ impl Reedline {
kitty_protocol: KittyProtocolGuard::default(),
#[cfg(feature = "external_printer")]
external_printer: None,
reedline_event_receiver,
reedline_event_sender,
}
}

Expand Down Expand Up @@ -718,6 +729,13 @@ impl Reedline {

let mut latest_resize = None;
loop {
while let Ok(reedline_event) = self.reedline_event_receiver.try_recv() {
reedline_events.push(reedline_event);
}
if !reedline_events.is_empty() {
break;
}

match event::read()? {
Event::Resize(x, y) => {
latest_resize = Some((x, y));
Expand Down Expand Up @@ -763,6 +781,7 @@ impl Reedline {
//
// (Text should only be `EditCommand::InsertChar`s)
let mut last_edit_commands = None;

for event in crossterm_events.drain(..) {
match (&mut last_edit_commands, self.edit_mode.parse_event(event)) {
(None, ReedlineEvent::Edit(ec)) => {
Expand All @@ -785,6 +804,10 @@ impl Reedline {
reedline_events.push(ReedlineEvent::Edit(ec));
}

while let Ok(reedline_event) = self.reedline_event_receiver.try_recv() {
reedline_events.push(reedline_event);
}

for event in reedline_events.drain(..) {
match self.handle_event(prompt, event)? {
EventStatus::Exits(signal) => {
Expand Down Expand Up @@ -1639,6 +1662,12 @@ impl Reedline {
.map(|token| (parsed.remainder.len(), indicator.len(), token.to_string())),
});

// if self.immediately_accept_bashisms {
// if let Err(_e) = self.reedline_event_sender.send(ReedlineEvent::Submit) {
Copy link
Collaborator

@fdncred fdncred Sep 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be good to remove these comments too

// // handle or log _e
// }
// }

if let Some((start, size, history)) = history_result {
let edits = vec![
EditCommand::MoveToPosition {
Expand Down
Loading