mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
Fix all the problems with the tui
This commit is contained in:
parent
36350a08b1
commit
6e24ff31ee
3
Cargo.lock
generated
3
Cargo.lock
generated
@ -4632,8 +4632,7 @@ checksum = "e604eb7b43c06650e854be16a2a03155743d3752dd1c943f6829e26b7a36e382"
|
||||
[[package]]
|
||||
name = "tui"
|
||||
version = "0.10.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a977b0bb2e2033a6fef950f218f13622c3c34e59754b704ce3492dedab1dfe95"
|
||||
source = "git+https://github.com/fdehau/tui-rs.git?branch=paragraph-scroll#54b841fab6cfdb38e8dc1382176e965787964b4c"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"cassowary",
|
||||
|
@ -15,7 +15,12 @@ common = { package = "veloren-common", path = "../common" }
|
||||
tracing = { version = "0.1", default-features = false }
|
||||
tracing-subscriber = { version = "0.2.3", default-features = false, features = ["env-filter", "fmt", "chrono", "ansi", "smallvec"] }
|
||||
crossterm = "0.17"
|
||||
tui = { version = "0.10", default-features = false, features = ['crossterm'] }
|
||||
lazy_static = "1"
|
||||
ansi-parser = "0.6"
|
||||
clap = "2.33"
|
||||
|
||||
[dependencies.tui]
|
||||
git = "https://github.com/fdehau/tui-rs.git"
|
||||
branch="paragraph-scroll"
|
||||
default-features = false
|
||||
features = ['crossterm']
|
@ -131,8 +131,5 @@ fn main() -> io::Result<()> {
|
||||
clock.tick(Duration::from_millis(1000 / TPS));
|
||||
}
|
||||
|
||||
drop(tui);
|
||||
std::thread::sleep(Duration::from_millis(10));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -6,7 +6,10 @@ use crossterm::{
|
||||
};
|
||||
use std::{
|
||||
io::{self, Write},
|
||||
sync::{Arc, mpsc, atomic::{AtomicBool, Ordering}},
|
||||
sync::{
|
||||
atomic::{AtomicBool, Ordering},
|
||||
mpsc, Arc,
|
||||
},
|
||||
time::Duration,
|
||||
};
|
||||
use tracing::{error, info, warn};
|
||||
@ -136,10 +139,11 @@ impl Tui {
|
||||
}
|
||||
|
||||
pub fn run(&mut self) {
|
||||
enable_raw_mode().unwrap();
|
||||
let mut stdout = io::stdout();
|
||||
execute!(stdout, EnterAlternateScreen, EnableMouseCapture).unwrap();
|
||||
|
||||
enable_raw_mode().unwrap();
|
||||
|
||||
let hook = std::panic::take_hook();
|
||||
std::panic::set_hook(Box::new(move |info| {
|
||||
Self::shutdown();
|
||||
@ -160,15 +164,8 @@ impl Tui {
|
||||
if let Err(e) = terminal.clear() {
|
||||
error!(?e, "clouldn't clean terminal");
|
||||
};
|
||||
let mut i: u64 = 0;
|
||||
|
||||
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();
|
||||
@ -184,18 +181,17 @@ impl Tui {
|
||||
};
|
||||
|
||||
let block = Block::default().borders(Borders::ALL);
|
||||
let size = block.inner(log_rect);
|
||||
|
||||
LOG.resize(size.height as usize);
|
||||
|
||||
let scroll = (LOG.height(size) as i16 - size.height as i16).max(0) as u16;
|
||||
|
||||
//trace!(?i, "{} {} {}", LOG.height(size) as i16, size.width, size.height);
|
||||
let mut wrap = Wrap::default();
|
||||
wrap.scroll_callback = Some(Box::new(|text_area, lines| {
|
||||
LOG.resize(text_area.height as usize);
|
||||
let len = lines.len() as u16;
|
||||
(len.saturating_sub(text_area.height), 0)
|
||||
}));
|
||||
|
||||
let logger = Paragraph::new(LOG.inner.lock().unwrap().clone())
|
||||
.block(block)
|
||||
.wrap(Wrap { trim: false })
|
||||
.scroll((scroll, 0));
|
||||
.wrap(wrap);
|
||||
f.render_widget(logger, log_rect);
|
||||
|
||||
let text: Text = input.as_str().into();
|
||||
@ -212,22 +208,18 @@ impl Tui {
|
||||
}) {
|
||||
warn!(?e, "couldn't draw frame");
|
||||
};
|
||||
if crossterm::event::poll(Duration::from_millis(100)).unwrap() {
|
||||
if crossterm::event::poll(Duration::from_millis(10)).unwrap() {
|
||||
Self::handle_events(&mut input, &mut msg_s);
|
||||
};
|
||||
}
|
||||
|
||||
if let Err(e) = terminal.clear() {
|
||||
error!(?e, "clouldn't clean terminal");
|
||||
};
|
||||
}));
|
||||
}
|
||||
|
||||
fn shutdown() {
|
||||
let mut stdout = io::stdout();
|
||||
|
||||
disable_raw_mode().unwrap();
|
||||
execute!(stdout, LeaveAlternateScreen, DisableMouseCapture).unwrap();
|
||||
disable_raw_mode().unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2,7 +2,8 @@ use std::{
|
||||
io::{self, Write},
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
use tui::{layout::Rect, text::Text};
|
||||
use tracing::warn;
|
||||
use tui::text::Text;
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct TuiLog<'a> {
|
||||
@ -13,33 +14,7 @@ impl<'a> TuiLog<'a> {
|
||||
pub fn resize(&self, h: usize) {
|
||||
let mut inner = self.inner.lock().unwrap();
|
||||
|
||||
if inner.height() > h {
|
||||
let length = inner.height() - h;
|
||||
inner.lines.drain(0..length);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn height(&self, rect: Rect) -> u16 {
|
||||
// TODO: There's probably a better solution
|
||||
let inner = self.inner.lock().unwrap();
|
||||
let mut h = 0;
|
||||
|
||||
for line in inner.lines.iter() {
|
||||
let mut w = 0;
|
||||
|
||||
for word in line.0.iter() {
|
||||
if word.width() + w > rect.width as usize {
|
||||
h += (word.width() / rect.width as usize).min(1);
|
||||
w = word.width() % rect.width as usize;
|
||||
} else {
|
||||
w += word.width();
|
||||
}
|
||||
}
|
||||
|
||||
h += 1;
|
||||
}
|
||||
|
||||
h as u16
|
||||
inner.lines.truncate(h);
|
||||
}
|
||||
}
|
||||
|
||||
@ -86,14 +61,15 @@ impl<'a> Write for TuiLog<'a> {
|
||||
|
||||
match iter.next().unwrap() {
|
||||
0 => {},
|
||||
1 => span.style.add_modifier = Modifier::BOLD,
|
||||
2 => span.style.add_modifier = Modifier::DIM,
|
||||
idx @ 30..=37 => {
|
||||
span.style.fg = Some(COLOR_TABLE[(idx - 30) as usize])
|
||||
},
|
||||
_ => println!("{:#?}", values),
|
||||
_ => warn!("Unknown color {:#?}", values),
|
||||
}
|
||||
},
|
||||
_ => println!("{:#?}", seq),
|
||||
_ => warn!("Unknown sequence {:#?}", seq),
|
||||
}
|
||||
},
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user