Added flags to toggle the tui

This commit is contained in:
Capucho 2020-07-28 14:47:01 +01:00 committed by Marcel Märtens
parent bf8e455839
commit 332cb20df1
3 changed files with 62 additions and 14 deletions

1
Cargo.lock generated
View File

@ -4911,6 +4911,7 @@ name = "veloren-server-cli"
version = "0.7.0" version = "0.7.0"
dependencies = [ dependencies = [
"ansi-parser", "ansi-parser",
"clap",
"crossterm", "crossterm",
"lazy_static", "lazy_static",
"tracing", "tracing",

View File

@ -18,3 +18,4 @@ crossterm = "0.17"
tui = { version = "0.10", default-features = false, features = ['crossterm'] } 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"

View File

@ -7,6 +7,7 @@ use server::{Event, Input, Server, ServerSettings};
use tracing::{error, info, warn, Level}; use tracing::{error, info, warn, Level};
use tracing_subscriber::{filter::LevelFilter, EnvFilter, FmtSubscriber}; use tracing_subscriber::{filter::LevelFilter, EnvFilter, FmtSubscriber};
use clap::{App, Arg};
use crossterm::{ use crossterm::{
event::{DisableMouseCapture, EnableMouseCapture}, event::{DisableMouseCapture, EnableMouseCapture},
execute, execute,
@ -28,16 +29,23 @@ use tui::{
const TPS: u64 = 30; const TPS: u64 = 30;
const RUST_LOG_ENV: &str = "RUST_LOG"; const RUST_LOG_ENV: &str = "RUST_LOG";
#[derive(Debug, Clone)]
enum Message {
Quit,
}
const COMMANDS: [Command; 2] = [ const COMMANDS: [Command; 2] = [
Command { Command {
name: "quit", name: "quit",
description: "Closes the server", description: "Closes the server",
split_spaces: true,
args: 0, args: 0,
cmd: |_, sender| sender.send(Message::Quit).unwrap(), cmd: |_, sender| sender.send(Message::Quit).unwrap(),
}, },
Command { Command {
name: "help", name: "help",
description: "List all command available", description: "List all command available",
split_spaces: true,
args: 0, args: 0,
cmd: |_, _| { cmd: |_, _| {
info!("===== Help ====="); info!("===== Help =====");
@ -52,8 +60,10 @@ const COMMANDS: [Command; 2] = [
struct Command<'a> { struct Command<'a> {
pub name: &'a str, pub name: &'a str,
pub description: &'a str, pub description: &'a str,
// Whether or not the command splits the arguments on whitespace
pub split_spaces: bool,
pub args: usize, pub args: usize,
pub cmd: fn(Vec<&str>, &mut mpsc::Sender<Message>), pub cmd: fn(Vec<String>, &mut mpsc::Sender<Message>),
} }
lazy_static! { lazy_static! {
@ -144,12 +154,29 @@ impl<'a> Write for TuiLog<'a> {
fn flush(&mut self) -> io::Result<()> { Ok(()) } fn flush(&mut self) -> io::Result<()> { Ok(()) }
} }
#[derive(Debug, Copy, Clone)]
enum Message {
Quit,
}
fn main() -> io::Result<()> { fn main() -> io::Result<()> {
let matches = App::new("Veloren server cli")
.version(
format!(
"{}-{}",
env!("CARGO_PKG_VERSION"),
common::util::GIT_HASH.to_string()
)
.as_str(),
)
.author("The veloren devs <https://gitlab.com/veloren/veloren>")
.about("The veloren server cli provides a easy to use interface to start a veloren server")
.arg(
Arg::with_name("basic")
.short("b")
.long("basic")
.help("Disables the tui")
.takes_value(false),
)
.get_matches();
let basic = matches.is_present("basic");
// Init logging // Init logging
let filter = match std::env::var_os(RUST_LOG_ENV).map(|s| s.into_string()) { let filter = match std::env::var_os(RUST_LOG_ENV).map(|s| s.into_string()) {
Some(Ok(env)) => { Some(Ok(env)) => {
@ -170,15 +197,21 @@ fn main() -> io::Result<()> {
.add_directive(LevelFilter::INFO.into()), .add_directive(LevelFilter::INFO.into()),
}; };
FmtSubscriber::builder() let subscriber = FmtSubscriber::builder()
.with_max_level(Level::ERROR) .with_max_level(Level::ERROR)
.with_env_filter(filter) .with_env_filter(filter);
.with_writer(|| LOG.clone())
.init(); if basic {
subscriber.init();
} else {
subscriber.with_writer(|| LOG.clone()).init();
}
let (sender, receiver) = mpsc::channel(); let (sender, receiver) = mpsc::channel();
start_tui(sender); if !basic {
start_tui(sender);
}
info!("Starting server..."); info!("Starting server...");
@ -229,7 +262,9 @@ fn main() -> io::Result<()> {
clock.tick(Duration::from_millis(1000 / TPS)); clock.tick(Duration::from_millis(1000 / TPS));
} }
stop_tui(); if !basic {
stop_tui();
}
Ok(()) Ok(())
} }
@ -303,12 +338,23 @@ fn start_tui(mut sender: mpsc::Sender<Message>) {
{ {
let args = args.collect::<Vec<_>>(); let args = args.collect::<Vec<_>>();
if args.len() > cmd.args { let (arg_len, args) = if cmd.split_spaces {
(
args.len(),
args.into_iter()
.map(|s| s.to_string())
.collect::<Vec<String>>(),
)
} else {
(1, vec![args.into_iter().collect::<String>()])
};
if arg_len > cmd.args {
warn!("{} only takes {} arguments", cmd_name, cmd.args); warn!("{} only takes {} arguments", cmd_name, cmd.args);
let cmd = cmd.cmd; let cmd = cmd.cmd;
cmd(args, &mut sender) cmd(args, &mut sender)
} else if args.len() < cmd.args { } else if arg_len < cmd.args {
error!("{} takes {} arguments", cmd_name, cmd.args); error!("{} takes {} arguments", cmd_name, cmd.args);
} else { } else {
let cmd = cmd.cmd; let cmd = cmd.cmd;