Skip to main content

xs/nu/commands/
cat_command.rs

1use nu_engine::CallExt;
2use nu_protocol::engine::{Call, Command, EngineState, Stack};
3use nu_protocol::{Category, PipelineData, ShellError, Signature, SyntaxShape, Type};
4
5use crate::store::{ReadOptions, Store};
6
7#[derive(Clone)]
8pub struct CatCommand {
9    store: Store,
10}
11
12impl CatCommand {
13    pub fn new(store: Store) -> Self {
14        Self { store }
15    }
16}
17
18impl Command for CatCommand {
19    fn name(&self) -> &str {
20        ".cat"
21    }
22
23    fn signature(&self) -> Signature {
24        Signature::build(".cat")
25            .input_output_types(vec![(Type::Nothing, Type::Any)])
26            .named(
27                "limit",
28                SyntaxShape::Int,
29                "limit the number of frames to retrieve",
30                None,
31            )
32            .named(
33                "after",
34                SyntaxShape::String,
35                "start after a specific frame ID (exclusive)",
36                Some('a'),
37            )
38            .named(
39                "from",
40                SyntaxShape::String,
41                "start from a specific frame ID (inclusive)",
42                None,
43            )
44            .named(
45                "last",
46                SyntaxShape::Int,
47                "return the N most recent frames",
48                None,
49            )
50            .named("topic", SyntaxShape::String, "filter by topic", Some('T'))
51            .category(Category::Experimental)
52    }
53
54    fn description(&self) -> &str {
55        "Reads the event stream and returns frames"
56    }
57
58    fn run(
59        &self,
60        engine_state: &EngineState,
61        stack: &mut Stack,
62        call: &Call,
63        _input: PipelineData,
64    ) -> Result<PipelineData, ShellError> {
65        let limit: Option<usize> = call.get_flag(engine_state, stack, "limit")?;
66        let last: Option<usize> = call.get_flag(engine_state, stack, "last")?;
67        let after: Option<String> = call.get_flag(engine_state, stack, "after")?;
68        let from: Option<String> = call.get_flag(engine_state, stack, "from")?;
69        let topic: Option<String> = call.get_flag(engine_state, stack, "topic")?;
70
71        // Helper to parse Scru128Id
72        let parse_id = |s: &str, name: &str| -> Result<scru128::Scru128Id, ShellError> {
73            s.parse().map_err(|e| ShellError::GenericError {
74                error: format!("Invalid {name}"),
75                msg: format!("Failed to parse Scru128Id: {e}"),
76                span: Some(call.head),
77                help: None,
78                inner: vec![],
79            })
80        };
81
82        let after: Option<scru128::Scru128Id> =
83            after.as_deref().map(|s| parse_id(s, "after")).transpose()?;
84        let from: Option<scru128::Scru128Id> =
85            from.as_deref().map(|s| parse_id(s, "from")).transpose()?;
86
87        let options = ReadOptions::builder()
88            .maybe_after(after)
89            .maybe_from(from)
90            .maybe_limit(limit)
91            .maybe_last(last)
92            .maybe_topic(topic)
93            .build();
94
95        let frames: Vec<_> = self.store.read_sync_with_options(options).collect();
96
97        use nu_protocol::Value;
98
99        let output = Value::list(
100            frames
101                .into_iter()
102                .map(|frame| crate::nu::util::frame_to_value(&frame, call.head))
103                .collect(),
104            call.head,
105        );
106
107        Ok(PipelineData::Value(output, None))
108    }
109}