reedline/examples/external_printer.rs
Gregor 70118f732c
Basic external printer (#467)
Addresses #320, #236

* Adding External printer

* Made ExternalPrinter as an optional feature. Clippy is happy, test pass, docs added.

* ExternalPrinter: prints multiple messages if available, more on error-handling.

* Bug(s) fixed. Prints messages. Working example in examples folder. Code formatted, clippyed, tests pass.

* Generic ExternalPrinter<T> where T: Display.

* Fixed: Works with buffers larger than a line.

* Fixed: Works with buffers larger than a line, refactored.

* Different approach, seems to look like what is expected. Gives the "illusion" of one line being entered. Needs more testing, could have some off by one errors ;)

Co-authored-by: Gregor Engberding <gregor@meinkopter.de>
2022-09-14 21:58:45 +02:00

65 lines
1.8 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Create a default reedline object to handle user input
// to run:
// cargo run --example external_printer --features=external_printer
#[cfg(feature = "external_printer")]
use {
reedline::ExternalPrinter,
reedline::{DefaultPrompt, Reedline, Signal},
std::thread,
std::thread::sleep,
std::time::Duration,
};
#[cfg(feature = "external_printer")]
fn main() {
let printer = ExternalPrinter::default();
// make a clone to use it in a different thread
let p_clone = printer.clone();
// get the Sender<String> to have full sending control
let p_sender = printer.sender();
// external printer that prints a message every second
thread::spawn(move || {
let mut i = 1;
loop {
sleep(Duration::from_secs(1));
assert!(p_clone.print(format!("Message {} delivered.", i)).is_ok());
i += 1;
}
});
// external printer that prints a bunch of messages after 3 seconds
thread::spawn(move || {
sleep(Duration::from_secs(3));
for _ in 0..10 {
sleep(Duration::from_millis(1));
assert!(p_sender.send(format!("Fast Hello !")).is_ok());
}
});
let mut line_editor = Reedline::create().with_external_printer(printer);
let prompt = DefaultPrompt::default();
loop {
if let Ok(sig) = line_editor.read_line(&prompt) {
match sig {
Signal::Success(buffer) => {
println!("We processed: {}", buffer);
}
Signal::CtrlD | Signal::CtrlC => {
println!("\nAborted!");
break;
}
}
continue;
}
break;
}
}
#[cfg(not(feature = "external_printer"))]
fn main() {
println!("Please enable the feature: external_printer")
}