Skip to content

Commit

Permalink
Implement ExternalPrinterWriter
Browse files Browse the repository at this point in the history
To enable duplex output of log messages

Fixes kkawakam#797
  • Loading branch information
xeruf committed Aug 23, 2024
1 parent e4eca97 commit 465b14d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
25 changes: 22 additions & 3 deletions examples/external_print.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
extern crate env_logger;
extern crate log;

use std::io::Write;
use std::thread;
use std::time::Duration;
use env_logger::Target;
use log::LevelFilter;

use rand::{thread_rng, Rng};

Expand All @@ -8,10 +14,24 @@ use rustyline::{DefaultEditor, ExternalPrinter, Result};
fn main() -> Result<()> {
let mut rl = DefaultEditor::new()?;
let mut printer = rl.create_external_printer()?;

env_logger::builder()
.filter_level(LevelFilter::Info)
.target(Target::Pipe(rl.create_external_writer()?))
.init();
thread::spawn(move || {
loop {
log::info!("Log Message");
thread::sleep(Duration::from_secs(3));
}
});

let mut writer = rl.create_external_writer().unwrap();
thread::spawn(move || {
let mut rng = thread_rng();
let mut i = 0usize;
loop {
writer.write("writing without newline".as_bytes());

Check warning on line 34 in examples/external_print.rs

View workflow job for this annotation

GitHub Actions / Test min versions

unused `Result` that must be used
printer
.print(format!("External message #{i}"))
.expect("External print failure");
Expand All @@ -23,7 +43,6 @@ fn main() -> Result<()> {

loop {
let line = rl.readline("> ")?;
rl.add_history_entry(line.as_str())?;
println!("Line: {line}");
println!("Read Line: {line}");
}
}
}
25 changes: 24 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ mod undo;
pub mod validate;

use std::fmt;
use std::io::{self, BufRead, Write};
use std::io::{self, BufRead, Read, Write};
use std::path::Path;
use std::result;

Expand Down Expand Up @@ -923,6 +923,12 @@ impl<H: Helper, I: History> Editor<H, I> {
self.term.create_external_printer()
}

/// Create a wrapper for the external printer implementing std::io::Write
pub fn create_external_writer(&mut self) -> Result<Box<dyn Write + Send>> {
self.create_external_printer().map(|printer|
Box::new(ExternalPrinterWriter(Box::new(printer))) as Box<dyn Write + Send>)
}

/// Change cursor visibility
pub fn set_cursor_visibility(
&mut self,
Expand All @@ -932,6 +938,23 @@ impl<H: Helper, I: History> Editor<H, I> {
}
}

struct ExternalPrinterWriter<P>(Box<P>);

impl<P> Write for ExternalPrinterWriter<P>
where P: ExternalPrinter + Send {
fn write(&mut self, mut buf: &[u8]) -> io::Result<usize> {
let mut string = String::with_capacity(buf.len());
buf.read_to_string(&mut string).inspect(|_| {
self.0.print(string); //.strip_suffix('\n').map(|s| s.to_string()).unwrap_or(string));

Check warning on line 948 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Test min versions

unused `std::result::Result` that must be used
})
}

fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}


impl<H: Helper, I: History> config::Configurer for Editor<H, I> {
fn config_mut(&mut self) -> &mut Config {
&mut self.config
Expand Down

0 comments on commit 465b14d

Please sign in to comment.