Skip to content

Commit

Permalink
Maintain the write lock while processing input
Browse files Browse the repository at this point in the history
  • Loading branch information
murarth committed Jun 29, 2018
1 parent 2965083 commit 37562b8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/prompter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const BLINK_TIMEOUT_MS: u64 = 500;
/// [`Interface`]: ../interface/struct.Interface.html
/// [`read_line`]: ../interface/struct.Interface.html#method.read_line
pub struct Prompter<'a, 'b: 'a, Term: 'b + Terminal> {
read: &'a mut ReadLock<'b, Term>,
pub(crate) read: &'a mut ReadLock<'b, Term>,
write: WriteLock<'b, Term>,
}

Expand Down
39 changes: 21 additions & 18 deletions src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,28 +240,31 @@ impl<'a, Term: 'a + Terminal> Reader<'a, Term> {
}
}

// If the macro buffer grows in size while input is being processed,
// we end this step and let the caller try again. This is to allow
// reading Ctrl-C to interrupt (perhaps infinite) macro execution.
let mut macro_len = self.lock.data.macro_buffer.len();

while self.lock.is_input_available() {
if let Some(ch) = self.lock.read_char()? {
let mut prompter = self.prompter();

if let Some(r) = prompter.handle_input(ch)? {
prompter.end_read_line()?;
return Ok(Some(r));
// Acquire the write lock and process all available input
{
let mut prompter = self.prompter();

// If the macro buffer grows in size while input is being processed,
// we end this step and let the caller try again. This is to allow
// reading Ctrl-C to interrupt (perhaps infinite) macro execution.
let mut macro_len = prompter.read.data.macro_buffer.len();

while prompter.read.is_input_available() {
if let Some(ch) = prompter.read.read_char()? {
if let Some(r) = prompter.handle_input(ch)? {
prompter.end_read_line()?;
return Ok(Some(r));
}
}
}

let new_macro_len = self.lock.data.macro_buffer.len();
let new_macro_len = prompter.read.data.macro_buffer.len();

if new_macro_len != 0 && new_macro_len >= macro_len {
break;
}
if new_macro_len != 0 && new_macro_len >= macro_len {
break;
}

macro_len = new_macro_len;
macro_len = new_macro_len;
}
}

Ok(None)
Expand Down

0 comments on commit 37562b8

Please sign in to comment.