xs/nu/commands/
last_command.rs1use nu_engine::CallExt;
2use nu_protocol::engine::{Call, Command, EngineState, Stack};
3use nu_protocol::{Category, PipelineData, ShellError, Signature, SyntaxShape, Type};
4
5use crate::nu::util;
6use crate::store::Store;
7
8#[derive(Clone)]
9pub struct LastCommand {
10 store: Store,
11}
12
13impl LastCommand {
14 pub fn new(store: Store) -> Self {
15 Self { store }
16 }
17}
18
19impl Command for LastCommand {
20 fn name(&self) -> &str {
21 ".last"
22 }
23
24 fn signature(&self) -> Signature {
25 Signature::build(".last")
26 .input_output_types(vec![(Type::Nothing, Type::Any)])
27 .required(
28 "topic",
29 SyntaxShape::String,
30 "topic to get most recent frame from",
31 )
32 .category(Category::Experimental)
33 }
34
35 fn description(&self) -> &str {
36 "get the most recent frame for a topic"
37 }
38
39 fn run(
40 &self,
41 engine_state: &EngineState,
42 stack: &mut Stack,
43 call: &Call,
44 _input: PipelineData,
45 ) -> Result<PipelineData, ShellError> {
46 let topic: String = call.req(engine_state, stack, 0)?;
47 let span = call.head;
48
49 if let Some(frame) = self.store.last(&topic) {
50 Ok(PipelineData::Value(
51 util::frame_to_value(&frame, span),
52 None,
53 ))
54 } else {
55 Ok(PipelineData::Empty)
56 }
57 }
58}