nu_command/experimental/
job_flush.rs1use nu_engine::command_prelude::*;
2use nu_protocol::engine::FilterTag;
3
4#[derive(Clone)]
5pub struct JobFlush;
6
7impl Command for JobFlush {
8 fn name(&self) -> &str {
9 "job flush"
10 }
11
12 fn description(&self) -> &str {
13 "Clear this job's mailbox."
14 }
15
16 fn extra_description(&self) -> &str {
17 r#"
18This command removes all messages in the mailbox of the current job.
19If a message is received while this command is executing, it may also be discarded.
20"#
21 }
22
23 fn signature(&self) -> nu_protocol::Signature {
24 Signature::build("job flush")
25 .category(Category::Experimental)
26 .named(
27 "tag",
28 SyntaxShape::Int,
29 "Clear messages with this tag",
30 None,
31 )
32 .input_output_types(vec![(Type::Nothing, Type::Nothing)])
33 .allow_variants_without_examples(true)
34 }
35
36 fn search_terms(&self) -> Vec<&str> {
37 vec![]
38 }
39
40 fn run(
41 &self,
42 engine_state: &EngineState,
43 stack: &mut Stack,
44 call: &Call,
45 _input: PipelineData,
46 ) -> Result<PipelineData, ShellError> {
47 let tag_arg: Option<Spanned<i64>> = call.get_flag(engine_state, stack, "tag")?;
48 if let Some(tag) = tag_arg
49 && tag.item < 0
50 {
51 return Err(ShellError::NeedsPositiveValue { span: tag.span });
52 }
53
54 let tag_arg = tag_arg.map(|it| it.item as FilterTag);
55
56 let mut mailbox = engine_state
57 .current_job
58 .mailbox
59 .lock()
60 .expect("failed to acquire lock");
61
62 if tag_arg.is_some() {
63 while mailbox.try_recv(tag_arg).is_ok() {}
64 } else {
65 mailbox.clear();
66 }
67
68 Ok(Value::nothing(call.head).into_pipeline_data())
69 }
70
71 fn examples(&self) -> Vec<Example<'_>> {
72 vec![Example {
73 example: "job flush",
74 description: "Clear the mailbox of the current job.",
75 result: None,
76 }]
77 }
78}