mirror of
https://github.com/natsukagami/youmubot.git
synced 2025-05-24 01:00:49 +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
|
@ -11,6 +11,7 @@ pub mod member_cache;
|
|||
pub mod pagination;
|
||||
pub mod ratelimit;
|
||||
pub mod setup;
|
||||
pub mod table_format;
|
||||
|
||||
pub use announcer::{Announcer, AnnouncerHandler};
|
||||
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
Add a link
Reference in a new issue