Add `command_prelude` module (#12291)
# Description
When implementing a `Command`, one must also import all the types
present in the function signatures for `Command`. This makes it so that
we often import the same set of types in each command implementation
file. E.g., something like this:
```rust
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
record, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData,
ShellError, Signature, Span, Type, Value,
};
```
This PR adds the `nu_engine::command_prelude` module which contains the
necessary and commonly used types to implement a `Command`:
```rust
// command_prelude.rs
pub use crate::CallExt;
pub use nu_protocol::{
ast::{Call, CellPath},
engine::{Command, EngineState, Stack},
record, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, IntoSpanned,
PipelineData, Record, ShellError, Signature, Span, Spanned, SyntaxShape, Type, Value,
};
```
This should reduce the boilerplate needed to implement a command and
also gives us a place to track the breadth of the `Command` API. I tried
to be conservative with what went into the prelude modules, since it
might be hard/annoying to remove items from the prelude in the future.
Let me know if something should be included or excluded.
2024-03-26 21:17:30 +00:00
|
|
|
use nu_engine::command_prelude::*;
|
|
|
|
|
2021-11-28 21:15:32 +00:00
|
|
|
use std::{
|
|
|
|
thread,
|
|
|
|
time::{Duration, Instant},
|
|
|
|
};
|
|
|
|
|
|
|
|
const CTRL_C_CHECK_INTERVAL: Duration = Duration::from_millis(100);
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Sleep;
|
|
|
|
|
|
|
|
impl Command for Sleep {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"sleep"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Delay for a specified amount of time."
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("sleep")
|
2022-11-09 21:55:05 +00:00
|
|
|
.input_output_types(vec![(Type::Nothing, Type::Nothing)])
|
2023-12-15 06:32:37 +00:00
|
|
|
.required("duration", SyntaxShape::Duration, "Time to sleep.")
|
|
|
|
.rest("rest", SyntaxShape::Duration, "Additional time.")
|
2021-11-28 21:15:32 +00:00
|
|
|
.category(Category::Platform)
|
|
|
|
}
|
|
|
|
|
2022-11-06 17:11:04 +00:00
|
|
|
fn search_terms(&self) -> Vec<&str> {
|
|
|
|
vec!["delay", "wait", "timer"]
|
|
|
|
}
|
|
|
|
|
2021-11-28 21:15:32 +00:00
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
|
|
|
call: &Call,
|
|
|
|
_input: PipelineData,
|
|
|
|
) -> Result<PipelineData, ShellError> {
|
|
|
|
fn duration_from_i64(val: i64) -> Duration {
|
|
|
|
Duration::from_nanos(if val < 0 { 0 } else { val as u64 })
|
|
|
|
}
|
|
|
|
|
|
|
|
let duration: i64 = call.req(engine_state, stack, 0)?;
|
|
|
|
let rest: Vec<i64> = call.rest(engine_state, stack, 1)?;
|
|
|
|
|
|
|
|
let total_dur =
|
|
|
|
duration_from_i64(duration) + rest.into_iter().map(duration_from_i64).sum::<Duration>();
|
2024-03-20 08:45:33 +00:00
|
|
|
let deadline = Instant::now() + total_dur;
|
2021-11-28 21:15:32 +00:00
|
|
|
|
|
|
|
loop {
|
2024-03-20 08:45:33 +00:00
|
|
|
// sleep for 100ms, or until the deadline
|
|
|
|
let time_until_deadline = deadline.saturating_duration_since(Instant::now());
|
|
|
|
if time_until_deadline.is_zero() {
|
2021-11-28 21:15:32 +00:00
|
|
|
break;
|
|
|
|
}
|
2024-03-20 08:45:33 +00:00
|
|
|
thread::sleep(CTRL_C_CHECK_INTERVAL.min(time_until_deadline));
|
|
|
|
// exit early if Ctrl+C was pressed
|
|
|
|
if nu_utils::ctrl_c::was_pressed(&engine_state.ctrlc) {
|
2023-09-05 14:21:30 +00:00
|
|
|
return Err(ShellError::InterruptedByUser {
|
|
|
|
span: Some(call.head),
|
|
|
|
});
|
2021-11-28 21:15:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-03 14:27:29 +00:00
|
|
|
Ok(Value::nothing(call.head).into_pipeline_data())
|
2021-11-28 21:15:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![
|
|
|
|
Example {
|
|
|
|
description: "Sleep for 1sec",
|
|
|
|
example: "sleep 1sec",
|
2023-09-03 14:27:29 +00:00
|
|
|
result: Some(Value::nothing(Span::test_data())),
|
2021-11-28 21:15:32 +00:00
|
|
|
},
|
2023-08-19 14:06:59 +00:00
|
|
|
Example {
|
|
|
|
description: "Sleep for 3sec",
|
|
|
|
example: "sleep 1sec 1sec 1sec",
|
|
|
|
result: None,
|
|
|
|
},
|
|
|
|
Example {
|
|
|
|
description: "Send output after 1sec",
|
|
|
|
example: "sleep 1sec; echo done",
|
|
|
|
result: None,
|
|
|
|
},
|
2021-11-28 21:15:32 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::Sleep;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn examples_work_as_expected() {
|
|
|
|
use crate::test_examples;
|
|
|
|
use std::time::Instant;
|
|
|
|
|
|
|
|
let start = Instant::now();
|
|
|
|
test_examples(Sleep {});
|
|
|
|
|
|
|
|
let elapsed = start.elapsed();
|
|
|
|
|
|
|
|
// only examples with actual output are run
|
|
|
|
assert!(elapsed >= std::time::Duration::from_secs(1));
|
|
|
|
assert!(elapsed < std::time::Duration::from_secs(2));
|
|
|
|
}
|
|
|
|
}
|