sequent_repl/commands/
timeline.rs1use std::borrow::Cow;
4use std::marker::PhantomData;
5use stanza::renderer::console::{Console, Decor};
6use stanza::renderer::Renderer;
7use stanza::style::{Bold, HAlign, MinWidth, Palette16, Styles, TextFg};
8use stanza::table::{Col, Row, Table};
9use sequent::{Simulation, SimulationError};
10use revolver::command::{ApplyCommandError, ApplyOutcome, Command, Description, NamedCommandParser, ParseCommandError};
11use revolver::looper::Looper;
12use revolver::terminal::Terminal;
13use crate::Context;
14
15pub struct Timeline<S, C> {
17 __phantom_data: PhantomData<(S, C)>
18}
19
20impl<S, C> Default for Timeline<S, C> {
21 fn default() -> Self {
22 Self {
23 __phantom_data: PhantomData::default()
24 }
25 }
26}
27
28impl<S, C: Context<State = S>, T: Terminal> Command<T> for Timeline<S, C> {
29 type Context = C;
30 type Error = SimulationError<S>;
31
32 fn apply(&mut self, looper: &mut Looper<C, SimulationError<S>, T>) -> Result<ApplyOutcome, ApplyCommandError<SimulationError<S>>> {
33 let (terminal, _, context) = looper.split();
34 let table = timeline(context.sim());
35 let renderer = Console(
36 Decor::default()
37 .suppress_all_lines()
38 .suppress_outer_border(),
39 );
40 terminal.print_line(&renderer.render(&table))?;
41 Ok(ApplyOutcome::Applied)
42 }
43}
44
45pub struct Parser<S, C> {
47 __phantom_data: PhantomData<(S, C)>
48}
49
50impl<S, C> Default for Parser<S, C> {
51 fn default() -> Self {
52 Self {
53 __phantom_data: PhantomData::default()
54 }
55 }
56}
57
58impl<S: 'static, C: Context<State = S> + 'static, T: Terminal> NamedCommandParser<T> for Parser<S, C> {
59 type Context = C;
60 type Error = SimulationError<S>;
61
62 fn parse(&self, s: &str) -> Result<Box<dyn Command<T, Context = C, Error = SimulationError<S>>>, ParseCommandError> {
63 self.parse_no_args(s, Timeline::default)
64 }
65
66 fn shorthand(&self) -> Option<Cow<'static, str>> {
67 Some("tl".into())
68 }
69
70 fn name(&self) -> Cow<'static, str> {
71 "timeline".into()
72 }
73
74 fn description(&self) -> Description {
75 Description {
76 purpose: "Displays the timeline of events.".into(),
77 usage: Cow::default(),
78 examples: Vec::default()
79 }
80 }
81}
82
83fn timeline<S>(simulation: &Simulation<S>) -> Table {
84 const CURSOR: &str = "▶";
85
86 let mut table = Table::default()
87 .with_cols(vec![
88 Col::new(Styles::default()),
89 Col::new(Styles::default().with(HAlign::Right)),
90 Col::new(Styles::default().with(MinWidth(15))),
91 Col::new(Styles::default().with(MinWidth(40))),
92 ])
93 .with_row(Row::new(
94 Styles::default()
95 .with(Bold(true))
96 .with(TextFg(Palette16::Yellow)),
97 vec![
98 "".into(),
99 "".into(),
100 "Event name".into(),
101 "Encoded event arguments".into(),
102 ],
103 ));
104
105 for (idx, event) in simulation.scenario().timeline.iter().enumerate() {
106 let on_cursor = idx == simulation.cursor();
107 table.push_row(Row::new(
108 Styles::default().with(Bold(on_cursor)),
109 vec![
110 if on_cursor {
111 CURSOR
112 } else {
113 ""
114 }
115 .into(),
116 idx.into(),
117 event.name().into(),
118 event.to_string().into(),
119 ],
120 ));
121 }
122
123 if simulation.cursor() == simulation.scenario().timeline.len() {
124 table.push_row(Row::new(Styles::default().with(Bold(true)), vec![
125 CURSOR.into(),
126 simulation.scenario().timeline.len().into(),
127 ]));
128 }
129
130 table
131}
132
133#[cfg(test)]
134mod tests;