aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/ttyplay/input.rs
blob: f69423bddeea81257e086b4090a4e617b8f6b63f (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
pub fn spawn_task(
    event_w: async_std::channel::Sender<crate::event::Event>,
    mut input: textmode::Input,
) {
    async_std::task::spawn(async move {
        let mut search: Option<String> = None;
        let mut prev_search = None;
        while let Some(key) = input.read_key().await.unwrap() {
            if let Some(ref mut search_contents) = search {
                match key {
                    textmode::Key::Char(c) => {
                        search_contents.push(c);
                        event_w
                            .send(crate::event::Event::ActiveSearch(
                                search_contents.clone(),
                            ))
                            .await
                            .unwrap();
                    }
                    textmode::Key::Backspace => {
                        search_contents.pop();
                        event_w
                            .send(crate::event::Event::ActiveSearch(
                                search_contents.clone(),
                            ))
                            .await
                            .unwrap();
                    }
                    textmode::Key::Ctrl(b'm') => {
                        event_w
                            .send(crate::event::Event::RunSearch(
                                search_contents.clone(),
                                false,
                            ))
                            .await
                            .unwrap();
                        prev_search = search;
                        search = None;
                    }
                    textmode::Key::Escape => {
                        event_w
                            .send(crate::event::Event::CancelSearch)
                            .await
                            .unwrap();
                        search = None;
                    }
                    _ => {}
                }
            } else {
                let event = match key {
                    textmode::Key::Char('0') => {
                        crate::event::Event::TimerAction(
                            crate::event::TimerAction::FirstFrame,
                        )
                    }
                    textmode::Key::Char('$') => {
                        crate::event::Event::TimerAction(
                            crate::event::TimerAction::LastFrame,
                        )
                    }
                    textmode::Key::Char('l') => {
                        crate::event::Event::TimerAction(
                            crate::event::TimerAction::NextFrame,
                        )
                    }
                    textmode::Key::Char('h') => {
                        crate::event::Event::TimerAction(
                            crate::event::TimerAction::PreviousFrame,
                        )
                    }
                    textmode::Key::Char('q') => crate::event::Event::Quit,
                    textmode::Key::Char(' ') => {
                        crate::event::Event::TimerAction(
                            crate::event::TimerAction::Pause,
                        )
                    }
                    textmode::Key::Ctrl(b'i') => {
                        crate::event::Event::ToggleUi
                    }
                    textmode::Key::Char('?') => {
                        crate::event::Event::ToggleHelp
                    }
                    textmode::Key::Char('+') => {
                        crate::event::Event::TimerAction(
                            crate::event::TimerAction::SpeedUp,
                        )
                    }
                    textmode::Key::Char('-') => {
                        crate::event::Event::TimerAction(
                            crate::event::TimerAction::SlowDown,
                        )
                    }
                    textmode::Key::Char('=') => {
                        crate::event::Event::TimerAction(
                            crate::event::TimerAction::DefaultSpeed,
                        )
                    }
                    textmode::Key::Char('/') => {
                        search = Some("".to_string());
                        crate::event::Event::ActiveSearch("".to_string())
                    }
                    textmode::Key::Char('n') => {
                        if let Some(ref search) = prev_search {
                            crate::event::Event::RunSearch(
                                search.clone(),
                                false,
                            )
                        } else {
                            continue;
                        }
                    }
                    textmode::Key::Char('p') => {
                        if let Some(ref search) = prev_search {
                            crate::event::Event::RunSearch(
                                search.clone(),
                                true,
                            )
                        } else {
                            continue;
                        }
                    }
                    _ => continue,
                };
                event_w.send(event).await.unwrap();
            }
        }
    });
}