Files
nushell/crates/nu-command/src/platform/sleep.rs
Stefan Holderbach 95b78eee25 Change the usage misnomer to "description" (#13598)
# Description
    
The meaning of the word usage is specific to describing how a command
function is *used* and not a synonym for general description. Usage can
be used to describe the SYNOPSIS or EXAMPLES sections of a man page
where the permitted argument combinations are shown or example *uses*
are given.
Let's not confuse people and call it what it is a description.

Our `help` command already creates its own *Usage* section based on the
available arguments and doesn't refer to the description with usage.

# User-Facing Changes

`help commands` and `scope commands` will now use `description` or
`extra_description`
`usage`-> `description`
`extra_usage` -> `extra_description`

Breaking change in the plugin protocol:

In the signature record communicated with the engine.
`usage`-> `description`
`extra_usage` -> `extra_description`

The same rename also takes place for the methods on
`SimplePluginCommand` and `PluginCommand`

# Tests + Formatting
- Updated plugin protocol specific changes
# After Submitting
- [ ] update plugin protocol doc
2024-08-22 12:02:08 +02:00

105 lines
2.9 KiB
Rust

use nu_engine::command_prelude::*;
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 description(&self) -> &str {
"Delay for a specified amount of time."
}
fn signature(&self) -> Signature {
Signature::build("sleep")
.input_output_types(vec![(Type::Nothing, Type::Nothing)])
.required("duration", SyntaxShape::Duration, "Time to sleep.")
.rest("rest", SyntaxShape::Duration, "Additional time.")
.category(Category::Platform)
}
fn search_terms(&self) -> Vec<&str> {
vec!["delay", "wait", "timer"]
}
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>();
let deadline = Instant::now() + total_dur;
loop {
// sleep for 100ms, or until the deadline
let time_until_deadline = deadline.saturating_duration_since(Instant::now());
if time_until_deadline.is_zero() {
break;
}
thread::sleep(CTRL_C_CHECK_INTERVAL.min(time_until_deadline));
engine_state.signals().check(call.head)?;
}
Ok(Value::nothing(call.head).into_pipeline_data())
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Sleep for 1sec",
example: "sleep 1sec",
result: Some(Value::nothing(Span::test_data())),
},
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,
},
]
}
}
#[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));
}
}