Skip to content

Commit d01f43e

Browse files
committed
core: make usage hints more colorful
1 parent eef2598 commit d01f43e

File tree

5 files changed

+52
-29
lines changed

5 files changed

+52
-29
lines changed

crates/core/src/commands/builtin/core.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::plot::Plot;
66
use crate::server::Message;
77
use crate::worldedit::ray_trace_block;
88
use crate::{
9-
player::{Gamemode, PacketSender, PlayerPos},
9+
player::{Gamemode, PlayerPos},
1010
plot::database,
1111
};
1212
use mchprs_blocks::items::ItemStack;
@@ -417,15 +417,17 @@ pub(super) fn register_commands(registry: &mut CommandRegistry) {
417417
let msg =
418418
"Redpiler optimization is highly unstable and can break builds. Use with caution!";
419419
tracing::warn!("{}", msg);
420-
ctx.player()?.send_system_message(msg);
420+
ctx.reply(msg)?;
421421
}
422422

423423
ctx.plot.reset_redpiler();
424424
let start_time = Instant::now();
425425
ctx.plot.start_redpiler(options);
426426
let duration = start_time.elapsed();
427-
debug!("Compile took {:?}", duration);
428-
ctx.reply(&format!("Compilation completed in {:?}", duration))
427+
let msg = format!("Compilation completed in {:?}", duration);
428+
debug!(msg);
429+
ctx.reply(&msg)?;
430+
Ok(())
429431
}
430432

431433
let redpiler_flag_arg = ArgumentType::flags()

crates/core/src/commands/builtin/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,20 @@ fn register_help(registry: &mut CommandRegistry) {
5050
| parser::ParseResult::TooManyArguments { path, .. }
5151
| parser::ParseResult::InvalidArgument { path, .. } => {
5252
let usage = usage::generate_usage(&path);
53-
ctx.reply(&format!("Usage: {}", usage))?;
53+
ctx.reply_legacy(&format!("&6Usage: &e{}", usage))?;
5454
let flag_details = usage::generate_flag_details(path.last().unwrap());
5555
if !flag_details.is_empty() {
56-
ctx.reply("Available flags:")?;
56+
ctx.reply_legacy("&6Available flags:")?;
5757
for flag in flag_details {
5858
let flag_part = match flag.short {
59-
Some(short) => format!("-{} | --{}", short, flag.long),
60-
None => format!("--{}", flag.long),
59+
Some(short) => format!("&a-{} &e| &a--{}", short, flag.long),
60+
None => format!("&a--{}", flag.long),
6161
};
6262
let flag_details = match &flag.description {
63-
Some(desc) => format!(" {} ({})", flag_part, desc),
63+
Some(desc) => format!(" {}&e: {}", flag_part, desc),
6464
None => format!(" {}", flag_part),
6565
};
66-
ctx.reply(&flag_details)?;
66+
ctx.reply_legacy(&flag_details)?;
6767
}
6868
}
6969
}

crates/core/src/commands/context.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::player::{PacketSender, Player};
44
use crate::plot::{Plot, PlotWorld};
55
use crate::worldedit::{create_clipboard, WorldEditClipboard, WorldEditUndo};
66
use mchprs_blocks::BlockPos;
7+
use mchprs_text::TextComponent;
78

89
pub struct ExecutionContext<'a> {
910
pub plot: &'a mut Plot,
@@ -48,6 +49,19 @@ impl<'a> ExecutionContext<'a> {
4849
Ok(())
4950
}
5051

52+
pub fn reply_legacy(&self, message: &str) -> CommandResult<()> {
53+
match &self.sender {
54+
CommandSender::Player(_) => {
55+
let components = TextComponent::from_legacy_text(message);
56+
self.player()?.send_chat_message(&components);
57+
}
58+
CommandSender::Console => {
59+
println!("{}", message);
60+
}
61+
}
62+
Ok(())
63+
}
64+
5165
pub fn has_permission(&self, permission: &str) -> bool {
5266
match &self.sender {
5367
CommandSender::Player(_) => {

crates/core/src/commands/error.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,18 @@ impl CommandError {
6363
}
6464

6565
pub type CommandResult<T> = Result<T, CommandError>;
66+
67+
pub(crate) trait UnwrapRuntimeError<T> {
68+
fn unwrap_runtime(self) -> Result<T, InternalError>;
69+
}
70+
71+
impl<T> UnwrapRuntimeError<T> for CommandResult<T> {
72+
fn unwrap_runtime(self) -> Result<T, InternalError> {
73+
self.map_err(|err| match err {
74+
CommandError::Runtime(err) => InternalError::Message {
75+
message: format!("Runtime Error: {}", err),
76+
},
77+
CommandError::Internal(err) => err,
78+
})
79+
}
80+
}

crates/core/src/commands/executor.rs

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::{
22
commands::{
33
argument_set::ArgumentSet,
44
context::ExecutionContext,
5-
error::{CommandError, CommandResult, InternalError},
5+
error::{CommandError, CommandResult, InternalError, UnwrapRuntimeError},
66
node::CommandNode,
77
parser::{self, ParseResult},
88
registry::CommandRegistry,
@@ -36,7 +36,7 @@ impl CommandRegistry {
3636
match Self::execute_impl(&mut ctx, node, &path) {
3737
Ok(()) => Ok(()),
3838
Err(CommandError::Runtime(err)) => {
39-
Self::send_error(&ctx, &err.to_string())?;
39+
ctx.error(&err.to_string()).unwrap_runtime()?;
4040
Ok(())
4141
}
4242
Err(CommandError::Internal(err)) => Err(err),
@@ -62,7 +62,7 @@ impl CommandRegistry {
6262

6363
ParseResult::NothingMatched { .. } => {
6464
let ctx = ExecutionContext::new(plot, sender, ArgumentSet::empty());
65-
Self::send_error(&ctx, "Command not found!")?;
65+
ctx.error("Command not found!").unwrap_runtime()?;
6666
Ok(())
6767
}
6868
}
@@ -116,26 +116,18 @@ impl CommandRegistry {
116116
) -> Result<(), InternalError> {
117117
let ctx = ExecutionContext::new(plot, sender, ArgumentSet::empty());
118118

119-
Self::send_error(&ctx, error_message)?;
119+
ctx.error(error_message).unwrap_runtime()?;
120120

121121
let usage = usage::generate_usage(path);
122-
Self::send_error(&ctx, &usage)?;
122+
ctx.reply_legacy(&format!("&6Usage: &e{}", usage))
123+
.unwrap_runtime()?;
123124
let base_name = usage::generate_base_name(path);
124-
Self::send_error(
125-
&ctx,
126-
&format!("Run /help {base_name} for more information."),
127-
)?;
125+
ctx.reply_legacy(&format!(
126+
"&eRun &e/help {}&e for more information.",
127+
base_name
128+
))
129+
.unwrap_runtime()?;
128130

129131
Ok(())
130132
}
131-
132-
fn send_error(ctx: &ExecutionContext<'_>, message: &str) -> Result<(), InternalError> {
133-
match ctx.error(message) {
134-
Ok(()) => Ok(()),
135-
Err(CommandError::Internal(err)) => Err(err),
136-
Err(CommandError::Runtime(_)) => {
137-
unreachable!("ExecutionContext::error should never return a runtime error")
138-
}
139-
}
140-
}
141133
}

0 commit comments

Comments
 (0)