pub struct ThreadProtocol { /* private fields */ }Expand description
The state of a ThreadImage.
Has inner [ResizeProtocol] that is sent off to the tx mspc channel to do the
resize_encode() work.
Implementations§
Source§impl ThreadProtocol
impl ThreadProtocol
Sourcepub fn new(
tx: Sender<(StatefulProtocol, Resize, Rect)>,
inner: StatefulProtocol,
) -> ThreadProtocol
pub fn new( tx: Sender<(StatefulProtocol, Resize, Rect)>, inner: StatefulProtocol, ) -> ThreadProtocol
Examples found in repository?
examples/async.rs (line 77)
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
fn main() -> Result<(), Box<dyn std::error::Error>> {
// setup terminal
enable_raw_mode()?;
let mut stdout = io::stdout();
execute!(stdout, EnterAlternateScreen,)?;
let backend = CrosstermBackend::new(stdout);
let mut terminal = Terminal::new(backend)?;
let mut picker = Picker::from_query_stdio()?;
let dyn_img = image::io::Reader::open("./assets/Ada.png")?.decode()?;
// Send a [ResizeProtocol] to resize and encode it in a separate thread.
let (tx_worker, rec_worker) = mpsc::channel::<(StatefulProtocol, Resize, Rect)>();
// Send UI-events and the [ResizeProtocol] result back to main thread.
let (tx_main, rec_main) = mpsc::channel();
// Resize and encode in background thread.
let tx_main_render = tx_main.clone();
thread::spawn(move || loop {
if let Ok((mut protocol, resize, area)) = rec_worker.recv() {
protocol.resize_encode(&resize, protocol.background_color(), area);
tx_main_render.send(AppEvent::Redraw(protocol)).unwrap();
}
});
// Poll events in background thread to demonstrate polling terminal events and redraw events
// concurrently. It's not required to do it this way - the "redraw event" from the channel
// could be read after polling the terminal events (as long as it's done with a timout). But
// then the rendering of the image will always be somewhat delayed.
let tx_main_events = tx_main.clone();
thread::spawn(move || -> Result<(), std::io::Error> {
loop {
if ratatui::crossterm::event::poll(Duration::from_millis(1000))? {
if let Event::Key(key) = event::read()? {
tx_main_events.send(AppEvent::KeyEvent(key)).unwrap();
}
}
}
});
let mut app = App {
async_state: ThreadProtocol::new(tx_worker, picker.new_resize_protocol(dyn_img)),
};
loop {
terminal.draw(|f| ui(f, &mut app))?;
if let Ok(ev) = rec_main.try_recv() {
match ev {
AppEvent::KeyEvent(key) => {
if key.kind == KeyEventKind::Press {
if key.code == KeyCode::Char('q') {
break;
}
}
}
AppEvent::Redraw(protocol) => {
app.async_state.set_protocol(protocol);
}
}
}
}
// restore terminal
disable_raw_mode()?;
execute!(terminal.backend_mut(), LeaveAlternateScreen,)?;
terminal.show_cursor()?;
Ok(())
}Sourcepub fn set_protocol(&mut self, proto: StatefulProtocol)
pub fn set_protocol(&mut self, proto: StatefulProtocol)
Examples found in repository?
examples/async.rs (line 93)
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
fn main() -> Result<(), Box<dyn std::error::Error>> {
// setup terminal
enable_raw_mode()?;
let mut stdout = io::stdout();
execute!(stdout, EnterAlternateScreen,)?;
let backend = CrosstermBackend::new(stdout);
let mut terminal = Terminal::new(backend)?;
let mut picker = Picker::from_query_stdio()?;
let dyn_img = image::io::Reader::open("./assets/Ada.png")?.decode()?;
// Send a [ResizeProtocol] to resize and encode it in a separate thread.
let (tx_worker, rec_worker) = mpsc::channel::<(StatefulProtocol, Resize, Rect)>();
// Send UI-events and the [ResizeProtocol] result back to main thread.
let (tx_main, rec_main) = mpsc::channel();
// Resize and encode in background thread.
let tx_main_render = tx_main.clone();
thread::spawn(move || loop {
if let Ok((mut protocol, resize, area)) = rec_worker.recv() {
protocol.resize_encode(&resize, protocol.background_color(), area);
tx_main_render.send(AppEvent::Redraw(protocol)).unwrap();
}
});
// Poll events in background thread to demonstrate polling terminal events and redraw events
// concurrently. It's not required to do it this way - the "redraw event" from the channel
// could be read after polling the terminal events (as long as it's done with a timout). But
// then the rendering of the image will always be somewhat delayed.
let tx_main_events = tx_main.clone();
thread::spawn(move || -> Result<(), std::io::Error> {
loop {
if ratatui::crossterm::event::poll(Duration::from_millis(1000))? {
if let Event::Key(key) = event::read()? {
tx_main_events.send(AppEvent::KeyEvent(key)).unwrap();
}
}
}
});
let mut app = App {
async_state: ThreadProtocol::new(tx_worker, picker.new_resize_protocol(dyn_img)),
};
loop {
terminal.draw(|f| ui(f, &mut app))?;
if let Ok(ev) = rec_main.try_recv() {
match ev {
AppEvent::KeyEvent(key) => {
if key.kind == KeyEventKind::Press {
if key.code == KeyCode::Char('q') {
break;
}
}
}
AppEvent::Redraw(protocol) => {
app.async_state.set_protocol(protocol);
}
}
}
}
// restore terminal
disable_raw_mode()?;
execute!(terminal.backend_mut(), LeaveAlternateScreen,)?;
terminal.show_cursor()?;
Ok(())
}Auto Trait Implementations§
impl Freeze for ThreadProtocol
impl RefUnwindSafe for ThreadProtocol
impl Send for ThreadProtocol
impl Sync for ThreadProtocol
impl Unpin for ThreadProtocol
impl UnwindSafe for ThreadProtocol
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more