Misc improvements to osu! (#15)

* Use len() as hint for pagination

* Implement len for beatmapset paging

* More tweaks for pagination

* Move table rendering to display mod

* Use grid throughout the commands

* No more double user update

* Sort by PP by default

* Filter check by mod

* Filter lb by mod

* Improve 1-page cases
This commit is contained in:
Natsu Kagami 2021-06-19 22:54:08 +09:00 committed by GitHub
parent 1799b70bc1
commit 2feb91ac00
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 482 additions and 297 deletions

View file

@ -12,6 +12,8 @@ use tokio::time as tokio_time;
const ARROW_RIGHT: &str = "➡️";
const ARROW_LEFT: &str = "⬅️";
const REWIND: &str = "";
const FAST_FORWARD: &str = "";
/// A trait that provides the implementation of a paginator.
#[async_trait::async_trait]
@ -39,6 +41,16 @@ pub trait Paginate: Send + Sized {
.await
.map(Some)
}
/// Return the number of pages, if it is known in advance.
/// If this is given, bounds-check will be done outside of `prerender` / `render`.
fn len(&self) -> Option<usize> {
None
}
fn is_empty(&self) -> Option<bool> {
self.len().map(|v| v == 0)
}
}
#[async_trait::async_trait]
@ -90,15 +102,30 @@ async fn paginate_with_first_message(
mut message: Message,
timeout: std::time::Duration,
) -> Result<()> {
pager.prerender(&ctx, &mut message).await?;
pager.render(0, ctx, &mut message).await?;
// Just quit if there is only one page
if pager.len().filter(|&v| v == 1).is_some() {
return Ok(());
}
// React to the message
let large_count = pager.len().filter(|&p| p > 10).is_some();
if large_count {
// add >> and << buttons
message.react(&ctx, ReactionType::try_from(REWIND)?).await?;
}
message
.react(&ctx, ReactionType::try_from(ARROW_LEFT)?)
.await?;
message
.react(&ctx, ReactionType::try_from(ARROW_RIGHT)?)
.await?;
pager.prerender(&ctx, &mut message).await?;
pager.render(0, ctx, &mut message).await?;
if large_count {
// add >> and << buttons
message
.react(&ctx, ReactionType::try_from(FAST_FORWARD)?)
.await?;
}
// Build a reaction collector
let mut reaction_collector = message.await_reactions(&ctx).removed(true).await;
let mut page = 0;
@ -167,21 +194,33 @@ pub async fn handle_pagination_reaction(
let reaction = match reaction {
ReactionAction::Added(v) | ReactionAction::Removed(v) => v,
};
let pages = pager.len();
let fast = pages.map(|v| v / 10).unwrap_or(5).max(5) as u8;
match &reaction.emoji {
ReactionType::Unicode(ref s) => match s.as_str() {
ARROW_LEFT if page == 0 => Ok(page),
ARROW_LEFT => Ok(if pager.render(page - 1, ctx, message).await? {
page - 1
ReactionType::Unicode(ref s) => {
let new_page = match s.as_str() {
ARROW_LEFT | REWIND if page == 0 => return Ok(page),
ARROW_LEFT => page - 1,
REWIND => {
if page < fast {
0
} else {
page - fast
}
}
ARROW_RIGHT if pages.filter(|&pages| page as usize + 1 >= pages).is_some() => {
return Ok(page)
}
ARROW_RIGHT => page + 1,
FAST_FORWARD => (pages.unwrap() as u8 - 1).min(page + fast),
_ => return Ok(page),
};
Ok(if pager.render(new_page, ctx, message).await? {
new_page
} else {
page
}),
ARROW_RIGHT => Ok(if pager.render(page + 1, ctx, message).await? {
page + 1
} else {
page
}),
_ => Ok(page),
},
})
}
_ => Ok(page),
}
}