From 3d7be7d84b19d07d56a3725b6447fb741add2923 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Gro=C3=9Fe?= Date: Mon, 22 Jun 2026 10:13:29 +0100 Subject: [PATCH] display current and total pages --- src/document.rs | 19 ++++++++++++++++- src/model.rs | 2 +- .../mdfried__tests__duplicate image done.snap | 2 +- ...fried__tests__duplicate image preview.snap | 2 +- .../mdfried__tests__first parse done.snap | 2 +- ...ed__tests__first parse image previews.snap | 2 +- ...mdfried__tests__reload add image done.snap | 2 +- ...ried__tests__reload add image preview.snap | 2 +- ...dfried__tests__reload move image down.snap | 2 +- .../mdfried__tests__reload move image up.snap | 2 +- src/view.rs | 21 +++++++++++++++++++ 11 files changed, 48 insertions(+), 10 deletions(-) diff --git a/src/document.rs b/src/document.rs index 5501e1c..11d53c6 100644 --- a/src/document.rs +++ b/src/document.rs @@ -40,6 +40,7 @@ use crate::{ #[derive(Default)] pub struct Document { sections: Vec
, + total_lines: u16, } impl Document { @@ -48,6 +49,7 @@ impl Document { !self.sections.iter().any(|s| s.id == section.id), "Document::push expects unique ids" ); + self.total_lines += section.height; self.sections.push(section); } @@ -76,12 +78,16 @@ impl Document { } if let Some((start, end)) = range { + let old_height: u16 = self.sections[start..end].iter().map(|s| s.height).sum(); + let new_height: u16 = updates.iter().map(|s| s.height).sum(); self.sections.splice(start..end, updates); + self.total_lines = self.total_lines.saturating_sub(old_height) + new_height; } else if let Some(last) = self.sections.last() && last.id < first_id { log::debug!("Update section #{first_id} not found but id is higher than last section"); for section in updates { + self.total_lines += section.height; self.sections.push(section); } } else { @@ -106,12 +112,15 @@ impl Document { }; // Add an extra space if the source had one. + let old_height = section.height; let trailing_blank: u16 = trailing_blank.into(); + let new_height = size.height + trailing_blank; *section = Section { id: section_id, - height: size.height + trailing_blank, + height: new_height, content: SectionContent::Image(link, proto, size, max_size), }; + self.total_lines = self.total_lines.saturating_sub(old_height) + new_height; } pub fn update_header(&mut self, section_id: SectionID, rows: Vec<(String, u8, Protocol)>) { @@ -144,6 +153,7 @@ impl Document { match &mut section.content { SectionContent::Image(url, _, _, _) => { let url = url.clone(); + let old_height = section.height; let SectionContent::Image(link, proto, size, max_size) = std::mem::replace( &mut section.content, SectionContent::ImagePlaceholder(url, vec![]), @@ -154,6 +164,7 @@ impl Document { .images .insert(link.url.clone(), (proto, size, max_size)); section.height = 1; + self.total_lines = self.total_lines.saturating_sub(old_height) + section.height; } SectionContent::Header(text, tier, Some(_)) => { let text = text.clone(); @@ -197,7 +208,9 @@ impl Document { .position(|section| section.id == last_section_id) { log::debug!("trim: {idx} + 1"); + let removed: u16 = self.sections[idx + 1..].iter().map(|s| s.height).sum(); self.sections.truncate(idx + 1); + self.total_lines = self.total_lines.saturating_sub(removed); } } @@ -440,6 +453,10 @@ impl Document { } } } + + pub fn total_lines(&self) -> u16 { + self.total_lines + } } impl Deref for Document { diff --git a/src/model.rs b/src/model.rs index 81a6249..e19f87e 100644 --- a/src/model.rs +++ b/src/model.rs @@ -239,7 +239,7 @@ impl Model { } pub fn total_lines(&self) -> u16 { - self.document.iter().map(|s| s.height).sum() + self.document.total_lines() } pub fn process_events(&mut self) -> Result<(bool, bool, bool), Error> { diff --git a/src/snapshots/mdfried__tests__duplicate image done.snap b/src/snapshots/mdfried__tests__duplicate image done.snap index dcc7d7a..315d732 100644 --- a/src/snapshots/mdfried__tests__duplicate image done.snap +++ b/src/snapshots/mdfried__tests__duplicate image done.snap @@ -21,4 +21,4 @@ expression: terminal.backend() " 🭢▀🭢▀𜴀🭢▀. " " " " " -" " +" 1/1" diff --git a/src/snapshots/mdfried__tests__duplicate image preview.snap b/src/snapshots/mdfried__tests__duplicate image preview.snap index 811d1dd..3aef4bf 100644 --- a/src/snapshots/mdfried__tests__duplicate image preview.snap +++ b/src/snapshots/mdfried__tests__duplicate image preview.snap @@ -21,4 +21,4 @@ expression: terminal.backend() " " " " " " -" " +" 1/1" diff --git a/src/snapshots/mdfried__tests__first parse done.snap b/src/snapshots/mdfried__tests__first parse done.snap index b2e81e2..1d84a76 100644 --- a/src/snapshots/mdfried__tests__first parse done.snap +++ b/src/snapshots/mdfried__tests__first parse done.snap @@ -21,4 +21,4 @@ expression: terminal.backend() " " "[?7l[?7l]66;s=2:n=7:d=7:w=7;Last bi\]66;s=2:n=7:d=7:w=1;t\" Hidden by multi-width symbols: [(1, " "), (2, " "), (3, " "), (4, " "), (5, " "), (6, " "), (7, " "), (8, " "), (9, " "), (10, " "), (11, " "), (12, " "), (13, " "), (14, " "), (15, " "), (16, " "), (17, " "), (18, " "), (19, " "), (20, " "), (21, " "), (22, " "), (23, " "), (24, " "), (25, " "), (26, " "), (27, " "), (28, " "), (29, " "), (30, " "), (31, " "), (32, " "), (33, " "), (34, " "), (35, " "), (36, " "), (37, " "), (38, " "), (39, " "), (40, " "), (41, " "), (42, " "), (43, " "), (44, " "), (45, " "), (46, " "), (47, " "), (48, " "), (49, " "), (50, " "), (51, " "), (52, " "), (53, " "), (54, " "), (55, " "), (56, " "), (57, " "), (58, " "), (59, " "), (60, " "), (61, " "), (62, " "), (63, " "), (64, " "), (65, " "), (66, " "), (67, " "), (68, " "), (69, " "), (70, " "), (71, " "), (72, " "), (73, " "), (74, " "), (75, " "), (76, " "), (77, " "), (78, " "), (79, " ")] " " -" " +" 1/2" diff --git a/src/snapshots/mdfried__tests__first parse image previews.snap b/src/snapshots/mdfried__tests__first parse image previews.snap index a6862c6..2732992 100644 --- a/src/snapshots/mdfried__tests__first parse image previews.snap +++ b/src/snapshots/mdfried__tests__first parse image previews.snap @@ -21,4 +21,4 @@ expression: terminal.backend() " " " " " " -" " +" 1/1" diff --git a/src/snapshots/mdfried__tests__reload add image done.snap b/src/snapshots/mdfried__tests__reload add image done.snap index 1c0ed0b..243744d 100644 --- a/src/snapshots/mdfried__tests__reload add image done.snap +++ b/src/snapshots/mdfried__tests__reload add image done.snap @@ -21,4 +21,4 @@ expression: terminal.backend() " .΄▔΄▔ ⠁¯`▔▔▔΄ " " " " " -" " +" 1/2" diff --git a/src/snapshots/mdfried__tests__reload add image preview.snap b/src/snapshots/mdfried__tests__reload add image preview.snap index 1926fbf..3b6f42b 100644 --- a/src/snapshots/mdfried__tests__reload add image preview.snap +++ b/src/snapshots/mdfried__tests__reload add image preview.snap @@ -21,4 +21,4 @@ expression: terminal.backend() " " " " " " -" " +" 1/1" diff --git a/src/snapshots/mdfried__tests__reload move image down.snap b/src/snapshots/mdfried__tests__reload move image down.snap index 4debc08..9460b41 100644 --- a/src/snapshots/mdfried__tests__reload move image down.snap +++ b/src/snapshots/mdfried__tests__reload move image down.snap @@ -21,4 +21,4 @@ expression: terminal.backend() " " " " " " -" " +" 1/1" diff --git a/src/snapshots/mdfried__tests__reload move image up.snap b/src/snapshots/mdfried__tests__reload move image up.snap index d99941c..0dab5e6 100644 --- a/src/snapshots/mdfried__tests__reload move image up.snap +++ b/src/snapshots/mdfried__tests__reload move image up.snap @@ -21,4 +21,4 @@ expression: terminal.backend() " " " " " " -" " +" 1/1" diff --git a/src/view.rs b/src/view.rs index bf9186d..8bb8c83 100644 --- a/src/view.rs +++ b/src/view.rs @@ -124,6 +124,26 @@ pub fn view(model: &Model, buf: &mut Buffer) -> Option { builtin_override_view(model, content_area, buf); let status_line_y = inner_area.height - 1; + + let total = model.total_lines(); + let inner_h = model.inner_height(); + if total > 0 && inner_h > 0 { + let inner_h = inner_h as u32; + let total_pages = (total as u32).div_ceil(inner_h); + let at_end = model.scroll as u32 + inner_h >= total as u32; + let page = if at_end { + total_pages + } else { + model.scroll as u32 / inner_h + 1 + }; + let page_text = format!("{page}/{total_pages}"); + let page_width = page_text.len() as u16; + let x = buf.area().width.saturating_sub(page_width); + Paragraph::new(page_text) + .fg(Color::DarkGray) + .render(Rect::new(x, status_line_y, page_width, 1), buf); + } + let mut cursor_position = None; // Position::from((0, buf.area.height - 1)); if let Some(err) = &model.last_error { let line = Line::from(err.to_string()); @@ -204,6 +224,7 @@ pub fn view(model: &Model, buf: &mut Buffer) -> Option { } }; } + cursor_position }