Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use crate::{
#[derive(Default)]
pub struct Document {
sections: Vec<Section>,
total_lines: u16,
}

impl Document {
Expand All @@ -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);
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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)>) {
Expand Down Expand Up @@ -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![]),
Expand All @@ -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();
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -440,6 +453,10 @@ impl Document {
}
}
}

pub fn total_lines(&self) -> u16 {
self.total_lines
}
}

impl Deref for Document {
Expand Down
2 changes: 1 addition & 1 deletion src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
2 changes: 1 addition & 1 deletion src/snapshots/mdfried__tests__duplicate image done.snap
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ expression: terminal.backend()
" 🭢▀🭢▀𜴀🭢▀. "
" "
" "
" "
" 1/1"
2 changes: 1 addition & 1 deletion src/snapshots/mdfried__tests__duplicate image preview.snap
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ expression: terminal.backend()
" "
" "
" "
" "
" 1/1"
2 changes: 1 addition & 1 deletion src/snapshots/mdfried__tests__first parse done.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ expression: terminal.backend()
" "
" "
" "
" "
" 1/1"
2 changes: 1 addition & 1 deletion src/snapshots/mdfried__tests__reload add image done.snap
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ expression: terminal.backend()
" .΄▔΄▔ ⠁¯`▔▔▔΄ "
" "
" "
" "
" 1/2"
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ expression: terminal.backend()
" "
" "
" "
" "
" 1/1"
2 changes: 1 addition & 1 deletion src/snapshots/mdfried__tests__reload move image down.snap
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ expression: terminal.backend()
" "
" "
" "
" "
" 1/1"
2 changes: 1 addition & 1 deletion src/snapshots/mdfried__tests__reload move image up.snap
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ expression: terminal.backend()
" "
" "
" "
" "
" 1/1"
21 changes: 21 additions & 0 deletions src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,26 @@ pub fn view(model: &Model, buf: &mut Buffer) -> Option<Position> {
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());
Expand Down Expand Up @@ -204,6 +224,7 @@ pub fn view(model: &Model, buf: &mut Buffer) -> Option<Position> {
}
};
}

cursor_position
}

Expand Down
Loading