implement parse list options and related commands

This commit is contained in:
dusk 2025-09-30 18:45:35 +00:00
parent 3e7898e5cc
commit 95fc7e9f60
No known key found for this signature in database
18 changed files with 367 additions and 199 deletions

View file

@ -1,11 +1,38 @@
use command_parser::token::TokensIterator;
use crate::utils::get_list_flags;
use super::*;
pub fn group() -> (&'static str, [&'static str; 1]) {
("group", ["g"])
pub fn group() -> (&'static str, [&'static str; 2]) {
("group", ["g", "groups"])
}
pub fn targeted() -> TokensIterator {
tokens!(group(), GroupRef)
}
pub fn cmds() -> impl Iterator<Item = Command> {
let group = group();
let group_target = targeted();
let apply_list_opts = |cmd: Command| cmd.flags(get_list_flags());
let group_list_members = tokens!(group_target, ("members", ["list", "ls"]));
let group_list_members_cmd = [
command!(group_list_members => "group_list_members"),
command!(group_list_members, "list" => "group_list_members"),
command!(group_list_members, ("search", ["find", "query"]), ("query", OpaqueStringRemainder) => "group_search_members"),
]
.into_iter()
.map(apply_list_opts);
let system_groups_cmd = [
command!(group, ("list", ["ls"]) => "group_list_groups"),
command!(group, ("search", ["find", "query"]), ("query", OpaqueStringRemainder) => "group_search_groups"),
]
.into_iter()
.map(apply_list_opts);
system_groups_cmd.chain(group_list_members_cmd)
}

View file

@ -18,11 +18,14 @@ pub mod server_config;
pub mod switch;
pub mod system;
pub mod utils;
use command_parser::{command, command::Command, parameter::ParameterKind::*, tokens};
pub fn all() -> impl Iterator<Item = Command> {
(help::cmds())
.chain(system::cmds())
.chain(group::cmds())
.chain(member::cmds())
.chain(config::cmds())
.chain(fun::cmds())

View file

@ -1,5 +1,7 @@
use command_parser::token::TokensIterator;
use crate::utils::get_list_flags;
use super::*;
pub fn member() -> (&'static str, [&'static str; 1]) {
@ -291,6 +293,16 @@ pub fn cmds() -> impl Iterator<Item = Command> {
.chain(member_webhook_avatar_cmd)
.chain(member_server_avatar_cmd);
let member_group = tokens!(member_target, group::group());
let member_list_group_cmds = [
command!(member_group => "member_list_groups"),
command!(member_group, "list" => "member_list_groups"),
command!(member_group, ("search", ["find", "query"]), ("query", OpaqueStringRemainder) => "member_search_groups"),
]
.into_iter()
.map(|cmd| cmd.flags(get_list_flags()));
let member_group_cmds = member_list_group_cmds;
let member_delete_cmd =
[command!(member_target, delete => "member_delete").help("Deletes a member")].into_iter();
@ -298,10 +310,7 @@ pub fn cmds() -> impl Iterator<Item = Command> {
[command!(member_target, "soulscream" => "member_soulscream").show_in_suggestions(false)]
.into_iter();
let member_list = [command!(member, "list" => "members_list")].into_iter();
member_new_cmd
.chain(member_list)
.chain(member_info_cmd)
.chain(member_name_cmd)
.chain(member_description_cmd)
@ -318,4 +327,5 @@ pub fn cmds() -> impl Iterator<Item = Command> {
.chain(member_message_settings_cmd)
.chain(member_delete_cmd)
.chain(member_easter_eggs)
.chain(member_group_cmds)
}

View file

@ -1,3 +1,5 @@
use crate::utils::get_list_flags;
use super::*;
pub fn cmds() -> impl Iterator<Item = Command> {
@ -7,7 +9,7 @@ pub fn cmds() -> impl Iterator<Item = Command> {
[
command!(random => "random_self").flag(group),
command!(system::targeted(), random => "system_random").flag(group),
command!(group::targeted(), random => "group_random_member"),
command!(group::targeted(), random => "group_random_member").flags(get_list_flags()),
]
.into_iter()
.map(|cmd| cmd.flag(("all", ["a"])))

View file

@ -1,5 +1,7 @@
use command_parser::token::TokensIterator;
use crate::utils::get_list_flags;
use super::*;
pub fn cmds() -> impl Iterator<Item = Command> {
@ -251,8 +253,33 @@ pub fn edit() -> impl Iterator<Item = Command> {
]
.into_iter();
let system_list =
[command!(system_target, ("members", ["list"]) => "system_members_list")].into_iter();
let system_list = ("members", ["list"]);
let system_search = tokens!(
("search", ["query", "find"]),
("query", OpaqueStringRemainder),
);
let add_list_flags = |cmd: Command| cmd.flags(get_list_flags());
let system_list_cmd = [
command!(system_target, system_list => "system_members_list"),
command!(system_target, system_search => "system_members_search"),
]
.into_iter()
.map(add_list_flags);
let system_list_self_cmd = [
command!(system, system_list => "system_members_list_self"),
command!(system, system_search => "system_members_search_self"),
]
.into_iter()
.map(add_list_flags);
let system_groups = tokens!(system_target, ("groups", ["gs"]));
let system_groups_cmd = [
command!(system_groups => "system_list_groups"),
command!(system_groups, ("list", ["ls"]) => "system_list_groups"),
command!(system_groups, ("search", ["find", "query"]), ("query", OpaqueStringRemainder) => "system_search_groups"),
]
.into_iter()
.map(add_list_flags);
system_new_cmd
.chain(system_name_self_cmd)
@ -265,6 +292,7 @@ pub fn edit() -> impl Iterator<Item = Command> {
.chain(system_avatar_self_cmd)
.chain(system_server_avatar_self_cmd)
.chain(system_banner_self_cmd)
.chain(system_list_self_cmd)
.chain(system_delete)
.chain(system_privacy_cmd)
.chain(system_proxy_cmd)
@ -281,5 +309,6 @@ pub fn edit() -> impl Iterator<Item = Command> {
.chain(system_info_cmd)
.chain(system_front_cmd)
.chain(system_link)
.chain(system_list)
.chain(system_list_cmd)
.chain(system_groups_cmd)
}

View file

@ -0,0 +1,52 @@
use command_parser::flag::Flag;
pub fn get_list_flags() -> [Flag; 22] {
[
// Short or long list
Flag::from(("full", ["f", "big", "details", "long"])),
// Search description
Flag::from((
"search-description",
[
"filter-description",
"in-description",
"sd",
"description",
"desc",
],
)),
// Sort properties
Flag::from(("by-name", ["bn"])),
Flag::from(("by-display-name", ["bdn"])),
Flag::from(("by-id", ["bid"])),
Flag::from(("by-message-count", ["bmc"])),
Flag::from(("by-created", ["bc", "bcd"])),
Flag::from((
"by-last-fronted",
["by-last-front", "by-last-switch", "blf", "bls"],
)),
Flag::from(("by-last-message", ["blm", "blp"])),
Flag::from(("by-birthday", ["by-birthdate", "bbd"])),
Flag::from(("random", ["rand"])),
// Sort reverse
Flag::from(("reverse", ["r", "rev"])),
// Privacy filter
Flag::from(("all", ["a"])),
Flag::from(("private-only", ["po"])),
// Additional fields to include
Flag::from((
"with-last-switch",
["with-last-fronted", "with-last-front", "wls", "wlf"],
)),
Flag::from(("with-last-message", ["with-last-proxy", "wlm", "wlp"])),
Flag::from(("with-message-count", ["wmc"])),
Flag::from(("with-created", ["wc"])),
Flag::from((
"with-avatar",
["with-image", "with-icon", "wa", "wi", "ia", "ii", "img"],
)),
Flag::from(("with-pronouns", ["wp", "wprns"])),
Flag::from(("with-displayname", ["wdn"])),
Flag::from(("with-birthday", ["wbd", "wb"])),
]
}

View file

@ -11,7 +11,7 @@ use crate::{flag::Flag, token::Token};
pub struct Command {
// TODO: fix hygiene
pub tokens: Vec<Token>,
pub flags: Vec<Flag>,
pub flags: HashSet<Flag>,
pub help: SmolStr,
pub cb: SmolStr,
pub show_in_suggestions: bool,
@ -34,7 +34,7 @@ impl Command {
}
}
Self {
flags: Vec::new(),
flags: HashSet::new(),
help: SmolStr::new_static("<no help text>"),
cb: cb.into(),
show_in_suggestions: true,
@ -54,34 +54,57 @@ impl Command {
self
}
pub fn flags(mut self, flags: impl IntoIterator<Item = impl Into<Flag>>) -> Self {
self.flags.extend(flags.into_iter().map(Into::into));
self
}
pub fn flag(mut self, flag: impl Into<Flag>) -> Self {
self.flags.push(flag.into());
self.flags.insert(flag.into());
self
}
pub fn hidden_flag(mut self, flag: impl Into<Flag>) -> Self {
let flag = flag.into();
self.hidden_flags.insert(flag.get_name().into());
self.flags.push(flag);
self.flags.insert(flag);
self
}
}
impl Display for Command {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let visible_flags = self
.flags
.iter()
.filter(|flag| !self.hidden_flags.contains(flag.get_name()))
.collect::<Vec<_>>();
let write_flags = |f: &mut std::fmt::Formatter<'_>, space: bool| {
for flag in &self.flags {
if self.hidden_flags.contains(flag.get_name()) {
continue;
if visible_flags.is_empty() {
return Ok(());
}
write!(f, "{}(", space.then_some(" ").unwrap_or(""))?;
let mut written = 0;
let max_flags = visible_flags.len().min(5);
for flag in &visible_flags {
if written > max_flags {
break;
}
write!(f, "{flag}")?;
if max_flags - 1 > written {
write!(f, " ")?;
}
written += 1;
}
if visible_flags.len() > written {
let rest_count = visible_flags.len() - written;
write!(
f,
"{}[{flag}]{}",
space.then_some(" ").unwrap_or(""),
space.then_some("").unwrap_or(" ")
" ...and {rest_count} flag{}...",
(rest_count > 1).then_some("s").unwrap_or(""),
)?;
}
std::fmt::Result::Ok(())
write!(f, "){}", space.then_some("").unwrap_or(" "))
};
for (idx, token) in self.tokens.iter().enumerate() {

View file

@ -1,4 +1,4 @@
use std::fmt::Display;
use std::{fmt::Display, hash::Hash};
use smol_str::SmolStr;
@ -28,6 +28,20 @@ impl Display for Flag {
}
}
impl PartialEq for Flag {
fn eq(&self, other: &Self) -> bool {
self.name.eq(&other.name)
}
}
impl Eq for Flag {}
impl Hash for Flag {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.name.hash(state);
}
}
#[derive(Debug)]
pub enum FlagMatchError {
ValueMatchFailed(FlagValueMatchError),

View file

@ -1,7 +1,7 @@
#![feature(anonymous_lifetime_in_impl_trait)]
pub mod command;
mod flag;
pub mod flag;
pub mod parameter;
mod string;
pub mod token;

View file

@ -1,4 +1,4 @@
use std::{env, fmt::Write, fs, path::PathBuf, str::FromStr};
use std::{collections::HashSet, env, fmt::Write, fs, path::PathBuf, str::FromStr};
use command_parser::{
parameter::{Parameter, ParameterKind},
@ -20,16 +20,26 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
writeln!(&mut glue, "using Myriad.Types;")?;
writeln!(&mut glue, "namespace PluralKit.Bot;\n")?;
let mut commands_seen = HashSet::new();
let mut record_fields = String::new();
for command in &commands {
if commands_seen.contains(&command.cb) {
continue;
}
writeln!(
&mut record_fields,
r#"public record {command_name}({command_name}Params parameters, {command_name}Flags flags): Commands;"#,
command_name = command_callback_to_name(&command.cb),
)?;
commands_seen.insert(command.cb.clone());
}
commands_seen.clear();
let mut match_branches = String::new();
for command in &commands {
if commands_seen.contains(&command.cb) {
continue;
}
let mut command_params_init = String::new();
let command_params = find_parameters(&command.tokens);
for param in &command_params {
@ -68,6 +78,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
command_name = command_callback_to_name(&command.cb),
command_callback = command.cb,
)?;
commands_seen.insert(command.cb.clone());
}
write!(
&mut glue,
@ -87,7 +98,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}}
"#,
)?;
commands_seen.clear();
for command in &commands {
if commands_seen.contains(&command.cb) {
continue;
}
let mut command_params_fields = String::new();
let command_params = find_parameters(&command.tokens);
for param in &command_params {
@ -133,6 +149,76 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
)?;
}
command_reply_format.push_str("return ReplyFormat.Standard;\n");
let mut command_list_options = String::new();
let mut command_list_options_class = String::new();
let list_flags = command_definitions::utils::get_list_flags();
if list_flags.iter().all(|flag| command.flags.contains(&flag)) {
write!(&mut command_list_options_class, ": IHasListOptions")?;
writeln!(
&mut command_list_options,
r#"
public ListOptions GetListOptions(Context ctx, SystemId target)
{{
var directLookupCtx = ctx.DirectLookupContextFor(target);
var lookupCtx = ctx.LookupContextFor(target);
var p = new ListOptions();
p.Type = full ? ListType.Long : ListType.Short;
// Search description filter
p.SearchDescription = search_description;
// Sort property
if (by_name) p.SortProperty = SortProperty.Name;
if (by_display_name) p.SortProperty = SortProperty.DisplayName;
if (by_id) p.SortProperty = SortProperty.Hid;
if (by_message_count) p.SortProperty = SortProperty.MessageCount;
if (by_created) p.SortProperty = SortProperty.CreationDate;
if (by_last_fronted) p.SortProperty = SortProperty.LastSwitch;
if (by_last_message) p.SortProperty = SortProperty.LastMessage;
if (by_birthday) p.SortProperty = SortProperty.Birthdate;
if (random) p.SortProperty = SortProperty.Random;
// Sort reverse
p.Reverse = reverse;
// Privacy filter
if (all) p.PrivacyFilter = null;
if (private_only) p.PrivacyFilter = PrivacyLevel.Private;
// PERM CHECK: If we're trying to access non-public members of another system, error
if (p.PrivacyFilter != PrivacyLevel.Public && directLookupCtx != LookupContext.ByOwner)
// TODO: should this just return null instead of throwing or something? >.>
throw Errors.NotOwnInfo;
// this is for searching
p.Context = lookupCtx;
// Additional fields to include
p.IncludeLastSwitch = with_last_switch;
p.IncludeLastMessage = with_last_message;
p.IncludeMessageCount = with_message_count;
p.IncludeCreated = with_created;
p.IncludeAvatar = with_avatar;
p.IncludePronouns = with_pronouns;
p.IncludeDisplayName = with_displayname;
p.IncludeBirthday = with_birthday;
// Always show the sort property (unless short list and already showing something else)
if (p.Type != ListType.Short || p.includedCount == 0)
{{
if (p.SortProperty == SortProperty.DisplayName) p.IncludeDisplayName = true;
if (p.SortProperty == SortProperty.MessageCount) p.IncludeMessageCount = true;
if (p.SortProperty == SortProperty.CreationDate) p.IncludeCreated = true;
if (p.SortProperty == SortProperty.LastSwitch) p.IncludeLastSwitch = true;
if (p.SortProperty == SortProperty.LastMessage) p.IncludeLastMessage = true;
if (p.SortProperty == SortProperty.Birthdate) p.IncludeBirthday = true;
}}
p.AssertIsValid();
return p;
}}
"#,
)?;
}
write!(
&mut glue,
r#"
@ -140,7 +226,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
{{
{command_params_fields}
}}
public class {command_name}Flags
public class {command_name}Flags {command_list_options_class}
{{
{command_flags_fields}
@ -148,10 +234,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
{{
{command_reply_format}
}}
{command_list_options}
}}
"#,
command_name = command_callback_to_name(&command.cb),
)?;
commands_seen.insert(command.cb.clone());
}
fs::write(write_location, glue)?;
Ok(())

View file

@ -17,7 +17,7 @@ fn main() {
}
} else {
for command in command_definitions::all() {
println!("{} - {}", command, command.help);
println!("{} => {} - {}", command.cb, command, command.help);
}
}
}