refactor(commands): remove help text from command macro and use method to set it

This commit is contained in:
dusk 2025-01-20 22:50:45 +09:00
parent 07541d9926
commit c6db96115e
No known key found for this signature in database
6 changed files with 44 additions and 74 deletions

View file

@ -36,11 +36,7 @@ pub struct Command {
} }
impl Command { impl Command {
pub fn new( pub fn new(tokens: impl IntoIterator<Item = Token>, cb: impl Into<SmolStr>) -> Self {
tokens: impl IntoIterator<Item = Token>,
help: impl Into<SmolStr>,
cb: impl Into<SmolStr>,
) -> Self {
let tokens = tokens.into_iter().collect::<Vec<_>>(); let tokens = tokens.into_iter().collect::<Vec<_>>();
assert!(tokens.len() > 0); assert!(tokens.len() > 0);
let mut parse_flags_before = tokens.len(); let mut parse_flags_before = tokens.len();
@ -60,7 +56,7 @@ impl Command {
} }
Self { Self {
flags: Vec::new(), flags: Vec::new(),
help: help.into(), help: SmolStr::new_static("<no help text>"),
cb: cb.into(), cb: cb.into(),
show_in_suggestions: true, show_in_suggestions: true,
parse_flags_before, parse_flags_before,
@ -68,6 +64,11 @@ impl Command {
} }
} }
pub fn help(mut self, v: impl Into<SmolStr>) -> Self {
self.help = v.into();
self
}
pub fn show_in_suggestions(mut self, v: bool) -> Self { pub fn show_in_suggestions(mut self, v: bool) -> Self {
self.show_in_suggestions = v; self.show_in_suggestions = v;
self self
@ -111,16 +112,15 @@ impl Display for Command {
// (and something like &dyn Trait would require everything to be referenced which doesnt look nice anyway) // (and something like &dyn Trait would require everything to be referenced which doesnt look nice anyway)
#[macro_export] #[macro_export]
macro_rules! command { macro_rules! command {
([$($v:expr),+], $cb:expr, $help:expr) => { ([$($v:expr),+], $cb:expr$(,)*) => {
$crate::commands::Command::new([$(Token::from($v)),*], $help, $cb) $crate::commands::Command::new([$(Token::from($v)),*], $cb)
}; };
} }
pub fn all() -> Vec<Command> { pub fn all() -> impl Iterator<Item = Command> {
(help::cmds()) (help::cmds())
.chain(system::cmds()) .chain(system::cmds())
.chain(member::cmds()) .chain(member::cmds())
.chain(config::cmds()) .chain(config::cmds())
.chain(fun::cmds()) .chain(fun::cmds())
.collect()
} }

View file

@ -5,21 +5,15 @@ pub fn cmds() -> impl Iterator<Item = Command> {
let autoproxy = ["autoproxy", "ap"]; let autoproxy = ["autoproxy", "ap"];
[ [
command!( command!([cfg, autoproxy, ["account", "ac"]], "cfg_ap_account_show")
[cfg, autoproxy, ["account", "ac"]], .help("Shows autoproxy status for the account"),
"cfg_ap_account_show",
"Shows autoproxy status for the account"
),
command!( command!(
[cfg, autoproxy, ["account", "ac"], Toggle], [cfg, autoproxy, ["account", "ac"], Toggle],
"cfg_ap_account_update", "cfg_ap_account_update"
"Toggles autoproxy for the account" )
), .help("Toggles autoproxy for the account"),
command!( command!([cfg, autoproxy, ["timeout", "tm"]], "cfg_ap_timeout_show")
[cfg, autoproxy, ["timeout", "tm"]], .help("Shows the autoproxy timeout"),
"cfg_ap_timeout_show",
"Shows the autoproxy timeout"
),
command!( command!(
[ [
cfg, cfg,
@ -27,9 +21,9 @@ pub fn cmds() -> impl Iterator<Item = Command> {
["timeout", "tm"], ["timeout", "tm"],
any!(Disable, Reset, ("timeout", OpaqueString::SINGLE)) // todo: we should parse duration / time values any!(Disable, Reset, ("timeout", OpaqueString::SINGLE)) // todo: we should parse duration / time values
], ],
"cfg_ap_timeout_update", "cfg_ap_timeout_update"
"Sets the autoproxy timeout" )
), .help("Sets the autoproxy timeout"),
] ]
.into_iter() .into_iter()
} }

View file

@ -2,8 +2,8 @@ use super::*;
pub fn cmds() -> impl Iterator<Item = Command> { pub fn cmds() -> impl Iterator<Item = Command> {
[ [
command!(["thunder"], "fun_thunder", "fun thunder"), command!(["thunder"], "fun_thunder"),
command!(["meow"], "fun_meow", "fun meow"), command!(["meow"], "fun_meow"),
] ]
.into_iter() .into_iter()
} }

View file

@ -3,9 +3,9 @@ use super::*;
pub fn cmds() -> impl Iterator<Item = Command> { pub fn cmds() -> impl Iterator<Item = Command> {
let help = ["help", "h"]; let help = ["help", "h"];
[ [
command!([help], "help", "Shows the help command"), command!([help], "help").help("Shows the help command"),
command!([help, "commands"], "help_commands", "help commands"), command!([help, "commands"], "help_commands").help("help commands"),
command!([help, "proxy"], "help_proxy", "help proxy"), command!([help, "proxy"], "help_proxy").help("help proxy"),
] ]
.into_iter() .into_iter()
} }

View file

@ -7,22 +7,13 @@ pub fn cmds() -> impl Iterator<Item = Command> {
let new = ["new", "n"]; let new = ["new", "n"];
[ [
command!( command!([member, new, ("name", OpaqueString::SINGLE)], "member_new")
[member, new, ("name", OpaqueString::SINGLE)], .help("Creates a new system member"),
"member_new", command!([member, MemberRef], "member_show")
"Creates a new system member" .help("Shows information about a member")
), .value_flag("pt", Disable),
command!( command!([member, MemberRef, description], "member_desc_show")
[member, MemberRef], .help("Shows a member's description"),
"member_show",
"Shows information about a member"
)
.value_flag("pt", Disable),
command!(
[member, MemberRef, description],
"member_desc_show",
"Shows a member's description"
),
command!( command!(
[ [
member, member,
@ -30,14 +21,11 @@ pub fn cmds() -> impl Iterator<Item = Command> {
description, description,
("description", OpaqueString::REMAINDER) ("description", OpaqueString::REMAINDER)
], ],
"member_desc_update", "member_desc_update"
"Changes a member's description" )
), .help("Changes a member's description"),
command!( command!([member, MemberRef, privacy], "member_privacy_show")
[member, MemberRef, privacy], .help("Displays a member's current privacy settings"),
"member_privacy_show",
"Displays a member's current privacy settings"
),
command!( command!(
[ [
member, member,
@ -46,15 +34,10 @@ pub fn cmds() -> impl Iterator<Item = Command> {
MemberPrivacyTarget, MemberPrivacyTarget,
("new_privacy_level", PrivacyLevel) ("new_privacy_level", PrivacyLevel)
], ],
"member_privacy_update", "member_privacy_update"
"Changes a member's privacy settings"
),
command!(
[member, MemberRef, "soulscream"],
"member_soulscream",
"todo"
) )
.show_in_suggestions(false), .help("Changes a member's privacy settings"),
command!([member, MemberRef, "soulscream"], "member_soulscream").show_in_suggestions(false),
] ]
.into_iter() .into_iter()
} }

View file

@ -5,17 +5,10 @@ pub fn cmds() -> impl Iterator<Item = Command> {
let new = ["new", "n"]; let new = ["new", "n"];
[ [
command!( command!([system], "system_show").help("Shows information about your system"),
[system], command!([system, new], "system_new").help("Creates a new system"),
"system_show", command!([system, new, ("name", OpaqueString::SINGLE)], "system_new")
"Shows information about your system" .help("Creates a new system"),
),
command!([system, new], "system_new", "Creates a new system"),
command!(
[system, new, ("name", OpaqueString::SINGLE)],
"system_new",
"Creates a new system"
),
] ]
.into_iter() .into_iter()
} }