mirror of
https://github.com/natsukagami/youmubot.git
synced 2025-04-20 01:08:55 +00:00
Use len() as hint for pagination
This commit is contained in:
parent
1799b70bc1
commit
f9d35954da
1 changed files with 44 additions and 14 deletions
|
@ -12,6 +12,8 @@ use tokio::time as tokio_time;
|
||||||
|
|
||||||
const ARROW_RIGHT: &str = "➡️";
|
const ARROW_RIGHT: &str = "➡️";
|
||||||
const ARROW_LEFT: &str = "⬅️";
|
const ARROW_LEFT: &str = "⬅️";
|
||||||
|
const REWIND: &str = "⏪";
|
||||||
|
const FAST_FORWARD: &str = "⏩";
|
||||||
|
|
||||||
/// A trait that provides the implementation of a paginator.
|
/// A trait that provides the implementation of a paginator.
|
||||||
#[async_trait::async_trait]
|
#[async_trait::async_trait]
|
||||||
|
@ -39,6 +41,12 @@ pub trait Paginate: Send + Sized {
|
||||||
.await
|
.await
|
||||||
.map(Some)
|
.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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait::async_trait]
|
#[async_trait::async_trait]
|
||||||
|
@ -91,12 +99,23 @@ async fn paginate_with_first_message(
|
||||||
timeout: std::time::Duration,
|
timeout: std::time::Duration,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
// React to the message
|
// 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
|
message
|
||||||
.react(&ctx, ReactionType::try_from(ARROW_LEFT)?)
|
.react(&ctx, ReactionType::try_from(ARROW_LEFT)?)
|
||||||
.await?;
|
.await?;
|
||||||
message
|
message
|
||||||
.react(&ctx, ReactionType::try_from(ARROW_RIGHT)?)
|
.react(&ctx, ReactionType::try_from(ARROW_RIGHT)?)
|
||||||
.await?;
|
.await?;
|
||||||
|
if large_count {
|
||||||
|
// add >> and << buttons
|
||||||
|
message
|
||||||
|
.react(&ctx, ReactionType::try_from(FAST_FORWARD)?)
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
pager.prerender(&ctx, &mut message).await?;
|
pager.prerender(&ctx, &mut message).await?;
|
||||||
pager.render(0, ctx, &mut message).await?;
|
pager.render(0, ctx, &mut message).await?;
|
||||||
// Build a reaction collector
|
// Build a reaction collector
|
||||||
|
@ -167,21 +186,32 @@ pub async fn handle_pagination_reaction(
|
||||||
let reaction = match reaction {
|
let reaction = match reaction {
|
||||||
ReactionAction::Added(v) | ReactionAction::Removed(v) => v,
|
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 {
|
match &reaction.emoji {
|
||||||
ReactionType::Unicode(ref s) => match s.as_str() {
|
ReactionType::Unicode(ref s) => {
|
||||||
ARROW_LEFT if page == 0 => Ok(page),
|
let new_page = match s.as_str() {
|
||||||
ARROW_LEFT => Ok(if pager.render(page - 1, ctx, message).await? {
|
ARROW_LEFT | REWIND if page == 0 => return Ok(page),
|
||||||
page - 1
|
ARROW_LEFT => page - 1,
|
||||||
|
REWIND => {
|
||||||
|
if page < fast {
|
||||||
|
0
|
||||||
} else {
|
} else {
|
||||||
page
|
page - fast
|
||||||
}),
|
}
|
||||||
ARROW_RIGHT => Ok(if pager.render(page + 1, ctx, message).await? {
|
}
|
||||||
page + 1
|
ARROW_RIGHT if pages.filter(|&pages| page as usize + 1 >= pages).is_some() => {
|
||||||
} else {
|
return Ok(page)
|
||||||
page
|
}
|
||||||
}),
|
ARROW_RIGHT => page + 1,
|
||||||
_ => Ok(page),
|
FAST_FORWARD => page + fast,
|
||||||
},
|
_ => return Ok(page),
|
||||||
|
};
|
||||||
|
Ok(match pager.render(new_page, ctx, message).await {
|
||||||
|
Err(_) => page,
|
||||||
|
Ok(_) => new_page,
|
||||||
|
})
|
||||||
|
}
|
||||||
_ => Ok(page),
|
_ => Ok(page),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue