Lines
100 %
Functions
56.82 %
Branches
//! Paging state of the Transactions tab over the `transaction-query` form.
use std::ops::Range;
use cli_core::render::ListRow;
use cli_core::transaction::{Page, submit_call};
use forms::{FormValues, PAGING_KEYS, TRANSACTION_QUERY};
use tvui::range_text;
pub const PAGE_SIZE: u64 = 20;
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Pager {
offset: u64,
total: Option<u64>,
has_more: bool,
filter: FormValues,
summary: String,
}
/// The filtering values of `values`: the non-empty ones, paging keys left out.
#[must_use]
pub fn filter_of(values: &FormValues) -> FormValues {
let mut filter = FormValues::default();
TRANSACTION_QUERY
.filter_fields()
.filter_map(|field| Some((field.key, values.text(field.key)?)))
.filter(|(_, text)| !text.is_empty())
.for_each(|(key, text)| filter.set(key, text));
filter
/// Short note of a filter: the account label and the `from–to` range.
pub fn filter_summary(account_label: &str, filter: &FormValues) -> String {
let (from, to) = (filter.text("from"), filter.text("to"));
let range = (from.is_some() || to.is_some())
.then(|| format!("{}\u{2013}{}", from.unwrap_or(""), to.unwrap_or("")));
let account = filter.text("account").map(|_| account_label.to_string());
[account, range]
.into_iter()
.flatten()
.collect::<Vec<_>>()
.join(" ")
fn query_call(filter: &FormValues, offset: u64) -> String {
let mut values = filter.clone();
let [limit_key, offset_key] = PAGING_KEYS;
values.set(limit_key, &PAGE_SIZE.to_string());
values.set(offset_key, &offset.to_string());
submit_call(&TRANSACTION_QUERY, &values)
impl Pager {
/// The `submit-form` call that fetches the current page.
pub fn request(&self) -> String {
query_call(&self.filter, self.offset)
/// The call that fetches the first page of `filter`, which is not applied yet.
pub fn request_filtered(filter: &FormValues) -> String {
query_call(&filter_of(filter), 0)
/// Show the first page of `filter` from now on.
pub fn set_filter(&mut self, filter: &FormValues, summary: String) {
*self = Self {
filter: filter_of(filter),
summary,
..Self::default()
};
/// Drop the filter and go back to the first page.
pub fn clear_filter(&mut self) {
*self = Self::default();
pub fn filter(&self) -> &FormValues {
&self.filter
pub fn summary(&self) -> &str {
&self.summary
/// Step to the next page; `false` when the last one is showing.
pub fn advance(&mut self) -> bool {
let moved = self.has_more;
if moved {
self.offset = self.offset.saturating_add(PAGE_SIZE);
moved
/// Step to the previous page; `false` when the first one is showing.
pub fn retreat(&mut self) -> bool {
let moved = self.offset > 0;
self.offset = self.offset.saturating_sub(PAGE_SIZE);
/// Record a received page. `true` when it is empty past the first page
/// (rows were deleted), after which the offset points at the last page.
pub fn accept(&mut self, page: &Page) -> bool {
self.total = page.total;
self.has_more = page.has_more;
let overshot = page.rows.is_empty() && self.offset > 0;
if overshot {
let rows = page.total.unwrap_or(0);
self.offset = rows.saturating_sub(1) / PAGE_SIZE * PAGE_SIZE;
overshot
/// The rows in all, when known, and the index of the page's first row among them.
pub fn extent(&self, loaded: usize) -> (usize, usize) {
let first = usize::try_from(self.offset).unwrap_or(usize::MAX);
let total = self.total.and_then(|total| usize::try_from(total).ok());
(total.unwrap_or(first.saturating_add(loaded)), first)
/// `n–m of total` for the `visible` rows of the page.
pub fn range(&self, visible: Range<usize>) -> Option<String> {
let total = usize::try_from(self.total?).ok()?;
let first = usize::try_from(self.offset).ok()?;
range_text(
first.saturating_add(visible.start)..first.saturating_add(visible.end),
total,
)
/// The display row of every transaction of the page.
pub fn rows(page: &Page) -> Vec<ListRow> {
page.rows
.iter()
.map(|row| ListRow {
id: Some(row.id.clone()),
cells: vec![row.date.clone(), row.note.clone(), row.amount.clone()],
})
.collect()
#[cfg(test)]
mod filter_tests;
mod tests;