chiark / gitweb /
Now we display the separator line of each toot. Progress!
authorSimon Tatham <anakin@pobox.com>
Fri, 29 Dec 2023 13:26:50 +0000 (13:26 +0000)
committerSimon Tatham <anakin@pobox.com>
Fri, 29 Dec 2023 18:17:34 +0000 (18:17 +0000)
src/client.rs
src/file.rs
src/text.rs

index cabbe335e4388ce431be72f376c7e88592cbf146..a2120d541b2645a38aec4a1bd10fe2a3bba498fa 100644 (file)
@@ -20,8 +20,8 @@ pub enum FeedId {
 }
 
 pub struct Feed {
-    ids: VecDeque<String>, // ids, whether of statuses, accounts or what
-    origin: isize,
+    pub ids: VecDeque<String>, // 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<Status> = 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")
     }
 }
index 7f9b701ec3eccde332cf557aaec08d28681ab48e..da7e921ae0b20f24db79c1de9a5919dfa3066c64 100644 (file)
@@ -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<Box<dyn TextFragment>>,
 }
 
 impl FeedFile {
     fn new(id: FeedId, client: &mut Client) -> Result<Self, ClientError> {
         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<ColouredString>, 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 {
index 638c03d2312821d56ea38fd426944073dc8396ae..f19e62fb258d878986afdcd61a0dba8fc6696af7 100644 (file)
@@ -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<Account>,
+}
+
+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<ColouredString> {
+        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
+    }
+}