mirror of
https://github.com/nushell/nushell.git
synced 2024-11-25 09:53:43 +01:00
Fix inaccurate sleep duration (#12235)
# Description Improves the accuracy of sleep when the duration is larger than 100ms. Fixes #12223. # User-Facing Changes Sleeping for 150ms should work now. ```nushell ~/nushell> timeit { sleep 150ms } 03/19/2024 10:41:55 AM AM 151ms 344µs 201ns ```
This commit is contained in:
parent
f56070cbcd
commit
f8c1e03ea7
@ -52,16 +52,17 @@ impl Command for Sleep {
|
|||||||
|
|
||||||
let total_dur =
|
let total_dur =
|
||||||
duration_from_i64(duration) + rest.into_iter().map(duration_from_i64).sum::<Duration>();
|
duration_from_i64(duration) + rest.into_iter().map(duration_from_i64).sum::<Duration>();
|
||||||
|
let deadline = Instant::now() + total_dur;
|
||||||
|
|
||||||
let ctrlc_ref = &engine_state.ctrlc.clone();
|
|
||||||
let start = Instant::now();
|
|
||||||
loop {
|
loop {
|
||||||
thread::sleep(CTRL_C_CHECK_INTERVAL.min(total_dur));
|
// sleep for 100ms, or until the deadline
|
||||||
if start.elapsed() >= total_dur {
|
let time_until_deadline = deadline.saturating_duration_since(Instant::now());
|
||||||
|
if time_until_deadline.is_zero() {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
thread::sleep(CTRL_C_CHECK_INTERVAL.min(time_until_deadline));
|
||||||
if nu_utils::ctrl_c::was_pressed(ctrlc_ref) {
|
// exit early if Ctrl+C was pressed
|
||||||
|
if nu_utils::ctrl_c::was_pressed(&engine_state.ctrlc) {
|
||||||
return Err(ShellError::InterruptedByUser {
|
return Err(ShellError::InterruptedByUser {
|
||||||
span: Some(call.head),
|
span: Some(call.head),
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user