From e3735612d2a4647c6b5c41b7a7bb5911f73e3fa0 Mon Sep 17 00:00:00 2001 From: NeilBrown Date: Sun, 20 Aug 2023 20:11:32 +1000 Subject: [PATCH] lib-renderline: use foreach_attr() to parse image attrs. This isn't a big win, but it is a bit nicer. Signed-off-by: NeilBrown --- DOC/TODO.md | 2 +- lib-renderline.c | 30 ++++++++++++++---------------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/DOC/TODO.md b/DOC/TODO.md index b51d4463..00f258de 100644 --- a/DOC/TODO.md +++ b/DOC/TODO.md @@ -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 diff --git a/lib-renderline.c b/lib-renderline.c index 497695bc..16dc0fca 100644 --- a/lib-renderline.c +++ b/lib-renderline.c @@ -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); -- 2.39.5