From: Simon Tatham Date: Fri, 29 Dec 2023 13:26:50 +0000 (+0000) Subject: Now we display the separator line of each toot. Progress! X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=c2944166db0b5bf348de90d811eec34c15526f98;p=mastodonochrome.git Now we display the separator line of each toot. Progress! --- diff --git a/src/client.rs b/src/client.rs index cabbe33..a2120d5 100644 --- a/src/client.rs +++ b/src/client.rs @@ -20,8 +20,8 @@ pub enum FeedId { } pub struct Feed { - ids: VecDeque, // ids, whether of statuses, accounts or what - origin: isize, + pub ids: VecDeque, // ids, whether of statuses, accounts or what + pub origin: isize, } pub struct Client { @@ -193,7 +193,9 @@ impl Client { let body = req.send()?.text()?; let st: Status = match serde_json::from_str(&body) { Ok(st) => Ok(st), - Err(e) => Err(ClientError::UrlError(url.clone(), e.to_string())), + Err(e) => { + Err(ClientError::UrlError(url.clone(), e.to_string())) + }, }?; if st.id != id { return Err(ClientError::UrlError( @@ -255,7 +257,10 @@ impl Client { let body = req.send()?.text()?; let sts: Vec = match serde_json::from_str(&body) { Ok(sts) => Ok(sts), - Err(e) => Err(ClientError::UrlError(url.clone(), e.to_string())), + Err(e) => { + dbg!(&body); + Err(ClientError::UrlError(url.clone(), e.to_string())) + }, }?; for st in &sts { self.cache_status(st); @@ -286,8 +291,8 @@ impl Client { Ok(()) } - pub fn borrow_feed(&self, id: FeedId) -> &Feed { - self.feeds.get(&id).expect( + pub fn borrow_feed(&self, id: &FeedId) -> &Feed { + self.feeds.get(id).expect( "should only ever borrow feeds that have been fetched") } } diff --git a/src/file.rs b/src/file.rs index 7f9b701..da7e921 100644 --- a/src/file.rs +++ b/src/file.rs @@ -1,5 +1,6 @@ use super::client::{Client, ClientError, FeedId, FeedExtend}; use super::coloured_string::ColouredString; +use super::text::*; use super::tui::{ ActivityState, CursorPosition, LogicalAction, OurKey, @@ -7,22 +8,49 @@ use super::tui::{ struct FeedFile { id: FeedId, + items: Vec>, } impl FeedFile { fn new(id: FeedId, client: &mut Client) -> Result { client.fetch_feed(&id, FeedExtend::Initial)?; - Ok(FeedFile { + let mut ff = FeedFile { id: id, - }) + items: Vec::new(), + }; + + ff.update_items(client); + + Ok(ff) + } + + fn update_items(&mut self, client: &mut Client) { + let ids: Vec<_> = client.borrow_feed(&self.id).ids + .iter().map(|x| x.clone()).collect(); + self.items.clear(); + for id in ids { + let st = client.status_by_id(&id) + .expect("Any id stored in a Feed should also be cached") + .clone(); + self.items.push(Box::new(StatusDisplay::new(st))); + } } } impl ActivityState for FeedFile { - fn draw(&self, _w: usize, _h: usize) + fn draw(&self, w: usize, h: usize) -> (Vec, CursorPosition) { - (Vec::new(), CursorPosition::None) // FIXME + let mut lines = Vec::new(); + + for item in &self.items { + lines.extend(item.render(w).iter().map(|line| line.to_owned())); + if lines.len() + 1 >= h { + break; + } + } + + (lines, CursorPosition::None) // FIXME } fn handle_keypress(&mut self, _key: OurKey) -> LogicalAction { diff --git a/src/text.rs b/src/text.rs index 638c03d..f19e62f 100644 --- a/src/text.rs +++ b/src/text.rs @@ -6,6 +6,7 @@ use std::collections::{HashMap, BTreeMap, BTreeSet}; use unicode_width::UnicodeWidthStr; use super::html; +use super::types::*; use super::tui::OurKey; use super::coloured_string::{ColouredString, ColouredStringSlice}; @@ -1479,3 +1480,36 @@ fn test_menu_keypress() { " k K "), }); } + +pub struct StatusDisplay { + st: Status, + via: Option, +} + +impl StatusDisplay { + pub fn new(st: Status) -> Self { + let (st, via) = match st.reblog { + Some(b) => (*b, Some(st.account)), + None => (st, None), + }; + + StatusDisplay { + st: st, + via: via, + } + } +} + +impl TextFragment for StatusDisplay { + fn render(&self, width: usize) -> Vec { + let mut lines = Vec::new(); + + let sep = SeparatorLine::new( + Some(self.st.created_at), + self.st.favourited == Some(true), + self.st.reblogged == Some(true)); + lines.extend(sep.render(width).iter().map(|line| line.to_owned())); + + lines + } +}