From ee89afa76a25d6c16b61db7423dce0844e3d062c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20M=C3=A4rtens?= Date: Mon, 31 Aug 2020 14:38:54 +0200 Subject: [PATCH] clean shutdown and do a full frame all 10 ticks. also increase polling time to 100ms and dont poll while drawing but afterwards --- server-cli/src/main.rs | 2 +- server-cli/src/tui_runner.rs | 54 ++++++++++++++++++++++++------------ 2 files changed, 38 insertions(+), 18 deletions(-) diff --git a/server-cli/src/main.rs b/server-cli/src/main.rs index 484e9e4e24..f40ba83a9b 100644 --- a/server-cli/src/main.rs +++ b/server-cli/src/main.rs @@ -11,7 +11,7 @@ use crate::{ }; use common::clock::Clock; use server::{Event, Input, Server, ServerSettings}; -use tracing::{error, info, warn, Level}; +use tracing::{info, Level}; use tracing_subscriber::{filter::LevelFilter, EnvFilter, FmtSubscriber}; use clap::{App, Arg}; diff --git a/server-cli/src/tui_runner.rs b/server-cli/src/tui_runner.rs index 874299f378..5773998eb3 100644 --- a/server-cli/src/tui_runner.rs +++ b/server-cli/src/tui_runner.rs @@ -6,7 +6,7 @@ use crossterm::{ }; use std::{ io::{self, Write}, - sync::mpsc, + sync::{Arc, mpsc, atomic::{AtomicBool, Ordering}}, time::Duration, }; use tracing::{error, info, warn}; @@ -58,6 +58,7 @@ pub const COMMANDS: [Command; 2] = [ pub struct Tui { msg_s: Option>, background: Option>, + running: Arc, } impl Tui { @@ -67,13 +68,12 @@ impl Tui { Self { msg_s: Some(msg_s), background: None, + running: Arc::new(AtomicBool::new(true)), }, msg_r, ) } - fn inner() {} - fn handle_events(input: &mut String, msg_s: &mut mpsc::Sender) { use crossterm::event::*; if let Event::Key(event) = read().unwrap() { @@ -142,10 +142,12 @@ impl Tui { let hook = std::panic::take_hook(); std::panic::set_hook(Box::new(move |info| { + Self::shutdown(); hook(info); })); let mut msg_s = self.msg_s.take().unwrap(); + let running = self.running.clone(); self.background = Some(std::thread::spawn(move || { // Start the tui @@ -155,10 +157,19 @@ impl Tui { let mut input = String::new(); - let _ = terminal.clear(); + if let Err(e) = terminal.clear() { + error!(?e, "clouldn't clean terminal"); + }; + let mut i: u64 = 0; - loop { - let _ = terminal.draw(|f| { + while running.load(Ordering::Relaxed) { + i += 1; + // This is a tmp fix that does a full redraw all 10 ticks, in case the backend breaks (which happens sometimes) + if i.rem_euclid(10) == 0 { + let size = terminal.size().unwrap(); + terminal.resize(size).unwrap(); + } + if let Err(e) = terminal.draw(|f| { let (log_rect, input_rect) = if f.size().height > 6 { let mut log_rect = f.size(); log_rect.height -= 3; @@ -179,7 +190,7 @@ impl Tui { let scroll = (LOG.height(size) as i16 - size.height as i16).max(0) as u16; - print!("{} {} {}", LOG.height(size) as i16, size.width, size.height); + //trace!(?i, "{} {} {}", LOG.height(size) as i16, size.width, size.height); let logger = Paragraph::new(LOG.inner.lock().unwrap().clone()) .block(block) @@ -198,23 +209,32 @@ impl Tui { f.render_widget(input_field, input_rect); f.set_cursor(x, size.y); - - use crossterm::event::*; - - if poll(Duration::from_millis(10)).unwrap() { - Self::handle_events(&mut input, &mut msg_s); - }; - }); + }) { + warn!(?e, "couldn't draw frame"); + }; + if crossterm::event::poll(Duration::from_millis(100)).unwrap() { + Self::handle_events(&mut input, &mut msg_s); + }; } + + if let Err(e) = terminal.clear() { + error!(?e, "clouldn't clean terminal"); + }; })); } -} -impl Drop for Tui { - fn drop(&mut self) { + fn shutdown() { let mut stdout = io::stdout(); disable_raw_mode().unwrap(); execute!(stdout, LeaveAlternateScreen, DisableMouseCapture).unwrap(); } } + +impl Drop for Tui { + fn drop(&mut self) { + self.running.store(false, Ordering::Relaxed); + self.background.take().map(|m| m.join()); + Self::shutdown(); + } +}