From: Simon Tatham Date: Sat, 6 Jan 2024 11:32:09 +0000 (+0000) Subject: Track the latest read item within File's lifetime. X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=a47e08b0ce22c1f589301a509d6dd20902247cbe;p=mastodonochrome.git Track the latest read item within File's lifetime. Nothing actually uses it yet. --- diff --git a/src/file.rs b/src/file.rs index 22680b3..07614c4 100644 --- a/src/file.rs +++ b/src/file.rs @@ -321,6 +321,7 @@ struct File { file_desc: Type, search_direction: Option, last_search: Option, + latest_read_index: Option, } impl File { @@ -364,6 +365,7 @@ impl File { file_desc, search_direction: None, last_search: None, + latest_read_index: None, }; Ok(ff) } @@ -452,6 +454,30 @@ impl File { } } + fn update_latest_read_index(&mut self) { + let pos_item_shown_in_full = match self.pos.width { + None => self.pos.line > 0, + Some(pw) => match self.last_size { + None => false, // if in doubt don't mark things as read + Some((sw, _sh)) => if pw == sw { + self.rendered.get(&self.pos.item) + .map_or(false, |lines| self.pos.line == lines.len()) + } else { + false // similarly, if in doubt + } + } + }; + + let latest_read_index = if pos_item_shown_in_full { + self.pos.item + } else { + self.pos.item - 1 + }; + + self.latest_read_index = max( + self.latest_read_index, Some(latest_read_index)); + } + fn update_pos_for_size(&mut self, w: usize, h: usize) { if self.pos.width.is_none() && self.pos.line == 0 { // Special case: look at the _top_ of the item @@ -1023,7 +1049,14 @@ impl None => panic!("handle_keypress before resize"), }; - match self.ui_mode { + self.update_latest_read_index(); + + // This whole match is in a lambda which we immediately call, + // so that if any clause needs to do an early return, it will + // only return from this lambda and not the whole function. + // That way we never miss the update_latest_read_index at the + // end. + let action = (|| match self.ui_mode { UIMode::Normal => match key { Pr('q') | Pr('Q') => LogicalAction::Pop, Up => { @@ -1229,7 +1262,11 @@ impl Pr('q') | Pr('Q') => self.abort_selection(), _ => LogicalAction::Nothing, } - } + })(); + + self.update_latest_read_index(); + + action } fn handle_feed_updates(&mut self, feeds_updated: &HashSet,