]> git.neil.brown.name Git - edlib.git/commitdiff
lib-renderline: use foreach_attr() to parse image attrs.
authorNeilBrown <neil@brown.name>
Sun, 20 Aug 2023 10:11:32 +0000 (20:11 +1000)
committerNeilBrown <neil@brown.name>
Mon, 21 Aug 2023 12:27:57 +0000 (22:27 +1000)
This isn't a big win, but it is a bit nicer.

Signed-off-by: NeilBrown <neil@brown.name>
DOC/TODO.md
lib-renderline.c

index b51d4463020e307601152cff76c276cb9ecd389c..00f258debb2cfde02df23a092fafee1386da778c 100644 (file)
@@ -40,7 +40,7 @@ the file.
 - [ ] resolve shift-left vs shift_left distinction - add a "fixed" suffix?
 - [ ] should zoom affect whole window, not just pane?
 - [X] ditch cached-size for images - store in pane data instead.
-- [ ] use foreach_attr for parsing image details
+- [X] use foreach_attr for parsing image details
 - [ ] mergeview command to show diff between "found" and "replacement".
 - [ ] mergeview command to include both found and replacement, discard
   expected
index 497695bcfc0c700d4c60ee77ccd298dda5d585b8..16dc0fcae5f2d7a351e7328dcd3196238bb4908c 100644 (file)
@@ -1005,6 +1005,8 @@ static int render_image(struct pane *p safe, struct pane *focus safe,
        struct xy xyscale = pane_scale(focus);
        int scale = xyscale.x;
        struct xy size= {-1, -1};
+       const char *a, *v;
+       const char *end;
 
        if (dodraw)
                home_call(focus, "Draw:clear", p);
@@ -1015,10 +1017,10 @@ static int render_image(struct pane *p safe, struct pane *focus safe,
        while (*line == soh)
                line += 1;
 
-       while (*line && *line != stx && *line != etx) {
-               int len = strcspn(line, "," STX ETX);
-               if (strstarts(line, "image:")) {
-                       fname = strndup(line+6, len-6);
+       end = line + strcspn(line, STX);
+       foreach_attr(a, v, line, end) {
+               if (amatch(a, "image") && v) {
+                       aupdate(&fname, v);
                        if (rd->image_size.x <= 0) {
                                struct call_return cr =
                                        home_call_ret(all, focus,
@@ -1031,20 +1033,18 @@ static int render_image(struct pane *p safe, struct pane *focus safe,
                                }
                        } else
                                size = rd->image_size;
-               } else if (strstarts(line, "width:")) {
-                       width = atoi(line + 6);
-                       width = width * scale / 1000;
-               } else if (strstarts(line, "height:")) {
-                       height = atoi(line + 7);
-                       height = height * scale / 1000;
-               } else if (strstarts(line, "noupscale") &&
+               } else if (amatch(a, "width") && v) {
+                       width = anum(v) * scale / 1000;
+               } else if (amatch(a, "height") && v) {
+                       height = anum(v) * scale / 1000;
+               } else if (amatch(a, "noupscale") &&
                           fname && size.x > 0) {
                        if (size.x < p->parent->w)
                                width = size.x;
                        if (size.y < p->parent->h)
                                height = size.y;
                } else if ((offset >= 0 || want_xypos) &&
-                          strstarts(line, "map:")) {
+                          amatch(a, "map") && v) {
                        /*
                         * A map is map:LxxxLxxxLxxxLxxx or similar
                         * Where each "Lxxx" recognised by a CAP followed
@@ -1056,11 +1056,9 @@ static int render_image(struct pane *p safe, struct pane *focus safe,
                         * If offset is in the map, then set ->cx,->cy to
                         * the appropriate location.
                         */
-                       map_offset = line+4 - orig_line;
-                       parse_map(line+4, &rows, &cols);
+                       map_offset = v - orig_line;
+                       parse_map(v, &rows, &cols);
                }
-               line += len;
-               line += strspn(line, ",");
        }
        pane_resize(p, (p->parent->w - width)/2, p->y, width, height);