terminal resizing events

This commit is contained in:
Michael Sippel 2020-12-14 19:35:35 +01:00
parent eebabdda2d
commit c776a1a08e
Signed by: senvas
GPG key ID: F96CF119C34B64A6
2 changed files with 37 additions and 11 deletions

View file

@ -8,6 +8,8 @@ version = "0.1.0"
[dependencies] [dependencies]
cgmath = "*" cgmath = "*"
termion = "*" termion = "*"
signal-hook = "*"
signal-hook-async-std = "*"
[dependencies.async-std] [dependencies.async-std]
version = "1.7.0" version = "1.7.0"

View file

@ -1,6 +1,12 @@
use { use {
std::io::{Write, stdout, stdin}, std::io::{Write, stdout, stdin},
async_std::{
stream::StreamExt,
task
},
signal_hook,
signal_hook_async_std::Signals,
cgmath::Vector2, cgmath::Vector2,
termion::{ termion::{
raw::IntoRawMode, raw::IntoRawMode,
@ -15,12 +21,13 @@ use {
}; };
pub enum TerminalEvent { pub enum TerminalEvent {
Resize((u16, u16)), Resize(Vector2<i16>),
Input(termion::event::Event) Input(termion::event::Event)
} }
pub struct Terminal { pub struct Terminal {
events: ChannelReceiver<Vec<TerminalEvent>> events: ChannelReceiver<Vec<TerminalEvent>>,
signal_handle: signal_hook_async_std::Handle
} }
impl Terminal { impl Terminal {
@ -33,21 +40,38 @@ impl Terminal {
input_tx.notify(TerminalEvent::Input(event.unwrap())); input_tx.notify(TerminalEvent::Input(event.unwrap()));
} }
}); });
/*
let mut resize_stream = signal(tokio::signal::unix::SignalKind::window_change()).unwrap(); // send initial teriminal size
let resize_tx = event_tx.clone(); let (w,h) = termion::terminal_size().unwrap();
tokio::spawn(async move { event_tx.notify(TerminalEvent::Resize(Vector2::new(w as i16, h as i16)));
loop {
resize_stream.recv().await; // and again on SIGWINCH
resize_tx.send(TerminalEvent::Resize(termion::terminal_size().unwrap())); let signals = Signals::new(&[ signal_hook::SIGWINCH ]).unwrap();
let handle = signals.handle();
task::spawn(async move {
let mut signals = signals.fuse();
while let Some(signal) = signals.next().await {
match signal {
signal_hook::SIGWINCH => {
let (w,h) = termion::terminal_size().unwrap();
event_tx.notify(TerminalEvent::Resize(Vector2::new(w as i16, h as i16)));
},
_ => unreachable!(),
}
} }
}); });
*/
Terminal { Terminal {
events: event_rx events: event_rx,
signal_handle: handle
} }
} }
pub async fn next_event(&mut self) -> TerminalEvent {
self.events.next().await.unwrap()
}
pub async fn show(view_port: OuterViewPort<Vector2<i16>, TerminalAtom>) -> std::io::Result<()> { pub async fn show(view_port: OuterViewPort<Vector2<i16>, TerminalAtom>) -> std::io::Result<()> {
let (atom_tx, atom_rx) = crate::channel::queue_channel(); let (atom_tx, atom_rx) = crate::channel::queue_channel();