vector/top/
events.rs

1use crossterm::event::{Event, EventStream, KeyCode};
2use futures::StreamExt;
3use tokio::sync::{mpsc, oneshot};
4
5/// Capture keyboard input, and send it upstream via a channel. This is used for interaction
6/// with the dashboard, and exiting from `vector top`.
7pub fn capture_key_press() -> (mpsc::UnboundedReceiver<KeyCode>, oneshot::Sender<()>) {
8    let (tx, rx) = mpsc::unbounded_channel();
9    let (kill_tx, mut kill_rx) = oneshot::channel();
10
11    let mut events = EventStream::new();
12
13    tokio::spawn(async move {
14        loop {
15            tokio::select! {
16                biased;
17                _ = &mut kill_rx => return,
18                Some(Ok(event)) = events.next() => {
19                     if let Event::Key(k) = event {
20                        _ = tx.clone().send(k.code);
21                    };
22                }
23            }
24        }
25    });
26
27    (rx, kill_tx)
28}