Skip to content

Commit

Permalink
add prettier CommandTree formatting for help command and usage hint
Browse files Browse the repository at this point in the history
  • Loading branch information
user622628252416 committed Aug 21, 2024
1 parent 59a4fe5 commit 5041b44
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 81 deletions.
4 changes: 2 additions & 2 deletions pumpkin/src/commands/cmd_gamemode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::commands::CommandSender;
use crate::commands::CommandSender::Player;
use crate::entity::player::GameMode;

pub(crate) const NAME: &str = "gamemode";
const NAMES: [&str; 1] = ["gamemode"];

const DESCRIPTION: &str = "Change a player's gamemode.";

Expand Down Expand Up @@ -57,7 +57,7 @@ pub fn parse_arg_gamemode(consumed_args: &ConsumedArgs) -> Result<GameMode, Inva
}

pub(crate) fn init_command_tree<'a>() -> CommandTree<'a> {
CommandTree::new(DESCRIPTION).with_child(
CommandTree::new(NAMES, DESCRIPTION).with_child(
require(&|sender| sender.permission_lvl() >= 2).with_child(
argument(ARG_GAMEMODE, consume_arg_gamemode)
.with_child(
Expand Down
24 changes: 12 additions & 12 deletions pumpkin/src/commands/cmd_help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use crate::commands::tree_builder::argument;
use crate::commands::{dispatcher_init, CommandSender, DISPATCHER};
use pumpkin_text::TextComponent;

pub(crate) const NAME: &str = "help";
pub(crate) const ALIAS: &str = "?";
const NAMES: [&str; 3] = ["help", "h", "?"];

const DESCRIPTION: &str = "Print a help message.";

Expand Down Expand Up @@ -40,18 +39,16 @@ fn parse_arg_command<'a>(
}

pub(crate) fn init_command_tree<'a>() -> CommandTree<'a> {
CommandTree::new(DESCRIPTION)
CommandTree::new(NAMES, DESCRIPTION)
.with_child(
argument(ARG_COMMAND, consume_arg_command).execute(&|sender, args| {
let dispatcher = DISPATCHER.get_or_init(dispatcher_init);

let (name, tree) = parse_arg_command(args, dispatcher)?;

sender.send_message(TextComponent::text(&format!(
"{} - {} Usage:{}",
name,
tree.description,
tree.paths_formatted(name)
"{} - {} Usage: {}",
name, tree.description, tree
)));

Ok(())
Expand All @@ -60,12 +57,15 @@ pub(crate) fn init_command_tree<'a>() -> CommandTree<'a> {
.execute(&|sender, _args| {
let dispatcher = DISPATCHER.get_or_init(dispatcher_init);

for (name, tree) in &dispatcher.commands {
let mut names: Vec<&str> = dispatcher.commands.keys().copied().collect();
names.sort();

for name in names {
let tree = &dispatcher.commands[name];

sender.send_message(TextComponent::text(&format!(
"{} - {} Usage:{}",
name,
tree.description,
tree.paths_formatted(name)
"{} - {} Usage: {}",
name, tree.description, tree
)));
}

Expand Down
4 changes: 2 additions & 2 deletions pumpkin/src/commands/cmd_pumpkin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use pumpkin_text::{color::NamedColor, TextComponent};

use crate::commands::tree::CommandTree;

pub(crate) const NAME: &str = "pumpkin";
const NAMES: [&str; 1] = ["pumpkin"];

const DESCRIPTION: &str = "Display information about Pumpkin.";

pub(crate) fn init_command_tree<'a>() -> CommandTree<'a> {
CommandTree::new(DESCRIPTION).execute(&|sender, _| {
CommandTree::new(NAMES, DESCRIPTION).execute(&|sender, _| {
let version = env!("CARGO_PKG_VERSION");
let description = env!("CARGO_PKG_DESCRIPTION");

Expand Down
4 changes: 2 additions & 2 deletions pumpkin/src/commands/cmd_stop.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::commands::tree::CommandTree;
use crate::commands::tree_builder::require;

pub(crate) const NAME: &str = "stop";
const NAMES: [&str; 1] = ["stop"];

const DESCRIPTION: &str = "Stop the server.";

pub(crate) fn init_command_tree<'a>() -> CommandTree<'a> {
CommandTree::new(DESCRIPTION).with_child(
CommandTree::new(NAMES, DESCRIPTION).with_child(
require(&|sender| sender.permission_lvl() >= 4)
.execute(&|_sender, _args| std::process::exit(0)),
)
Expand Down
18 changes: 14 additions & 4 deletions pumpkin/src/commands/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@ impl<'a> CommandDispatcher<'a> {
}
}

Err(format!(
"Invalid Syntax. Usage:{}",
tree.paths_formatted(key)
))
Err(format!("Invalid Syntax. Usage: {}", tree))
}

fn try_is_fitting_path(
Expand Down Expand Up @@ -99,4 +96,17 @@ impl<'a> CommandDispatcher<'a> {

Ok(false)
}

/// Register a command with the dispatcher.
pub(crate) fn register(&mut self, tree: CommandTree<'a>) {
let mut names = tree.names.iter();

let primary_name = names.next().expect("at least one name must be provided");

for &name in names {
self.commands.insert(name, tree.clone());
}

self.commands.insert(primary_name, tree);
}
}
16 changes: 9 additions & 7 deletions pumpkin/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod cmd_stop;
mod dispatcher;
mod tree;
mod tree_builder;
mod tree_format;

pub enum CommandSender<'a> {
Rcon(&'a mut Vec<String>),
Expand Down Expand Up @@ -70,15 +71,16 @@ static DISPATCHER: OnceLock<CommandDispatcher> = OnceLock::new();

/// create [CommandDispatcher] instance for [DISPATCHER]
fn dispatcher_init<'a>() -> CommandDispatcher<'a> {
let mut map = HashMap::new();
let mut dispatcher = CommandDispatcher {
commands: HashMap::new(),
};

map.insert(cmd_pumpkin::NAME, cmd_pumpkin::init_command_tree());
map.insert(cmd_gamemode::NAME, cmd_gamemode::init_command_tree());
map.insert(cmd_stop::NAME, cmd_stop::init_command_tree());
map.insert(cmd_help::NAME, cmd_help::init_command_tree());
map.insert(cmd_help::ALIAS, cmd_help::init_command_tree());
dispatcher.register(cmd_pumpkin::init_command_tree());
dispatcher.register(cmd_gamemode::init_command_tree());
dispatcher.register(cmd_stop::init_command_tree());
dispatcher.register(cmd_help::init_command_tree());

CommandDispatcher { commands: map }
dispatcher
}

pub fn handle_command(sender: &mut CommandSender, cmd: &str) {
Expand Down
56 changes: 5 additions & 51 deletions pumpkin/src/commands/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::collections::{HashMap, VecDeque};

use crate::commands::dispatcher::InvalidTreeError;
use crate::commands::CommandSender;

/// see [crate::commands::tree_builder::argument]
pub(crate) type RawArgs<'a> = Vec<&'a str>;

Expand All @@ -11,11 +12,13 @@ pub(crate) type ConsumedArgs<'a> = HashMap<&'a str, String>;
/// see [crate::commands::tree_builder::argument]
pub(crate) type ArgumentConsumer<'a> = fn(&CommandSender, &mut RawArgs) -> Option<String>;

#[derive(Clone)]
pub(crate) struct Node<'a> {
pub(crate) children: Vec<usize>,
pub(crate) node_type: NodeType<'a>,
}

#[derive(Clone)]
pub(crate) enum NodeType<'a> {
ExecuteLeaf {
run: &'a (dyn Fn(&mut CommandSender, &ConsumedArgs) -> Result<(), InvalidTreeError> + Sync),
Expand All @@ -31,9 +34,11 @@ pub(crate) enum NodeType<'a> {
},
}

#[derive(Clone)]
pub(crate) struct CommandTree<'a> {
pub(crate) nodes: Vec<Node<'a>>,
pub(crate) children: Vec<usize>,
pub(crate) names: Vec<&'a str>,
pub(crate) description: &'a str,
}

Expand All @@ -51,57 +56,6 @@ impl<'a> CommandTree<'a> {
todo,
}
}

/// format possible paths as [String], using ```name``` as the command name
///
/// todo: merge into single line
pub(crate) fn paths_formatted(&'a self, name: &str) -> String {
let paths: Vec<Vec<&NodeType>> = self
.iter_paths()
.map(|path| path.iter().map(|&i| &self.nodes[i].node_type).collect())
.collect();

let len = paths
.iter()
.map(|path| {
path.iter()
.map(|node| match node {
NodeType::ExecuteLeaf { .. } => 0,
NodeType::Literal { string } => string.len() + 1,
NodeType::Argument { name, .. } => name.len() + 3,
NodeType::Require { .. } => 0,
})
.sum::<usize>()
+ name.len()
+ 2
})
.sum::<usize>();

let mut s = String::with_capacity(len);

for path in paths.iter() {
s.push(if paths.len() > 1 { '\n' } else { ' ' });
s.push('/');
s.push_str(name);
for node in path {
match node {
NodeType::Literal { string } => {
s.push(' ');
s.push_str(string);
}
NodeType::Argument { name, .. } => {
s.push(' ');
s.push('<');
s.push_str(name);
s.push('>');
}
_ => {}
}
}
}

s
}
}

struct TraverseAllPathsIter<'a> {
Expand Down
15 changes: 14 additions & 1 deletion pumpkin/src/commands/tree_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,23 @@ impl<'a> CommandTree<'a> {
self
}

pub fn new(description: &'a str) -> Self {
/// provide at least one name
pub fn new<const NAME_COUNT: usize>(
names: [&'a str; NAME_COUNT],
description: &'a str,
) -> Self {
assert!(NAME_COUNT > 0);

let mut names_vec = Vec::with_capacity(NAME_COUNT);

for name in names {
names_vec.push(name);
}

Self {
nodes: Vec::new(),
children: Vec::new(),
names: names_vec,
description,
}
}
Expand Down
Loading

0 comments on commit 5041b44

Please sign in to comment.