Skip to content

Commit

Permalink
Add writer method to Prompter
Browse files Browse the repository at this point in the history
  • Loading branch information
murarth committed May 30, 2018
1 parent 531962e commit 82fdc4a
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 10 deletions.
4 changes: 3 additions & 1 deletion examples/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ struct DemoFunction;
impl<Term: Terminal> Function<Term> for DemoFunction {
fn execute(&self, prompter: &mut Prompter<Term>, _count: i32, _ch: char) -> io::Result<()> {
assert_eq!(prompter.sequence(), DEMO_FN_SEQ);
prompter.insert_str("<demo function executed>")
let mut writer = prompter.writer()?;

writeln!(writer, "demo function executed")
}
}
2 changes: 1 addition & 1 deletion src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl<Term: Terminal> Interface<Term> {
/// allowing output to be written without corrupting the prompt text.
/// The prompt will be redrawn when the `Writer` instance is dropped.
pub fn lock_writer(&self) -> io::Result<Writer<Term>> {
Writer::new(self.lock_write())
Writer::with_mutex(self.lock_write())
}

fn lock_read(&self) -> ReadLock<Term> {
Expand Down
11 changes: 10 additions & 1 deletion src/prompter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use util::{
word_start, word_end, RangeArgument,
};
use variables::VariableIter;
use writer::{display_str, Digit, Display, HistoryIter, PromptType, WriteLock};
use writer::{display_str, Digit, Display, HistoryIter, PromptType, Writer, WriteLock};

/// Timeout, in milliseconds, to wait for input when "blinking"
const BLINK_TIMEOUT_MS: u64 = 500;
Expand All @@ -50,6 +50,15 @@ impl<'a, 'b: 'a, Term: 'b + Terminal> Prompter<'a, 'b, Term> {
Prompter{read, write}
}

/// Returns a `Writer` instance using the currently held write lock.
///
/// This method will erase the prompt, allowing output to be written
/// without corrupting the prompt text. The prompt will be redrawn
/// when the `Writer` instance is dropped.
pub fn writer<'c>(&'c mut self) -> io::Result<Writer<'c, 'b, Term>> {
Writer::with_ref(&mut self.write)
}

/// Resets input state at the start of `read_line`
fn reset_input(&mut self) {
self.read.reset_data();
Expand Down
9 changes: 7 additions & 2 deletions src/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ impl DefaultTerminal {
pub fn stderr() -> io::Result<DefaultTerminal> {
mortal::Terminal::stderr().map(DefaultTerminal)
}

unsafe fn cast_writer<'a>(writer: &'a mut TerminalWriter<Self>)
-> &'a mut TerminalWriteGuard<'a> {
&mut *(writer as *mut _ as *mut TerminalWriteGuard)
}
}

impl Terminal for DefaultTerminal {
Expand Down Expand Up @@ -203,7 +208,7 @@ impl<'a> TerminalReader<DefaultTerminal> for TerminalReadGuard<'a> {
lock: &mut TerminalWriter<DefaultTerminal>,
block_signals: bool, report_signals: SignalSet)
-> io::Result<PrepareState> {
let lock = &mut *(lock as *mut _ as *mut TerminalWriteGuard);
let lock = DefaultTerminal::cast_writer(lock);

self.prepare_with_lock(lock, PrepareConfig{
block_signals,
Expand All @@ -221,7 +226,7 @@ impl<'a> TerminalReader<DefaultTerminal> for TerminalReadGuard<'a> {
unsafe fn restore_with_lock(&mut self,
lock: &mut TerminalWriter<DefaultTerminal>, state: PrepareState)
-> io::Result<()> {
let lock = &mut *(lock as *mut _ as *mut TerminalWriteGuard);
let lock = DefaultTerminal::cast_writer(lock);
self.restore_with_lock(lock, state)
}

Expand Down
43 changes: 38 additions & 5 deletions src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,13 @@ const PROMPT_SEARCH_SUFFIX: usize = 3;
///
/// [`Interface`]: ../interface/struct.Interface.html
/// [`Interface::lock_writer`]: ../interface/struct.Interface.html#method.lock_writer
pub struct Writer<'a, Term: 'a + Terminal> {
write: WriteLock<'a, Term>,
pub struct Writer<'a, 'b: 'a, Term: 'b + Terminal> {
write: WriterImpl<'a, 'b, Term>,
}

enum WriterImpl<'a, 'b: 'a, Term: 'b + Terminal> {
Mutex(WriteLock<'b, Term>),
MutRef(&'a mut WriteLock<'b, Term>),
}

pub(crate) struct Write {
Expand Down Expand Up @@ -925,15 +930,23 @@ impl<'a, Term: Terminal> WriteLock<'a, Term> {

}

impl<'a, Term: Terminal> Writer<'a, Term> {
pub(crate) fn new(mut write: WriteLock<'a, Term>) -> io::Result<Writer<'a, Term>> {
impl<'a, 'b: 'a, Term: 'b + Terminal> Writer<'a, 'b, Term> {
fn new(mut write: WriterImpl<'a, 'b, Term>) -> io::Result<Self> {
if write.is_prompt_drawn {
write.clear_full_prompt()?;
}

Ok(Writer{write})
}

pub(crate) fn with_mutex(write: WriteLock<'b, Term>) -> io::Result<Self> {
Writer::new(WriterImpl::Mutex(write))
}

pub(crate) fn with_ref(write: &'a mut WriteLock<'b, Term>) -> io::Result<Self> {
Writer::new(WriterImpl::MutRef(write))
}

/// Returns an iterator over history entries.
pub fn history(&self) -> HistoryIter {
self.write.history()
Expand Down Expand Up @@ -964,7 +977,7 @@ impl<'a, Term: Terminal> Writer<'a, Term> {
}
}

impl<'a, Term: Terminal> Drop for Writer<'a, Term> {
impl<'a, 'b: 'a, Term: 'b + Terminal> Drop for Writer<'a, 'b, Term> {
fn drop(&mut self) {
if self.write.is_prompt_drawn {
// There's not really anything useful to be done with this error.
Expand Down Expand Up @@ -1127,6 +1140,26 @@ impl PromptType {
}
}

impl<'a, 'b, Term: 'b + Terminal> Deref for WriterImpl<'a, 'b, Term> {
type Target = WriteLock<'b, Term>;

fn deref(&self) -> &WriteLock<'b, Term> {
match *self {
WriterImpl::Mutex(ref m) => m,
WriterImpl::MutRef(ref m) => m,
}
}
}

impl<'a, 'b: 'a, Term: 'b + Terminal> DerefMut for WriterImpl<'a, 'b, Term> {
fn deref_mut(&mut self) -> &mut WriteLock<'b, Term> {
match *self {
WriterImpl::Mutex(ref mut m) => m,
WriterImpl::MutRef(ref mut m) => m,
}
}
}

/// Iterator over `Interface` history entries
pub struct HistoryIter<'a>(vec_deque::Iter<'a, String>);

Expand Down

0 comments on commit 82fdc4a

Please sign in to comment.