chiark / gitweb /
wip library-add
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 27 Sep 2020 15:23:07 +0000 (16:23 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 27 Sep 2020 15:23:07 +0000 (16:23 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/bin/otter.rs

index 913d45b9e72b5abd90c6a280b8c45039fa83fe7c..54b83617d70c7cc4c2960ad409d99cb9882b33d0 100644 (file)
@@ -719,15 +719,11 @@ mod library_add {
 
     #[derive(Debug)]
     struct Placement {
-      top: Coord,
-      bot: Coord,
-      rhs: Coord,
-      lhs: Coord,
-      cline_upto: Coord,
-      cline_bottom: Coord,
+      lhs: Coord, top: Coord, rhs: Coord, bot: Coord,
+      clhs: Coord, cbot: Coord, // current line
     };
 
-    let placement = match situation {
+    let mut placement = match situation {
       Poor(insns, msg) => {
         if !args.adjust_markers() {
           throw!(anyhow!("only {} markers, wanted {}",
@@ -747,15 +743,41 @@ mod library_add {
         let bot = max(a.0[1], b.0[1]);
         Placement {
           lhs, rhs, top, bot,
-          cline_upto: lhs,
-          cline_bottom: top,
+          clhs: lhs, cbot: top,
         }
       }
     };
     dbg!(&placement);
 
-    let items = chan.list_items(&args.tlg.pat);
-    dbg!(&items);
+    impl Placement {
+      /// If returns None, has already maybe tried to take some space
+      fn place(&mut self, bbox: &[Pos;2]) -> Option<Pos> {
+        let PosC([w,h]) = bbox[1] - bbox[0];
+
+        let mut limit = 0..2;
+        let tlhs = loop {
+          limit.next()?; // item just too wide, maybe
+          let tlhs = self.clhs;
+          self.clhs += w;
+          if self.clhs <= self.rhs { break tlhs }
+          // line is full
+          self.top = self.cbot;
+          self.clhs = self.lhs;
+        };
+        self.cbot = min(self.cbot, h);
+        if self.cbot > self.bot { None? }
+
+        let ttopleft = PosC([tlhs, self.top]);
+        let tnominal = ttopleft - bbox[0];
+
+        dbg!(&self, &tnominal);
+        Some(tnominal)
+      }
+    }
+
+    let items = chan.list_items(&args.tlg.pat)?;
+    placement.place(&items[0].f0bbox);
+    //dbg!(&items);
 
     Ok(())
   }