mirror of
https://github.com/natsukagami/youmubot.git
synced 2025-04-19 00:38:54 +00:00
Move table_formatting
to prelude (#38)
This commit is contained in:
parent
d23c86fb99
commit
54426ed477
4 changed files with 94 additions and 84 deletions
|
@ -1,15 +1,5 @@
|
||||||
use super::db::{OsuSavedUsers, OsuUser};
|
use std::{convert::TryInto, sync::Arc};
|
||||||
use super::{calculate_weighted_map_length, OsuClient};
|
|
||||||
use super::{embeds::score_embed, BeatmapWithMode};
|
|
||||||
use crate::{
|
|
||||||
discord::beatmap_cache::BeatmapMetaCache,
|
|
||||||
discord::cache::save_beatmap,
|
|
||||||
discord::oppai_cache::{BeatmapCache, BeatmapContent},
|
|
||||||
models::{Mode, Score, User, UserEventRank},
|
|
||||||
request::UserID,
|
|
||||||
Client as Osu,
|
|
||||||
};
|
|
||||||
use announcer::MemberToChannels;
|
|
||||||
use serenity::builder::CreateMessage;
|
use serenity::builder::CreateMessage;
|
||||||
use serenity::{
|
use serenity::{
|
||||||
http::CacheHttp,
|
http::CacheHttp,
|
||||||
|
@ -18,11 +8,25 @@ use serenity::{
|
||||||
id::{ChannelId, UserId},
|
id::{ChannelId, UserId},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
use std::{convert::TryInto, sync::Arc};
|
|
||||||
|
use announcer::MemberToChannels;
|
||||||
use youmubot_prelude::announcer::CacheAndHttp;
|
use youmubot_prelude::announcer::CacheAndHttp;
|
||||||
use youmubot_prelude::stream::{FuturesUnordered, TryStreamExt};
|
use youmubot_prelude::stream::TryStreamExt;
|
||||||
use youmubot_prelude::*;
|
use youmubot_prelude::*;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
discord::beatmap_cache::BeatmapMetaCache,
|
||||||
|
discord::cache::save_beatmap,
|
||||||
|
discord::oppai_cache::{BeatmapCache, BeatmapContent},
|
||||||
|
models::{Mode, Score, User, UserEventRank},
|
||||||
|
request::UserID,
|
||||||
|
Client as Osu,
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::db::{OsuSavedUsers, OsuUser};
|
||||||
|
use super::{calculate_weighted_map_length, OsuClient};
|
||||||
|
use super::{embeds::score_embed, BeatmapWithMode};
|
||||||
|
|
||||||
/// osu! announcer's unique announcer key.
|
/// osu! announcer's unique announcer key.
|
||||||
pub const ANNOUNCER_KEY: &str = "osu";
|
pub const ANNOUNCER_KEY: &str = "osu";
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,11 @@ use serenity::{
|
||||||
model::channel::Message,
|
model::channel::Message,
|
||||||
utils::MessageBuilder,
|
utils::MessageBuilder,
|
||||||
};
|
};
|
||||||
use youmubot_prelude::{stream::FuturesUnordered, *};
|
use youmubot_prelude::{
|
||||||
|
stream::FuturesUnordered,
|
||||||
|
table_format::{table_formatting, Align},
|
||||||
|
*,
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
enum RankQuery {
|
enum RankQuery {
|
||||||
|
@ -37,75 +41,6 @@ impl FromStr for RankQuery {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
|
||||||
enum Align {
|
|
||||||
Left,
|
|
||||||
Middle,
|
|
||||||
Right,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Align {
|
|
||||||
fn pad(self, input: &str, len: usize) -> String {
|
|
||||||
match self {
|
|
||||||
Align::Left => format!("{:<len$}", input),
|
|
||||||
Align::Middle => format!("{:^len$}", input),
|
|
||||||
Align::Right => format!("{:>len$}", input),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn table_formatting<const N: usize, S: AsRef<str> + std::fmt::Debug, Ts: AsRef<[[S; N]]>>(
|
|
||||||
headers: &[&'static str; N],
|
|
||||||
padding: &[Align; N],
|
|
||||||
table: Ts,
|
|
||||||
) -> String {
|
|
||||||
let table = table.as_ref();
|
|
||||||
// get length for each column
|
|
||||||
let lens = headers
|
|
||||||
.iter()
|
|
||||||
.enumerate()
|
|
||||||
.map(|(i, header)| {
|
|
||||||
table
|
|
||||||
.iter()
|
|
||||||
.map(|r| r.as_ref()[i].as_ref().len())
|
|
||||||
.max()
|
|
||||||
.unwrap_or(0)
|
|
||||||
.max(header.len())
|
|
||||||
})
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
// paint with message builder
|
|
||||||
let mut m = MessageBuilder::new();
|
|
||||||
m.push_line("```");
|
|
||||||
// headers first
|
|
||||||
for (i, header) in headers.iter().enumerate() {
|
|
||||||
if i > 0 {
|
|
||||||
m.push(" | ");
|
|
||||||
}
|
|
||||||
m.push(padding[i].pad(header, lens[i]));
|
|
||||||
}
|
|
||||||
m.push_line("");
|
|
||||||
// separator
|
|
||||||
m.push_line(format!(
|
|
||||||
"{:-<total$}",
|
|
||||||
"",
|
|
||||||
total = lens.iter().sum::<usize>() + (lens.len() - 1) * 3
|
|
||||||
));
|
|
||||||
// table itself
|
|
||||||
for row in table {
|
|
||||||
let row = row.as_ref();
|
|
||||||
for (i, cell) in row.iter().enumerate() {
|
|
||||||
if i > 0 {
|
|
||||||
m.push(" | ");
|
|
||||||
}
|
|
||||||
let cell = cell.as_ref();
|
|
||||||
m.push(padding[i].pad(cell, lens[i]));
|
|
||||||
}
|
|
||||||
m.push_line("");
|
|
||||||
}
|
|
||||||
m.push("```");
|
|
||||||
m.build()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[command("ranks")]
|
#[command("ranks")]
|
||||||
#[description = "See the server's ranks"]
|
#[description = "See the server's ranks"]
|
||||||
#[usage = "[mode (Std, Taiko, Catch, Mania) = Std]"]
|
#[usage = "[mode (Std, Taiko, Catch, Mania) = Std]"]
|
||||||
|
|
|
@ -11,6 +11,7 @@ pub mod member_cache;
|
||||||
pub mod pagination;
|
pub mod pagination;
|
||||||
pub mod ratelimit;
|
pub mod ratelimit;
|
||||||
pub mod setup;
|
pub mod setup;
|
||||||
|
pub mod table_format;
|
||||||
|
|
||||||
pub use announcer::{Announcer, AnnouncerHandler};
|
pub use announcer::{Announcer, AnnouncerHandler};
|
||||||
pub use args::{ChannelId, Duration, RoleId, UserId, UsernameArg};
|
pub use args::{ChannelId, Duration, RoleId, UserId, UsernameArg};
|
||||||
|
|
70
youmubot-prelude/src/table_format.rs
Normal file
70
youmubot-prelude/src/table_format.rs
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
use serenity::all::MessageBuilder;
|
||||||
|
|
||||||
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||||
|
pub enum Align {
|
||||||
|
Left,
|
||||||
|
Middle,
|
||||||
|
Right,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Align {
|
||||||
|
fn pad(self, input: &str, len: usize) -> String {
|
||||||
|
match self {
|
||||||
|
Align::Left => format!("{:<len$}", input),
|
||||||
|
Align::Middle => format!("{:^len$}", input),
|
||||||
|
Align::Right => format!("{:>len$}", input),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn table_formatting<const N: usize, S: AsRef<str> + std::fmt::Debug, Ts: AsRef<[[S; N]]>>(
|
||||||
|
headers: &[&'static str; N],
|
||||||
|
padding: &[Align; N],
|
||||||
|
table: Ts,
|
||||||
|
) -> String {
|
||||||
|
let table = table.as_ref();
|
||||||
|
// get length for each column
|
||||||
|
let lens = headers
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.map(|(i, header)| {
|
||||||
|
table
|
||||||
|
.iter()
|
||||||
|
.map(|r| r.as_ref()[i].as_ref().len())
|
||||||
|
.max()
|
||||||
|
.unwrap_or(0)
|
||||||
|
.max(header.len())
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
// paint with message builder
|
||||||
|
let mut m = MessageBuilder::new();
|
||||||
|
m.push_line("```");
|
||||||
|
// headers first
|
||||||
|
for (i, header) in headers.iter().enumerate() {
|
||||||
|
if i > 0 {
|
||||||
|
m.push(" | ");
|
||||||
|
}
|
||||||
|
m.push(padding[i].pad(header, lens[i]));
|
||||||
|
}
|
||||||
|
m.push_line("");
|
||||||
|
// separator
|
||||||
|
m.push_line(format!(
|
||||||
|
"{:-<total$}",
|
||||||
|
"",
|
||||||
|
total = lens.iter().sum::<usize>() + (lens.len() - 1) * 3
|
||||||
|
));
|
||||||
|
// table itself
|
||||||
|
for row in table {
|
||||||
|
let row = row.as_ref();
|
||||||
|
for (i, cell) in row.iter().enumerate() {
|
||||||
|
if i > 0 {
|
||||||
|
m.push(" | ");
|
||||||
|
}
|
||||||
|
let cell = cell.as_ref();
|
||||||
|
m.push(padding[i].pad(cell, lens[i]));
|
||||||
|
}
|
||||||
|
m.push_line("");
|
||||||
|
}
|
||||||
|
m.push("```");
|
||||||
|
m.build()
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue