]> git.ipfire.org Git - thirdparty/systemd.git/commit
string-util: pass ANSI sequences through unchanged 28146/head
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 24 Jun 2023 00:40:14 +0000 (18:40 -0600)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 27 Jun 2023 22:55:02 +0000 (16:55 -0600)
commitcb558ab222f0dbda3afd985c2190f35693963ffa
tree1b3a0ffa54ac3603bab08c80f0481b21e9ae0b66
parentd9c72e54190db2a0845d1558b5beb734e9f629ff
string-util: pass ANSI sequences through unchanged

Cutting off in the middle may leave the terminal in a bad state, breaking
further output. But we don't know what a given ANSI sequence does, e.g.
ANSI_NORMAL should not be skipped. But it is also nice to keep various
sequences intact, so that if we had part of the string in blue, and we cut out
the beginning of the blue part, we still want to keep the remainder in color.
So let's just pass them through, stripping out the characters that take up
actual space.

Also, use memcpy_safe as we may end up copying zero bytes when ellipsizing at
the start/end of a string.

Fixes: #24502
This also fixes an ugliness where we would ellipsize string with ANSI
sequences too much, leading to output that was narrower on screen than the
requested length:

  Starting AAAAAAAAAAAAAAAAAAAAA.service
  Starting BBBBBBBBBBBBBBBBBBBBB.service
  Starting LONG…ER.service

Co-authored-by: Jan Janssen <medhefgo@web.de>
src/basic/string-util.c
src/test/test-ellipsize.c