chiark / gitweb /
Factor out feed extension code into a method of File.
authorSimon Tatham <anakin@pobox.com>
Thu, 4 Jan 2024 07:48:00 +0000 (07:48 +0000)
committerSimon Tatham <anakin@pobox.com>
Thu, 4 Jan 2024 08:14:03 +0000 (08:14 +0000)
Mostly just because it's large and cumbersome and it's nice to keep
the key-handling match cases short.

src/file.rs

index 5773a95fc7e2463f1c3299fbe91f4e271d55c0fe..1042b7caf13f6d4d81225ff3e30493d8ae4c1c0b 100644 (file)
@@ -412,6 +412,38 @@ impl<Type: FileType, Source: FileDataSource> File<Type, Source> {
         self.ensure_enough_rendered();
         self.after_setting_pos();
     }
+
+    fn try_extend(&mut self, client: &mut Client) -> Result<(), ClientError> {
+        let action = match self.contents.source.try_extend(client) {
+            Ok(any_new) => {
+                self.rendered.remove(&self.contents.first_index());
+                if let Some(i) = self.contents.extender_index() {
+                    self.rendered.remove(&i);
+                }
+
+                if !any_new {
+                    // Can't extend this any further into the past
+                    self.contents.extender = None;
+                    if self.pos.item() < self.contents.origin {
+                        self.coarsen_pos();
+                        if self.pos.item() <
+                            self.contents.first_index()
+                        {
+                            self.pos = FilePosition::Coarse(
+                                self.contents.first_index());
+                        }
+                    }
+                }
+
+                self.contents.update_items(client);
+                Ok(())
+            }
+            Err(e) => Err(e),
+        };
+
+        self.ensure_enough_rendered();
+        action
+    }
 }
 
 impl<Type: FileType, Source: FileDataSource>
@@ -544,35 +576,10 @@ impl<Type: FileType, Source: FileDataSource>
             }
             Pr('0') | Home => {
                 if self.at_top() && self.contents.extender.is_some() {
-                    let action = match self.contents.source.try_extend(client) {
-                        Ok(any_new) => {
-                            self.rendered.remove(&self.contents.first_index());
-                            if let Some(i) = self.contents.extender_index() {
-                                self.rendered.remove(&i);
-                            }
-
-                            if !any_new {
-                                // Can't extend this any further into the past
-                                // FIXME: this is not tested yet
-                                self.contents.extender = None;
-                                if self.pos.item() < self.contents.origin {
-                                    self.coarsen_pos();
-                                    if self.pos.item() <
-                                        self.contents.first_index()
-                                    {
-                                        self.pos = FilePosition::Coarse(
-                                            self.contents.first_index());
-                                    }
-                                }
-                            }
-
-                            self.contents.update_items(client);
-                            LogicalAction::Nothing
-                        }
+                    match self.try_extend(client) {
+                        Ok(_) => LogicalAction::Nothing,
                         Err(e) => LogicalAction::Error(e),
-                    };
-                    self.ensure_enough_rendered();
-                    action
+                    }
                 } else {
                     self.goto_top();
                     LogicalAction::Nothing