mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
clean shutdown and do a full frame all 10 ticks. also increase polling time to 100ms and dont poll while drawing but afterwards
This commit is contained in:
parent
6ca4f31a9e
commit
ee89afa76a
@ -11,7 +11,7 @@ use crate::{
|
|||||||
};
|
};
|
||||||
use common::clock::Clock;
|
use common::clock::Clock;
|
||||||
use server::{Event, Input, Server, ServerSettings};
|
use server::{Event, Input, Server, ServerSettings};
|
||||||
use tracing::{error, info, warn, Level};
|
use tracing::{info, Level};
|
||||||
use tracing_subscriber::{filter::LevelFilter, EnvFilter, FmtSubscriber};
|
use tracing_subscriber::{filter::LevelFilter, EnvFilter, FmtSubscriber};
|
||||||
|
|
||||||
use clap::{App, Arg};
|
use clap::{App, Arg};
|
||||||
|
@ -6,7 +6,7 @@ use crossterm::{
|
|||||||
};
|
};
|
||||||
use std::{
|
use std::{
|
||||||
io::{self, Write},
|
io::{self, Write},
|
||||||
sync::mpsc,
|
sync::{Arc, mpsc, atomic::{AtomicBool, Ordering}},
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
use tracing::{error, info, warn};
|
use tracing::{error, info, warn};
|
||||||
@ -58,6 +58,7 @@ pub const COMMANDS: [Command; 2] = [
|
|||||||
pub struct Tui {
|
pub struct Tui {
|
||||||
msg_s: Option<mpsc::Sender<Message>>,
|
msg_s: Option<mpsc::Sender<Message>>,
|
||||||
background: Option<std::thread::JoinHandle<()>>,
|
background: Option<std::thread::JoinHandle<()>>,
|
||||||
|
running: Arc<AtomicBool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Tui {
|
impl Tui {
|
||||||
@ -67,13 +68,12 @@ impl Tui {
|
|||||||
Self {
|
Self {
|
||||||
msg_s: Some(msg_s),
|
msg_s: Some(msg_s),
|
||||||
background: None,
|
background: None,
|
||||||
|
running: Arc::new(AtomicBool::new(true)),
|
||||||
},
|
},
|
||||||
msg_r,
|
msg_r,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn inner() {}
|
|
||||||
|
|
||||||
fn handle_events(input: &mut String, msg_s: &mut mpsc::Sender<Message>) {
|
fn handle_events(input: &mut String, msg_s: &mut mpsc::Sender<Message>) {
|
||||||
use crossterm::event::*;
|
use crossterm::event::*;
|
||||||
if let Event::Key(event) = read().unwrap() {
|
if let Event::Key(event) = read().unwrap() {
|
||||||
@ -142,10 +142,12 @@ impl Tui {
|
|||||||
|
|
||||||
let hook = std::panic::take_hook();
|
let hook = std::panic::take_hook();
|
||||||
std::panic::set_hook(Box::new(move |info| {
|
std::panic::set_hook(Box::new(move |info| {
|
||||||
|
Self::shutdown();
|
||||||
hook(info);
|
hook(info);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
let mut msg_s = self.msg_s.take().unwrap();
|
let mut msg_s = self.msg_s.take().unwrap();
|
||||||
|
let running = self.running.clone();
|
||||||
|
|
||||||
self.background = Some(std::thread::spawn(move || {
|
self.background = Some(std::thread::spawn(move || {
|
||||||
// Start the tui
|
// Start the tui
|
||||||
@ -155,10 +157,19 @@ impl Tui {
|
|||||||
|
|
||||||
let mut input = String::new();
|
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 {
|
while running.load(Ordering::Relaxed) {
|
||||||
let _ = terminal.draw(|f| {
|
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 (log_rect, input_rect) = if f.size().height > 6 {
|
||||||
let mut log_rect = f.size();
|
let mut log_rect = f.size();
|
||||||
log_rect.height -= 3;
|
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;
|
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())
|
let logger = Paragraph::new(LOG.inner.lock().unwrap().clone())
|
||||||
.block(block)
|
.block(block)
|
||||||
@ -198,23 +209,32 @@ impl Tui {
|
|||||||
f.render_widget(input_field, input_rect);
|
f.render_widget(input_field, input_rect);
|
||||||
|
|
||||||
f.set_cursor(x, size.y);
|
f.set_cursor(x, size.y);
|
||||||
|
}) {
|
||||||
use crossterm::event::*;
|
warn!(?e, "couldn't draw frame");
|
||||||
|
};
|
||||||
if poll(Duration::from_millis(10)).unwrap() {
|
if crossterm::event::poll(Duration::from_millis(100)).unwrap() {
|
||||||
Self::handle_events(&mut input, &mut msg_s);
|
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 shutdown() {
|
||||||
fn drop(&mut self) {
|
|
||||||
let mut stdout = io::stdout();
|
let mut stdout = io::stdout();
|
||||||
|
|
||||||
disable_raw_mode().unwrap();
|
disable_raw_mode().unwrap();
|
||||||
execute!(stdout, LeaveAlternateScreen, DisableMouseCapture).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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user