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: immediately execute command #882

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ let mut line_editor = Reedline::create();
let prompt = DefaultPrompt::default();

loop {
let sig = line_editor.read_line(&prompt);
let sig = line_editor.read_line(&prompt, false);
match sig {
Ok(Signal::Success(buffer)) => {
println!("We processed: {}", buffer);
Expand Down
2 changes: 1 addition & 1 deletion examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn main() -> io::Result<()> {
let prompt = DefaultPrompt::default();

loop {
let sig = line_editor.read_line(&prompt)?;
let sig = line_editor.read_line(&prompt, false)?;
match sig {
Signal::Success(buffer) => {
println!("We processed: {buffer}");
Expand Down
2 changes: 1 addition & 1 deletion examples/completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ fn main() -> io::Result<()> {
let prompt = DefaultPrompt::default();

loop {
let sig = line_editor.read_line(&prompt)?;
let sig = line_editor.read_line(&prompt, false)?;
match sig {
Signal::Success(buffer) => {
println!("We processed: {buffer}");
Expand Down
2 changes: 1 addition & 1 deletion examples/custom_prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn main() -> io::Result<()> {
let prompt = CustomPrompt(Cell::new(0), "Custom Prompt");

loop {
let sig = line_editor.read_line(&prompt)?;
let sig = line_editor.read_line(&prompt, false)?;
match sig {
Signal::Success(buffer) => {
println!("We processed: {buffer}");
Expand Down
2 changes: 1 addition & 1 deletion examples/cwd_aware_hinter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fn main() -> io::Result<()> {
} else {
std::env::set_current_dir(&home_dir).unwrap();
}
let sig = line_editor.read_line(&prompt)?;
let sig = line_editor.read_line(&prompt, false)?;
match sig {
Signal::Success(buffer) => {
println!("We processed: {buffer}");
Expand Down
2 changes: 1 addition & 1 deletion examples/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ fn main() -> reedline::Result<()> {
let prompt = DefaultPrompt::default();

loop {
let sig = line_editor.read_line(&prompt);
let sig = line_editor.read_line(&prompt, false);

match sig {
Ok(Signal::CtrlD) => {
Expand Down
2 changes: 1 addition & 1 deletion examples/external_printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn main() {
let prompt = DefaultPrompt::default();

loop {
if let Ok(sig) = line_editor.read_line(&prompt) {
if let Ok(sig) = line_editor.read_line(&prompt, false) {
match sig {
Signal::Success(buffer) => {
println!("We processed: {buffer}");
Expand Down
2 changes: 1 addition & 1 deletion examples/highlighter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() -> io::Result<()> {
let prompt = DefaultPrompt::default();

loop {
let sig = line_editor.read_line(&prompt)?;
let sig = line_editor.read_line(&prompt, false)?;
match sig {
Signal::Success(buffer) => {
println!("We processed: {buffer}");
Expand Down
2 changes: 1 addition & 1 deletion examples/hinter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() -> io::Result<()> {
let prompt = DefaultPrompt::default();

loop {
let sig = line_editor.read_line(&prompt)?;
let sig = line_editor.read_line(&prompt, false)?;
match sig {
Signal::Success(buffer) => {
println!("We processed: {buffer}");
Expand Down
2 changes: 1 addition & 1 deletion examples/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn main() -> io::Result<()> {
let prompt = DefaultPrompt::default();

loop {
let sig = line_editor.read_line(&prompt)?;
let sig = line_editor.read_line(&prompt, false)?;
match sig {
Signal::Success(buffer) => {
println!("We processed: {buffer}");
Expand Down
2 changes: 1 addition & 1 deletion examples/ide_completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ fn main() -> io::Result<()> {
let prompt = DefaultPrompt::default();

loop {
let sig = line_editor.read_line(&prompt)?;
let sig = line_editor.read_line(&prompt, false)?;
match sig {
Signal::Success(buffer) => {
println!("We processed: {buffer}");
Expand Down
2 changes: 1 addition & 1 deletion examples/transient_prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ fn main() -> io::Result<()> {
let prompt = DefaultPrompt::default();

loop {
let sig = line_editor.read_line(&prompt)?;
let sig = line_editor.read_line(&prompt, false)?;
match sig {
Signal::Success(buffer) => {
println!("We processed: {buffer}");
Expand Down
2 changes: 1 addition & 1 deletion examples/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn main() -> io::Result<()> {
let prompt = DefaultPrompt::default();

loop {
let sig = line_editor.read_line(&prompt)?;
let sig = line_editor.read_line(&prompt, false)?;
match sig {
Signal::Success(buffer) => {
println!("We processed: {buffer}");
Expand Down
10 changes: 5 additions & 5 deletions src/core_editor/line_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ impl LineBuffer {
pub fn grapheme_left_index(&self) -> usize {
self.lines[..self.insertion_point]
.grapheme_indices(true)
.last()
.next_back()
.map(|(i, _)| i)
.unwrap_or(0)
}
Expand Down Expand Up @@ -213,7 +213,7 @@ impl LineBuffer {
.unwrap_or_else(|| {
self.lines
.grapheme_indices(true)
.last()
.next_back()
.map(|x| x.0)
.unwrap_or(0)
})
Expand All @@ -238,7 +238,7 @@ impl LineBuffer {
.unwrap_or_else(|| {
self.lines
.grapheme_indices(true)
.last()
.next_back()
.map(|x| x.0)
.unwrap_or(0)
})
Expand Down Expand Up @@ -272,7 +272,7 @@ impl LineBuffer {
self.lines[..self.insertion_point]
.split_word_bound_indices()
.filter(|(_, word)| !is_whitespace_str(word))
.last()
.next_back()
.map(|(i, _)| i)
.unwrap_or(0)
}
Expand Down Expand Up @@ -471,7 +471,7 @@ impl LineBuffer {
let left_index = self.lines[..right_index]
.split_word_bound_indices()
.filter(|(_, word)| !is_whitespace_str(word))
.last()
.next_back()
.map(|(i, _)| i)
.unwrap_or(0);

Expand Down
57 changes: 33 additions & 24 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ enum InputMode {
/// let mut line_editor = Reedline::create();
/// let prompt = DefaultPrompt::default();
///
/// let out = line_editor.read_line(&prompt).unwrap();
/// let out = line_editor.read_line(&prompt, false).unwrap();
/// match out {
/// Signal::Success(content) => {
/// // process content
Expand Down Expand Up @@ -642,12 +642,12 @@ impl Reedline {
///
/// Returns a [`std::io::Result`] in which the `Err` type is [`std::io::Result`]
/// and the `Ok` variant wraps a [`Signal`] which handles user inputs.
pub fn read_line(&mut self, prompt: &dyn Prompt) -> Result<Signal> {
pub fn read_line(&mut self, prompt: &dyn Prompt, immediately_execute: bool) -> Result<Signal> {
terminal::enable_raw_mode()?;
self.bracketed_paste.enter();
self.kitty_protocol.enter();

let result = self.read_line_helper(prompt);
let result = self.read_line_helper(prompt, immediately_execute);

self.bracketed_paste.exit();
self.kitty_protocol.exit();
Expand Down Expand Up @@ -687,7 +687,11 @@ impl Reedline {

/// Helper implementing the logic for [`Reedline::read_line()`] to be wrapped
/// in a `raw_mode` context.
fn read_line_helper(&mut self, prompt: &dyn Prompt) -> Result<Signal> {
fn read_line_helper(
&mut self,
prompt: &dyn Prompt,
immediately_execute: bool,
) -> Result<Signal> {
self.painter
.initialize_prompt_position(self.suspended_state.as_ref())?;
if self.suspended_state.is_some() {
Expand Down Expand Up @@ -734,30 +738,32 @@ impl Reedline {

let mut events: Vec<Event> = vec![];

// If the `external_printer` feature is enabled, we need to
// periodically yield so that external printers get a chance to
// print. Otherwise, we can just block until we receive an event.
#[cfg(feature = "external_printer")]
if event::poll(EXTERNAL_PRINTER_WAIT)? {
if !immediately_execute {
// If the `external_printer` feature is enabled, we need to
// periodically yield so that external printers get a chance to
// print. Otherwise, we can just block until we receive an event.
#[cfg(feature = "external_printer")]
if event::poll(EXTERNAL_PRINTER_WAIT)? {
events.push(crossterm::event::read()?);
}
#[cfg(not(feature = "external_printer"))]
events.push(crossterm::event::read()?);
}
#[cfg(not(feature = "external_printer"))]
events.push(crossterm::event::read()?);

// Receive all events in the queue without blocking. Will stop when
// a line of input is completed.
while !completed(&events) && event::poll(Duration::from_millis(0))? {
events.push(crossterm::event::read()?);
}

// If we believe there's text pasting or resizing going on, batch
// more events at the cost of a slight delay.
if events.len() > EVENTS_THRESHOLD
|| events.iter().any(|e| matches!(e, Event::Resize(_, _)))
{
while !completed(&events) && event::poll(POLL_WAIT)? {
// a line of input is completed.
// Receive all events in the queue without blocking. Will stop when
while !completed(&events) && event::poll(Duration::from_millis(0))? {
events.push(crossterm::event::read()?);
}

// If we believe there's text pasting or resizing going on, batch
// more events at the cost of a slight delay.
if events.len() > EVENTS_THRESHOLD
|| events.iter().any(|e| matches!(e, Event::Resize(_, _)))
{
while !completed(&events) && event::poll(POLL_WAIT)? {
events.push(crossterm::event::read()?);
}
}
Comment on lines -737 to +766
Copy link

Choose a reason for hiding this comment

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

could you explain the purpose of these changes? it seems like only the if immediately_execute below all this should be necessary to accomplish the change, but not sure what you had in mind.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the review! Now that I see how github displays the change, I am confused as well. All of this is indentation, except for the if !immediately_execute. I think that github confuses the two instances of events.push(crossterm::event::read()?);.

Copy link
Author

Choose a reason for hiding this comment

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

The point is that I don't want reedline to read from stdin when it is in immediately_accept mode. So I skip all these conditional/looped input reads and further down add the Submit to the queue.

}

// Convert `Event` into `ReedlineEvent`. Also, fuse consecutive
Expand Down Expand Up @@ -787,6 +793,9 @@ impl Reedline {
if let Some((x, y)) = resize {
reedline_events.push(ReedlineEvent::Resize(x, y));
}
if immediately_execute {
reedline_events.push(ReedlineEvent::Submit);
}

// Handle reedline events.
for event in reedline_events {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
//! let prompt = DefaultPrompt::default();
//!
//! loop {
//! let sig = line_editor.read_line(&prompt);
//! let sig = line_editor.read_line(&prompt, false);
//! match sig {
//! Ok(Signal::Success(buffer)) => {
//! println!("We processed: {}", buffer);
Expand Down