Re: [PATCH 1/3 v2] Limit width of formatted text by characters rather than bytes
Jeremie Courreges-Anglas <[email protected]> Mon, 28 Aug 2017 20:50:19 +0200
| Newsgroups | gmane.comp.window-managers.ratpoison.devel |
|---|---|
| Message-ID | <[email protected]> |
--===============9121855140965526242== Content-Type: multipart/signed; boundary="=-=-="; micalg=pgp-sha512; protocol="application/pgp-signature" --=-=-= Content-Type: text/plain Content-Transfer-Encoding: quoted-printable Hi Will, On Sun, Aug 27 2017, Will Storey <[email protected]> wrote: > When formatting text for display in the window list, it is possible to > specify a limit to truncate at. This is useful for example with %t when > you have a long title in the window. > > The prior implementation truncated counting by bytes. This was > problematic if the limit happened to be in the middle of a multibyte > character. When that happened, the window list text cut off starting at > the invalid character. > > We now count by characters rather than bytes. This ensures we always > include a full multibyte character. > > It is possible to see the problem with this test case: > > set winfmt %n%s%10t > set winliststyle row > set winname title > > Then create a window such that we truncate in the middle of a multibyte > character. This is possible with the following HTML document: > > <!DOCTYPE html> > <meta charset=3D"utf-8"> > <title>testing ™ 1 2 3</title> > > Assuming you are using UTF-8 encoding, if your browser's title has only > this text, then truncating at 10 will truncate on the second of the > three bytes in the trademark symbol. First, thanks for your submission. You're dealing with a known problem. The direction taken so far in ratpoison was: don't deal with wide characters, only handle UTF-8 in a rather dumb but at least simple way. Rationale: =2D the wide characters API has a lot of gotchas. I won't detail them here but what to do in case of an invalid sequence often remains an open question. Here, I can see that you return a partial length early. I'm not sure this is desirable. =2D UTF-8 is easy and looks like the sanest choice for a multibyte locale. No offense, but other less commonly used locales are just a pain to handle. Think state-dependant encodings. So while technically speaking the wide characters API looks like the obvious choice, I think its cost is a bit high. Consistency is good. If we start using the wide chars API somewhere, it should be used in all places where it makes sense. I'm not sure this is an easy task even in ratpoison. :) Handling only UTF-8 as a multibyte locale, the tentative diff below seems to do the job. *WARNING*: I have barely tested it with your html testcase. Feedback / test reports welcome. diff --git a/src/format.c b/src/format.c index caf8781..fa8b068 100644 =2D-- a/src/format.c +++ b/src/format.c @@ -82,11 +82,18 @@ concat_width (struct sbuf *buf, char *s, int width) { if (width >=3D 0) { =2D char *s1 =3D xsprintf ("%%.%ds", width); =2D char *s2 =3D xsprintf (s1, s); =2D sbuf_concat (buf, s2); =2D free (s1); =2D free (s2); + int len =3D 0; + + while (s[len] !=3D '\0' && len < width) + { + if (RP_IS_UTF8_START (s[len])) + do + len++; + while (RP_IS_UTF8_CONT (s[len])); + else + len++; + } + sbuf_printf_concat (buf, "%.*s", len, s); } else sbuf_concat (buf, s); =2D-=20 jca | PGP : 0x1524E7EE / 5135 92C1 AD36 5293 2BDF DDCC 0DFA 74AE 1524 E7EE --=-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEEUTWSwa02UpMr393MDfp0rhUk5+4FAlmkZesACgkQDfp0rhUk 5+6COg//YyIckkR1pTMC8JvPnwT8PTijPaMrUJv6vmknUp632CexBxsK6Ur2eV8a iKrn963gA62DynEDX2KJFjAEMwd23TJiQ1dH7KXGDk3qaB01P3Q/qATtgG0sXC+s fClF02ZApfIOAlNtDWxHjwTsF7446Do9FpQVFajHj54zpHz0oq9bWZeDYIh9xt5V iI43TDmV0qO+fOnbNm6ntoBMI/aCJrS7kKPqo1n86WUZoABNlE7Fh3MURaUIetGb 6TuRKTCoYPw9Zyr1wpwvgZ8UfT2f6KSvxpq/HRrv4z+jSsrKKAR/r7LshVCJZhF0 WuaNL4l8tU8j/qsWk458WXZbsclc7npCU4hpdrcuKe2gjFF8yh40vTWxwtV3gqeh WCBrloOsFKrLKxj7dprqRo4fqteqICBlAwpXLhWxbQ08dYdR6A+++uoHcuFkFcH9 Us8VezFd2qscbPT5gDw9ogdpZDW5Z7GWDRac+jbGo9KFG56u3Ixilbq0H3GBCbyb LC+XJb9C12gwW6AJ/0VAIfu7oHnfiP3IB2TKK67/J93VGeEtcIs1AKOpbcZJhKsV 3Sv9g7+w4YBJVAkuU4X7NJ8MULPaaEt6DZoe1GmbBmLq42FUxWcT1da6Mv1ESR22 pKkXG8gW6sUjB+Byi2/QqT+0CefJ0uG/JW9S9vIPqhNj40K/Hck= =E7Kt -----END PGP SIGNATURE----- --=-=-=-- --===============9121855140965526242== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Ratpoison-devel mailing list [email protected] https://lists.nongnu.org/mailman/listinfo/ratpoison-devel --===============9121855140965526242==--