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