diff --git a/Cargo.lock b/Cargo.lock index 818e711135..fdef68c4cb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -93,6 +93,12 @@ dependencies = [ "num-traits", ] +[[package]] +name = "arc-swap" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d25d88fd6b8041580a654f9d0c581a047baee2b3efee13275f2fc392fc75034" + [[package]] name = "arr_macro" version = "0.1.3" @@ -170,7 +176,7 @@ dependencies = [ "kv-log-macro", "log", "memchr", - "mio", + "mio 0.6.22", "mio-uds", "num_cpus", "once_cell", @@ -440,11 +446,17 @@ version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7aa2097be53a00de9e8fc349fea6d76221f398f5c4fa550d420669906962d160" dependencies = [ - "mio", + "mio 0.6.22", "mio-extras", "nix 0.14.1", ] +[[package]] +name = "cassowary" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df8670b8c7b9dae1793364eafadf7239c40d669904660c5960d74cfd80b46a53" + [[package]] name = "cast" version = "0.2.3" @@ -950,6 +962,31 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "crossterm" +version = "0.17.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f4919d60f26ae233e14233cc39746c8c8bb8cd7b05840ace83604917b51b6c7" +dependencies = [ + "bitflags", + "crossterm_winapi", + "lazy_static", + "libc", + "mio 0.7.0", + "parking_lot 0.10.2", + "signal-hook", + "winapi 0.3.8", +] + +[[package]] +name = "crossterm_winapi" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "057b7146d02fb50175fd7dbe5158f6097f33d02831f43b4ee8ae4ddf67b68f5c" +dependencies = [ + "winapi 0.3.8", +] + [[package]] name = "csv" version = "1.1.3" @@ -2527,12 +2564,26 @@ dependencies = [ "kernel32-sys", "libc", "log", - "miow", + "miow 0.2.1", "net2", "slab", "winapi 0.2.8", ] +[[package]] +name = "mio" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e9971bc8349a361217a8f2a41f5d011274686bd4436465ba51730921039d7fb" +dependencies = [ + "lazy_static", + "libc", + "log", + "miow 0.3.5", + "ntapi", + "winapi 0.3.8", +] + [[package]] name = "mio-extras" version = "2.0.6" @@ -2541,7 +2592,7 @@ checksum = "52403fe290012ce777c4626790c8951324a2b9e3316b3143779c72b029742f19" dependencies = [ "lazycell", "log", - "mio", + "mio 0.6.22", "slab", ] @@ -2553,7 +2604,7 @@ checksum = "afcb699eb26d4332647cc848492bbc15eafb26f08d0304550d5aa1f612e066f0" dependencies = [ "iovec", "libc", - "mio", + "mio 0.6.22", ] [[package]] @@ -2568,6 +2619,16 @@ dependencies = [ "ws2_32-sys", ] +[[package]] +name = "miow" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07b88fb9795d4d36d62a012dfbf49a8f5cf12751f36d31a9dbe66d528e58979e" +dependencies = [ + "socket2", + "winapi 0.3.8", +] + [[package]] name = "mopa" version = "0.2.2" @@ -2702,12 +2763,21 @@ dependencies = [ "fsevent-sys", "inotify", "libc", - "mio", + "mio 0.6.22", "mio-extras", "walkdir", "winapi 0.3.8", ] +[[package]] +name = "ntapi" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a31937dea023539c72ddae0e3571deadc1414b300483fa7aaec176168cfa9d2" +dependencies = [ + "winapi 0.3.8", +] + [[package]] name = "num" version = "0.1.42" @@ -3909,6 +3979,27 @@ version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5752e017e03af9d735b4b069f53b7a7fd90fefafa04d8bd0c25581b0bff437f" +[[package]] +name = "signal-hook" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "604508c1418b99dfe1925ca9224829bb2a8a9a04dda655cc01fcad46f4ab05ed" +dependencies = [ + "libc", + "mio 0.7.0", + "signal-hook-registry", +] + +[[package]] +name = "signal-hook-registry" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94f478ede9f64724c5d173d7bb56099ec3e2d9fc2774aac65d34b8b890405f41" +dependencies = [ + "arc-swap", + "libc", +] + [[package]] name = "slab" version = "0.4.2" @@ -3956,6 +4047,18 @@ dependencies = [ "smithay-client-toolkit", ] +[[package]] +name = "socket2" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03088793f677dce356f3ccc2edb1b314ad191ab702a5de3faf49304f7e104918" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "winapi 0.3.8", +] + [[package]] name = "specs" version = "0.16.1" @@ -4266,7 +4369,7 @@ checksum = "5a09c0b5bb588872ab2f09afa13ee6e9dac11e10a0ec9e8e3ba39a5a5d530af6" dependencies = [ "bytes", "futures 0.1.29", - "mio", + "mio 0.6.22", "num_cpus", "tokio-current-thread", "tokio-executor", @@ -4328,7 +4431,7 @@ dependencies = [ "futures 0.1.29", "lazy_static", "log", - "mio", + "mio 0.6.22", "num_cpus", "parking_lot 0.9.0", "slab", @@ -4356,7 +4459,7 @@ dependencies = [ "bytes", "futures 0.1.29", "iovec", - "mio", + "mio 0.6.22", "tokio-io", "tokio-reactor", ] @@ -4517,6 +4620,19 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e604eb7b43c06650e854be16a2a03155743d3752dd1c943f6829e26b7a36e382" +[[package]] +name = "tui" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a977b0bb2e2033a6fef950f218f13622c3c34e59754b704ce3492dedab1dfe95" +dependencies = [ + "bitflags", + "cassowary", + "crossterm", + "unicode-segmentation", + "unicode-width", +] + [[package]] name = "tuple_utils" version = "0.3.0" @@ -4785,8 +4901,11 @@ dependencies = [ name = "veloren-server-cli" version = "0.7.0" dependencies = [ + "crossterm", + "lazy_static", "tracing", "tracing-subscriber", + "tui", "veloren-common", "veloren-server", ] @@ -5025,7 +5144,7 @@ dependencies = [ "calloop", "downcast-rs", "libc", - "mio", + "mio 0.6.22", "nix 0.14.1", "wayland-commons", "wayland-scanner", @@ -5162,7 +5281,7 @@ dependencies = [ "lazy_static", "libc", "log", - "mio", + "mio 0.6.22", "mio-extras", "ndk", "ndk-glue", diff --git a/server-cli/Cargo.toml b/server-cli/Cargo.toml index 93ab4b095b..207caf9dfb 100644 --- a/server-cli/Cargo.toml +++ b/server-cli/Cargo.toml @@ -14,3 +14,6 @@ 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" diff --git a/server-cli/src/main.rs b/server-cli/src/main.rs index 3668f8c97a..d5769a1307 100644 --- a/server-cli/src/main.rs +++ b/server-cli/src/main.rs @@ -1,15 +1,33 @@ #![deny(unsafe_code)] +#[macro_use] extern crate lazy_static; + use common::clock::Clock; use server::{Event, Input, Server, ServerSettings}; -use std::time::Duration; -use tracing::{info, Level}; +use tracing::{error, info, Level}; use tracing_subscriber::{filter::LevelFilter, EnvFilter, FmtSubscriber}; +use std::{ + io::{self, Write}, + sync::{mpsc, Arc, Mutex}, + time::Duration, +}; +use tui::{ + backend::CrosstermBackend, + layout::{Constraint, Direction, Layout}, + text::Text, + widgets::{Block, Borders, Paragraph, Wrap}, + Terminal, +}; + const TPS: u64 = 30; const RUST_LOG_ENV: &str = "RUST_LOG"; -fn main() { +lazy_static! { + static ref LOG: TuiLog<'static> = TuiLog::default(); +} + +fn main() -> io::Result<()> { // Init logging let filter = match std::env::var_os(RUST_LOG_ENV).map(|s| s.into_string()) { Some(Ok(env)) => { @@ -33,8 +51,87 @@ fn main() { FmtSubscriber::builder() .with_max_level(Level::ERROR) .with_env_filter(filter) + .with_writer(|| LOG.clone()) .init(); + let (sender, receiver) = mpsc::channel(); + + std::thread::spawn(move || { + // Start the tui + let stdout = io::stdout(); + let backend = CrosstermBackend::new(stdout); + let mut terminal = Terminal::new(backend).unwrap(); + + crossterm::terminal::enable_raw_mode().unwrap(); + + let mut input = String::new(); + + let _ = terminal.clear(); + + loop { + let _ = terminal.draw(|f| { + let chunks = Layout::default() + .direction(Direction::Vertical) + .constraints([Constraint::Max(u16::MAX), Constraint::Max(3)].as_ref()) + .split(f.size()); + + let block = Block::default().borders(Borders::ALL); + let size = block.inner(chunks[0]); + + LOG.resize(size.height as usize); + + let logger = Paragraph::new(LOG.inner.lock().unwrap().clone()) + .block(block) + .wrap(Wrap { trim: false }); + f.render_widget(logger, chunks[0]); + + let text: Text = input.as_str().into(); + + let block = Block::default().borders(Borders::ALL); + let size = block.inner(chunks[1]); + + let x = (size.x + text.width() as u16).min(size.width); + + let input_field = Paragraph::new(text).block(block); + f.render_widget(input_field, chunks[1]); + + f.set_cursor(x, size.y); + + use crossterm::event::{KeyModifiers, *}; + + if poll(Duration::from_millis(50)).unwrap() { + // It's guaranteed that the `read()` won't block when the `poll()` + // function returns `true` + match read().unwrap() { + Event::Key(event) => match event.code { + KeyCode::Char('c') => { + if event.modifiers.contains(KeyModifiers::CONTROL) { + sender.send(Message::Quit).unwrap() + } else { + input.push('c'); + } + }, + KeyCode::Char(c) => input.push(c), + KeyCode::Backspace => { + input.pop(); + }, + KeyCode::Enter => { + match input.as_str() { + "quit" => sender.send(Message::Quit).unwrap(), + _ => error!("invalid command"), + } + + input = String::new(); + }, + _ => {}, + }, + _ => {}, + } + } + }); + } + }); + info!("Starting server..."); // Set up an fps clock @@ -44,7 +141,6 @@ fn main() { let settings = ServerSettings::load(); let server_port = &settings.gameserver_address.port(); let metrics_port = &settings.metrics_address.port(); - // Create server let mut server = Server::new(settings).expect("Failed to create server instance!"); @@ -68,7 +164,56 @@ fn main() { // Clean up the server after a tick. server.cleanup(); + match receiver.try_recv() { + Ok(msg) => match msg { + Message::Quit => { + info!("Closing the server"); + break; + }, + }, + Err(e) => match e { + mpsc::TryRecvError::Empty => {}, + mpsc::TryRecvError::Disconnected => panic!(), + }, + }; + // Wait for the next tick. clock.tick(Duration::from_millis(1000 / TPS)); } + + Ok(()) +} + +#[derive(Debug, Default, Clone)] +struct TuiLog<'a> { + inner: Arc>>, +} + +impl<'a> TuiLog<'a> { + 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); + } + } +} + +impl<'a> Write for TuiLog<'a> { + fn write(&mut self, buf: &[u8]) -> io::Result { + let line = String::from_utf8(buf.into()) + .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; + + self.inner.lock().unwrap().lines.push(From::from(line)); + + Ok(buf.len()) + } + + fn flush(&mut self) -> io::Result<()> { Ok(()) } +} + +#[derive(Debug, Copy, Clone)] +enum Message { + Quit, }