aboutsummaryrefslogtreecommitdiffstats
path: root/src/cmd/play.rs
blob: 427f611ac04c51025fddd209d1f7c4ffa733c53f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
use crate::prelude::*;
use std::io::Write as _;

#[derive(serde::Deserialize, Debug, Default)]
pub struct Config {
    #[serde(default)]
    ttyrec: crate::config::Ttyrec,

    #[serde(default)]
    play: crate::config::Play,
}

impl crate::config::Config for Config {
    fn merge_args<'a>(
        &mut self,
        matches: &clap::ArgMatches<'a>,
    ) -> Result<()> {
        self.ttyrec.merge_args(matches)?;
        self.play.merge_args(matches)?;
        Ok(())
    }

    fn run(
        &self,
    ) -> Box<dyn futures::future::Future<Item = (), Error = Error> + Send>
    {
        Box::new(PlaySession::new(
            &self.ttyrec.filename,
            self.play.playback_ratio,
            self.play.max_frame_length,
        ))
    }
}

pub fn cmd<'a, 'b>(app: clap::App<'a, 'b>) -> clap::App<'a, 'b> {
    crate::config::Ttyrec::cmd(crate::config::Play::cmd(
        app.about("Play recorded terminal sessions"),
    ))
}

pub fn config(
    config: Option<config::Config>,
) -> Result<Box<dyn crate::config::Config>> {
    let config: Config = if let Some(config) = config {
        config
            .try_into()
            .context(crate::error::CouldntParseConfig)?
    } else {
        Config::default()
    };
    Ok(Box::new(config))
}

#[allow(clippy::large_enum_variant)]
enum FileState {
    Closed {
        filename: String,
    },
    Opening {
        filename: String,
        fut: tokio::fs::file::OpenFuture<String>,
    },
    Open {
        reader: ttyrec::Reader<tokio::fs::File>,
    },
    Eof,
}

struct PlaySession {
    file: FileState,
    playback_ratio: f32,
    max_frame_length: Option<std::time::Duration>,

    raw_screen: Option<crossterm::RawScreen>,
    key_reader: crate::key_reader::KeyReader,
    to_write: DumbDelayQueue<Vec<u8>>,
    // to_write: tokio::timer::delay_queue::DelayQueue<Vec<u8>>,
    base_time: std::time::Instant,
    last_frame_time: std::time::Duration,
    total_time_clamped: std::time::Duration,
    paused: Option<std::time::Instant>,
}

impl PlaySession {
    fn new(
        filename: &str,
        playback_ratio: f32,
        max_frame_length: Option<std::time::Duration>,
    ) -> Self {
        Self {
            file: FileState::Closed {
                filename: filename.to_string(),
            },
            playback_ratio,
            max_frame_length,

            raw_screen: None,
            key_reader: crate::key_reader::KeyReader::new(),
            to_write: DumbDelayQueue::new(),
            // to_write: tokio::timer::delay_queue::DelayQueue::new(),
            base_time: std::time::Instant::now(),
            last_frame_time: std::time::Duration::default(),
            total_time_clamped: std::time::Duration::default(),
            paused: None,
        }
    }

    fn keypress(&mut self, e: &crossterm::InputEvent) -> Result<bool> {
        match e {
            crossterm::InputEvent::Keyboard(crossterm::KeyEvent::Char(
                'q',
            )) => return Ok(true),
            crossterm::InputEvent::Keyboard(crossterm::KeyEvent::Char(
                ' ',
            )) => {
                if let Some(time) = self.paused.take() {
                    let diff = std::time::Instant::now() - time;
                    self.to_write.time_incr(diff);
                    self.base_time += diff;
                } else {
                    self.paused = Some(std::time::Instant::now());
                }
            }
            _ => {}
        }
        Ok(false)
    }
}

impl PlaySession {
    const POLL_FNS:
        &'static [&'static dyn for<'a> Fn(
            &'a mut Self,
        )
            -> component_future::Poll<
            (),
            Error,
        >] = &[
        &Self::poll_open_file,
        &Self::poll_read_file,
        &Self::poll_input,
        &Self::poll_write_terminal,
    ];

    fn poll_open_file(&mut self) -> component_future::Poll<(), Error> {
        match &mut self.file {
            FileState::Closed { filename } => {
                self.file = FileState::Opening {
                    filename: filename.to_string(),
                    fut: tokio::fs::File::open(filename.to_string()),
                };
                Ok(component_future::Async::DidWork)
            }
            FileState::Opening { filename, fut } => {
                let file = component_future::try_ready!(fut
                    .poll()
                    .with_context(|| {
                        crate::error::OpenFile {
                            filename: filename.to_string(),
                        }
                    }));
                let reader = ttyrec::Reader::new(file);
                self.file = FileState::Open { reader };
                Ok(component_future::Async::DidWork)
            }
            _ => Ok(component_future::Async::NothingToDo),
        }
    }

    fn poll_read_file(&mut self) -> component_future::Poll<(), Error> {
        if let FileState::Open { reader } = &mut self.file {
            if let Some(frame) = component_future::try_ready!(reader
                .poll_read()
                .context(crate::error::ReadTtyrec))
            {
                let frame_time = frame.time - reader.offset().unwrap();
                let frame_dur = (frame_time - self.last_frame_time)
                    .div_f32(self.playback_ratio);
                self.total_time_clamped += self
                    .max_frame_length
                    .map_or(frame_dur, |max_frame_length| {
                        frame_dur.min(max_frame_length)
                    });

                self.to_write.insert_at(
                    frame.data,
                    self.base_time + self.total_time_clamped,
                );

                self.last_frame_time = frame_time;
            } else {
                self.file = FileState::Eof;
            }
            Ok(component_future::Async::DidWork)
        } else {
            Ok(component_future::Async::NothingToDo)
        }
    }

    fn poll_input(&mut self) -> component_future::Poll<(), Error> {
        if self.raw_screen.is_none() {
            self.raw_screen = Some(
                crossterm::RawScreen::into_raw_mode()
                    .context(crate::error::ToRawMode)?,
            );
        }

        let e = component_future::try_ready!(self.key_reader.poll()).unwrap();
        let quit = self.keypress(&e)?;
        if quit {
            self.raw_screen = None;

            // TODO async
            let stdout = std::io::stdout();
            let mut stdout = stdout.lock();
            stdout
                .write(b"\x1bc")
                .context(crate::error::WriteTerminal)?;
            stdout.flush().context(crate::error::FlushTerminal)?;
            Ok(component_future::Async::Ready(()))
        } else {
            Ok(component_future::Async::DidWork)
        }
    }

    fn poll_write_terminal(&mut self) -> component_future::Poll<(), Error> {
        if self.paused.is_some() {
            return Ok(component_future::Async::NothingToDo);
        }

        if let Some(data) = component_future::try_ready!(self
            .to_write
            .poll()
            .context(crate::error::Sleep))
        {
            // TODO async
            let stdout = std::io::stdout();
            let mut stdout = stdout.lock();
            stdout.write(&data).context(crate::error::WriteTerminal)?;
            stdout.flush().context(crate::error::FlushTerminal)?;
            Ok(component_future::Async::DidWork)
        } else if let FileState::Eof = self.file {
            Ok(component_future::Async::Ready(()))
        } else {
            Ok(component_future::Async::NothingToDo)
        }
    }
}

#[must_use = "futures do nothing unless polled"]
impl futures::future::Future for PlaySession {
    type Item = ();
    type Error = Error;

    fn poll(&mut self) -> futures::Poll<Self::Item, Self::Error> {
        component_future::poll_future(self, Self::POLL_FNS)
    }
}

// XXX tokio's delay_queue implementation seems to have a bug when
// interleaving inserts and polls - if you insert some entries, then poll
// successfully, then insert some more entries, the task won't get notified to
// wake up until the first entry after the successful poll is ready, instead
// of when the next entry of the original set is ready
// NOTE: this implementation is, as its name indicates, pretty dumb - it
// requires the entries to be inserted in order or else it won't work. this is
// fine for reading ttyrecs, but is probably not great for a general purpose
// thing.
struct DumbDelayQueueEntry<T> {
    timer: tokio::timer::Delay,
    data: T,
}

struct DumbDelayQueue<T> {
    queue: std::collections::VecDeque<DumbDelayQueueEntry<T>>,
}

impl<T> DumbDelayQueue<T> {
    fn new() -> Self {
        Self {
            queue: std::collections::VecDeque::new(),
        }
    }

    fn insert_at(&mut self, data: T, time: std::time::Instant) {
        self.queue.push_back(DumbDelayQueueEntry {
            data,
            timer: tokio::timer::Delay::new(time),
        })
    }

    fn time_incr(&mut self, dur: std::time::Duration) {
        for entry in &mut self.queue {
            entry.timer.reset(entry.timer.deadline() + dur);
        }
    }
}

#[must_use = "streams do nothing unless polled"]
impl<T> futures::stream::Stream for DumbDelayQueue<T> {
    type Item = T;
    type Error = tokio::timer::Error;

    fn poll(&mut self) -> futures::Poll<Option<Self::Item>, Self::Error> {
        if let Some(mut entry) = self.queue.pop_front() {
            match entry.timer.poll() {
                Ok(futures::Async::Ready(_)) => {
                    Ok(futures::Async::Ready(Some(entry.data)))
                }
                Ok(futures::Async::NotReady) => {
                    self.queue.push_front(entry);
                    Ok(futures::Async::NotReady)
                }
                Err(e) => {
                    self.queue.push_front(entry);
                    Err(e)
                }
            }
        } else {
            Ok(futures::Async::Ready(None))
        }
    }
}