sequent_repl/commands/
truncate.rs

1//! Truncation of the event timeline.
2
3use crate::commands::prompt::YesNo;
4use crate::Context;
5use revolver::command::{ApplyCommandError, ApplyOutcome, Command, Description, NamedCommandParser, ParseCommandError};
6use revolver::looper::Looper;
7use revolver::terminal::{Terminal};
8use std::borrow::Cow;
9use std::marker::PhantomData;
10use sequent::SimulationError;
11
12/// Command to truncate the event timeline at the current cursor location. The user will be given a
13/// yes/no prompt before proceeding with truncation.
14pub struct Truncate<S, C> {
15    __phantom_data: PhantomData<(S, C)>
16}
17
18impl<S, C> Default for Truncate<S, C> {
19    fn default() -> Self {
20        Self {
21            __phantom_data: PhantomData::default()
22        }
23    }
24}
25
26impl<S, C: Context<State = S>, T: Terminal> Command<T> for Truncate<S, C> {
27    type Context = C;
28    type Error = SimulationError<S>;
29
30    fn apply(&mut self, looper: &mut Looper<C, SimulationError<S>, T>) -> Result<ApplyOutcome, ApplyCommandError<SimulationError<S>>> {
31        let (terminal, _, context) = looper.split();
32        let response = terminal.read_from_str_default(
33            &format!(
34                "Truncate timeline from cursor location {}? [y/N]: ",
35                context.sim().cursor()
36            ),
37        )?;
38        match response {
39            YesNo::Yes => {
40                looper.context().sim().truncate();
41                Ok(ApplyOutcome::Applied)
42            }
43            YesNo::No => Ok(ApplyOutcome::Skipped),
44        }
45    }
46}
47
48/// Parser for [`Truncate`].
49pub struct Parser<S, C> {
50    __phantom_data: PhantomData<(S, C)>
51}
52
53impl<S, C> Default for Parser<S, C> {
54    fn default() -> Self {
55        Self {
56            __phantom_data: PhantomData::default()
57        }
58    }
59}
60
61impl<S: 'static, C: Context<State = S> + 'static, T: Terminal> NamedCommandParser<T> for Parser<S, C> {
62    type Context = C;
63    type Error = SimulationError<S>;
64
65    fn parse(&self, s: &str) -> Result<Box<dyn Command<T, Context = C, Error = SimulationError<S>>>, ParseCommandError> {
66        self.parse_no_args(s, Truncate::default)
67    }
68
69    fn shorthand(&self) -> Option<Cow<'static, str>> {
70        Some("tr".into())
71    }
72
73    fn name(&self) -> Cow<'static, str> {
74        "truncate".into()
75    }
76
77    fn description(&self) -> Description {
78        Description {
79            purpose: "Truncates the remaining events in the timeline.".into(),
80            usage: Cow::default(),
81            examples: Vec::default(),
82        }
83    }
84}
85
86#[cfg(test)]
87mod tests;