add example ctrlc
This commit is contained in:
parent
621241c731
commit
6c23183148
52
crates/nu_plugin_example/src/commands/ctrlc.rs
Normal file
52
crates/nu_plugin_example/src/commands/ctrlc.rs
Normal file
|
@ -0,0 +1,52 @@
|
|||
use std::sync::{Arc, Barrier};
|
||||
|
||||
use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand};
|
||||
use nu_protocol::{Category, LabeledError, PipelineData, Signature};
|
||||
|
||||
use crate::ExamplePlugin;
|
||||
|
||||
/// `example ctrlc`
|
||||
pub struct Ctrlc;
|
||||
|
||||
impl PluginCommand for Ctrlc {
|
||||
type Plugin = ExamplePlugin;
|
||||
|
||||
fn name(&self) -> &str {
|
||||
"example ctrlc"
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Example command that demonstrates registering a ctrl-c handler"
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build(self.name()).category(Category::Experimental)
|
||||
}
|
||||
|
||||
fn search_terms(&self) -> Vec<&str> {
|
||||
vec!["example"]
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
_plugin: &ExamplePlugin,
|
||||
engine: &EngineInterface,
|
||||
_call: &EvaluatedCall,
|
||||
_input: PipelineData,
|
||||
) -> Result<PipelineData, LabeledError> {
|
||||
let barrier = Arc::new(Barrier::new(2));
|
||||
|
||||
let _guard = {
|
||||
let barrier = barrier.clone();
|
||||
engine.register_ctrlc_handler(Box::new(move || {
|
||||
barrier.wait();
|
||||
}))
|
||||
};
|
||||
|
||||
eprintln!("waiting for ctrl-c signal...");
|
||||
barrier.wait();
|
||||
eprintln!("peace.");
|
||||
|
||||
Ok(PipelineData::Empty)
|
||||
}
|
||||
}
|
|
@ -17,11 +17,13 @@ mod config;
|
|||
mod disable_gc;
|
||||
mod env;
|
||||
mod view_span;
|
||||
mod ctrlc;
|
||||
|
||||
pub use config::Config;
|
||||
pub use disable_gc::DisableGc;
|
||||
pub use env::Env;
|
||||
pub use view_span::ViewSpan;
|
||||
pub use ctrlc::Ctrlc;
|
||||
|
||||
// Stream demos
|
||||
mod collect_bytes;
|
||||
|
|
|
@ -23,6 +23,7 @@ impl Plugin for ExamplePlugin {
|
|||
Box::new(Env),
|
||||
Box::new(ViewSpan),
|
||||
Box::new(DisableGc),
|
||||
Box::new(Ctrlc),
|
||||
// Stream demos
|
||||
Box::new(CollectBytes),
|
||||
Box::new(Echo),
|
||||
|
|
Loading…
Reference in New Issue
Block a user