nu-command 0.113.0

Nushell's built-in commands
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
use std::{
    path::{Path, PathBuf},
    sync::mpsc::{Receiver, RecvTimeoutError, channel},
    time::Duration,
};

use itertools::Either;
use notify_debouncer_full::{
    DebouncedEvent, Debouncer, FileIdMap, new_debouncer,
    notify::{
        self, EventKind, RecommendedWatcher, RecursiveMode, Watcher,
        event::{DataChange, ModifyKind, RenameMode},
    },
};

use nu_engine::{ClosureEval, command_prelude::*};
use nu_protocol::{
    Signals,
    engine::Closure,
    report_shell_error, report_shell_warning,
    shell_error::{generic::GenericError, io::IoError},
};

// durations chosen mostly arbitrarily
const CHECK_CTRL_C_FREQUENCY: Duration = Duration::from_millis(100);
const DEFAULT_WATCH_DEBOUNCE_DURATION: Duration = Duration::from_millis(100);

#[derive(Clone)]
pub struct Watch;

impl Command for Watch {
    fn name(&self) -> &str {
        "watch"
    }

    fn description(&self) -> &str {
        "Watch for file changes and execute Nu code when they happen."
    }

    fn extra_description(&self) -> &str {
        "When run without a closure, `watch` returns a stream of events instead."
    }

    fn search_terms(&self) -> Vec<&str> {
        vec!["watcher", "reload", "filesystem"]
    }

    fn signature(&self) -> nu_protocol::Signature {
        Signature::build("watch")
            .input_output_types(vec![
                (Type::Nothing, Type::Nothing),
                (
                    Type::Nothing,
                    Type::Table(
                        vec![
                            ("operation".into(), Type::String),
                            (
                                "path".into(),
                                Type::OneOf([Type::String, Type::Nothing].into()),
                            ),
                            (
                                "new_path".into(),
                                Type::OneOf([Type::String, Type::Nothing].into()),
                            ),
                        ]
                        .into_boxed_slice(),
                    ),
                ),
            ])
            .required(
                "path",
                SyntaxShape::Filepath,
                "The path to watch. Can be a file or directory.",
            )
            .optional(
                "closure",
                SyntaxShape::Closure(Some(vec![
                    SyntaxShape::String,
                    SyntaxShape::String,
                    SyntaxShape::String,
                ])),
                "Some Nu code to run whenever a file changes. \
                    The closure will be passed `operation`, `path`, \
                    and `new_path` (for renames only) arguments in that order (deprecated).",
            )
            .named(
                "debounce",
                SyntaxShape::Duration,
                "Debounce changes for this duration (default: 100ms). \
                    Adjust if you find that single writes are reported as multiple events.",
                Some('d'),
            )
            .named(
                "glob",
                // SyntaxShape::GlobPattern gets interpreted relative to cwd, so use String instead
                SyntaxShape::String,
                "Only report changes for files that match this glob pattern (default: all files)",
                Some('g'),
            )
            .named(
                "recursive",
                SyntaxShape::Boolean,
                "Watch all directories under `<path>` recursively. \
                    Will be ignored if `<path>` is a file (default: true).",
                Some('r'),
            )
            .switch(
                "quiet",
                "Hide the initial status message (default: false).",
                Some('q'),
            )
            .switch(
                "verbose",
                "Operate in verbose mode (default: false).",
                Some('v'),
            )
            .category(Category::FileSystem)
    }

    fn run(
        &self,
        engine_state: &EngineState,
        stack: &mut Stack,
        call: &Call,
        _input: PipelineData,
    ) -> Result<PipelineData, ShellError> {
        let head = call.head;
        let path = {
            let cwd = engine_state.cwd_as_string(Some(stack))?;
            let path_arg: Spanned<String> = call.req(engine_state, stack, 0)?;
            let path_no_whitespace = path_arg
                .item
                .trim_end_matches(|x| matches!(x, '\x09'..='\x0d'));

            nu_path::absolute_with(path_no_whitespace, cwd).map_err(|err| {
                ShellError::Io(IoError::new(
                    err,
                    path_arg.span,
                    PathBuf::from(path_no_whitespace),
                ))
            })?
        };
        let closure: Option<Spanned<Closure>> = call.opt(engine_state, stack, 1)?;
        let verbose = call.has_flag(engine_state, stack, "verbose")?;
        let quiet = call.has_flag(engine_state, stack, "quiet")?;
        let debounce_duration: Duration = call
            .get_flag(engine_state, stack, "debounce")?
            .unwrap_or(DEFAULT_WATCH_DEBOUNCE_DURATION);

        let glob_pattern = call
            .get_flag::<Spanned<String>>(engine_state, stack, "glob")?
            .map(|glob| {
                let absolute_path = path.join(glob.item);
                if verbose {
                    eprintln!("Absolute glob path: {absolute_path:?}");
                }

                nu_glob::Pattern::new(&absolute_path.to_string_lossy()).map_err(|_| {
                    ShellError::TypeMismatch {
                        err_message: "Glob pattern is invalid".to_string(),
                        span: glob.span,
                    }
                })
            })
            .transpose()?;

        let recursive_mode = {
            let recursive_flag = call
                .get_flag::<bool>(engine_state, stack, "recursive")?
                .unwrap_or(true);
            match recursive_flag {
                true => RecursiveMode::Recursive,
                false => RecursiveMode::NonRecursive,
            }
        };

        let iter = {
            let (tx, rx) = channel();
            let mut debouncer = new_debouncer(debounce_duration, None, tx)
                .and_then(|mut debouncer| {
                    debouncer.watcher().watch(&path, recursive_mode)?;
                    Ok(debouncer)
                })
                .map_err(|err| {
                    ShellError::Generic(GenericError::new(
                        "Failed to create watcher",
                        err.to_string(),
                        call.head,
                    ))
                })?;
            // need to cache to make sure that rename event works.
            debouncer.cache().add_root(&path, recursive_mode);
            WatchIterator::new(debouncer, rx, engine_state.signals().clone())
        };

        if let Some(closure) = closure {
            report_shell_warning(
                Some(stack),
                engine_state,
                &ShellWarning::Deprecated {
                    dep_type: "Argument".into(),
                    label: "remove this".into(),
                    span: closure.span,
                    help: "Since 0.113.0, running a closure with `watch` is deprecated. \
                            Instead, use `watch` by piping its output to `each` \
                            or as the source of a `for` loop."
                        .to_string()
                        .into(),
                    report_mode: nu_protocol::ReportMode::FirstUse,
                },
            );

            #[allow(deprecated)]
            run_closure(
                engine_state,
                stack,
                head,
                quiet,
                verbose,
                &path,
                glob_pattern,
                iter,
                closure.item,
            )
        } else {
            let out = iter
                .flat_map(|e| match e {
                    Ok(events) => Either::Right(events.into_iter().map(Ok)),
                    Err(err) => Either::Left(std::iter::once(Err(err))),
                })
                .filter_map(move |e| match e {
                    Ok(ev) if glob_filter(glob_pattern.as_ref(), &ev) => Some(ev.into_value(head)),
                    Ok(_) => None,
                    Err(err) => Some(Value::error(err, head)),
                })
                .into_pipeline_data(head, engine_state.signals().clone());
            Ok(out)
        }
    }

    fn examples(&self) -> Vec<Example<'_>> {
        vec![
            Example {
                description: "Run `cargo test` whenever a Rust file changes.",
                example: "for _ in (watch . --glob=**/*.rs) { cargo test }",
                result: None,
            },
            Example {
                description: "Watch all changes in the current directory.",
                example: "watch . | each { print }",
                result: None,
            },
            Example {
                description: "Filter, limit and modify `watch`'s output by using it as part of a pipeline.",
                example: r#"watch /foo/bar
    | where operation == Create
    | first 5
    | each {|e| $"New file!: ($e.path)" }
    | to text
    | save --append changes_in_bar.log"#,
                result: None,
            },
            Example {
                description: "Print file changes with a debounce time of 5 minutes.",
                example: r#"watch /foo/bar --debounce 5min | each {|e| $"Registered ($e.operation) on ($e.path)" | print }"#,
                result: None,
            },
            Example {
                description: "Note: if you are looking to run a command every N units of time, this can be accomplished with a loop and sleep.",
                example: "loop { command; sleep duration }",
                result: None,
            },
            Example {
                description: "Run `cargo test` whenever a Rust file changes (with the deprecated closure argument).",
                example: "watch . --glob=**/*.rs {|| cargo test }",
                result: None,
            },
        ]
    }
}

fn glob_filter(glob: Option<&nu_glob::Pattern>, ev: &WatchEvent) -> bool {
    let Some(glob) = glob else { return true };
    let path = ev
        .path
        .as_deref()
        .or(ev.new_path.as_deref())
        .expect("at least one of path or new_path should be present");
    glob.matches_path(path)
}

#[allow(clippy::too_many_arguments)]
#[inline]
#[deprecated(since = "0.113.0")]
fn run_closure(
    engine_state: &EngineState,
    stack: &mut Stack,
    head: Span,
    quiet: bool,
    verbose: bool,
    path: &Path,
    glob_pattern: Option<nu_glob::Pattern>,
    iter: WatchIterator,
    closure: Closure,
) -> Result<PipelineData, ShellError> {
    if !quiet {
        eprintln!("Now watching files at {path:?}. Press ctrl+c to abort.");
    }

    let mut closure = ClosureEval::new(engine_state, stack, closure);
    for events in iter {
        for event in events? {
            let matches_glob = glob_filter(glob_pattern.as_ref(), &event);

            if verbose && glob_pattern.is_some() {
                eprintln!("Matches glob: {matches_glob}");
            }

            if matches_glob {
                let result = closure
                    .add_arg(event.operation.into_value(head))?
                    .add_arg(event.path.into_value(head))?
                    .add_arg(event.new_path.into_value(head))?
                    .run_with_input(PipelineData::empty());

                match result {
                    Ok(val) => val.print_table(engine_state, stack, false, false)?,
                    Err(err) => report_shell_error(Some(stack), engine_state, &err),
                };
            }
        }
    }

    Ok(PipelineData::empty())
}

#[derive(IntoValue)]
struct WatchEvent {
    operation: WatchEventKind,
    path: Option<PathBuf>,
    new_path: Option<PathBuf>,
}

#[derive(IntoValue)]
#[nu_value(rename_all = "UpperCamelCase")]
enum WatchEventKind {
    Create,
    Write,
    Rename,
    Remove,
}

impl TryFrom<EventKind> for WatchEventKind {
    type Error = ();

    fn try_from(value: EventKind) -> Result<Self, Self::Error> {
        Ok(match value {
            EventKind::Create(_) => Self::Create,
            EventKind::Remove(_) => Self::Remove,
            EventKind::Modify(
                ModifyKind::Data(DataChange::Content | DataChange::Any) | ModifyKind::Any,
            ) => Self::Write,
            EventKind::Modify(ModifyKind::Name(
                RenameMode::Both | RenameMode::From | RenameMode::To,
            )) => Self::Rename,
            _ => return Err(()),
        })
    }
}

impl TryFrom<DebouncedEvent> for WatchEvent {
    type Error = ();

    fn try_from(ev: DebouncedEvent) -> Result<Self, Self::Error> {
        // TODO: Maybe we should handle all event kinds?
        let DebouncedEvent {
            event: notify::Event {
                kind, mut paths, ..
            },
            ..
        } = ev;

        let (path, new_path) = match paths.as_mut_slice() {
            [path] => (std::mem::take(path), None),
            [path, new_path] => (std::mem::take(path), Some(std::mem::take(new_path))),
            _ => return Err(()),
        };

        if let EventKind::Modify(ModifyKind::Name(RenameMode::To)) = kind {
            Ok(WatchEvent {
                operation: WatchEventKind::Rename,
                path: None,
                new_path: Some(path),
            })
        } else {
            Ok(WatchEvent {
                operation: kind.try_into()?,
                path: Some(path),
                new_path,
            })
        }
    }
}

struct WatchIterator {
    /// Debouncer needs to be kept alive for `rx` to keep receiving events.
    _debouncer: Debouncer<RecommendedWatcher, FileIdMap>,
    rx: Option<Receiver<Result<Vec<DebouncedEvent>, Vec<notify::Error>>>>,
    signals: Signals,
}

impl WatchIterator {
    fn new(
        debouncer: Debouncer<RecommendedWatcher, FileIdMap>,
        rx: Receiver<Result<Vec<DebouncedEvent>, Vec<notify::Error>>>,
        signals: Signals,
    ) -> Self {
        Self {
            _debouncer: debouncer,
            rx: Some(rx),
            signals,
        }
    }
}

impl Iterator for WatchIterator {
    type Item = Result<Vec<WatchEvent>, ShellError>;

    fn next(&mut self) -> Option<Self::Item> {
        let rx = self.rx.as_ref()?;
        while !self.signals.interrupted() {
            let x = match rx.recv_timeout(CHECK_CTRL_C_FREQUENCY) {
                Ok(x) => x,
                Err(RecvTimeoutError::Timeout) => continue,
                Err(RecvTimeoutError::Disconnected) => {
                    self.rx = None;
                    return Some(Err(ShellError::Generic(GenericError::new_internal(
                        "Disconnected",
                        "Unexpected disconnect from file watcher",
                    ))));
                }
            };

            let Ok(events) = x else {
                self.rx = None;
                return Some(Err(ShellError::Generic(GenericError::new_internal(
                    "Receiving events failed",
                    "Unexpected errors when receiving events",
                ))));
            };

            let watch_events = events
                .into_iter()
                .filter_map(|ev| WatchEvent::try_from(ev).ok())
                .collect::<Vec<_>>();

            return Some(Ok(watch_events));
        }
        self.rx = None;
        None
    }
}