Skip to main content

nu_command/filesystem/idx/
init.rs

1use super::state::init_runtime;
2use nu_engine::command_prelude::*;
3
4#[derive(Clone)]
5pub struct IdxInit;
6
7impl Command for IdxInit {
8    fn name(&self) -> &str {
9        "idx init"
10    }
11
12    fn signature(&self) -> Signature {
13        Signature::build(self.name())
14            .required("path", SyntaxShape::Directory, "Path to index.")
15            .switch(
16                "wait",
17                "Block until the initial scan completes before returning.",
18                Some('w'),
19            )
20            .input_output_types(vec![(Type::Nothing, Type::record())])
21            .category(Category::FileSystem)
22    }
23
24    fn description(&self) -> &str {
25        "Initialize the in-memory idx index for a path."
26    }
27
28    fn extra_description(&self) -> &str {
29        "By default idx init returns immediately and indexing continues in the background. Use `idx status` to check when scanning completes. Pass `--wait` to block until the initial scan finishes."
30    }
31
32    fn examples(&self) -> Vec<Example<'_>> {
33        vec![
34            Example {
35                description: "Initialize idx for the current directory",
36                example: "idx init .",
37                result: None,
38            },
39            Example {
40                description: "Initialize idx and wait for the initial scan to complete",
41                example: "idx init . --wait",
42                result: None,
43            },
44        ]
45    }
46
47    fn run(
48        &self,
49        engine_state: &EngineState,
50        stack: &mut Stack,
51        call: &Call,
52        _input: PipelineData,
53    ) -> Result<PipelineData, ShellError> {
54        let path: Spanned<String> = call.req(engine_state, stack, 0)?;
55        let wait = call.has_flag(engine_state, stack, "wait")?;
56        let cwd = engine_state.cwd(Some(stack))?;
57        let abs = nu_path::expand_path_with(path.item, cwd, true);
58        // There is a functionality in fff-search to update the index via watch but it was non-trivial to get working
59        // So, for now, let's always default to watch = false.
60        let watch = false;
61        let status = init_runtime(&abs, watch, wait, call.head)?;
62        Ok(PipelineData::value(status.to_value(call.head), None))
63    }
64}