mirror of
https://github.com/nushell/reedline.git
synced 2024-11-13 08:50:55 +03:00
70118f732c
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>
65 lines
1.8 KiB
Rust
65 lines
1.8 KiB
Rust
// 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‘")
|
||
}
|