From 8aa76b3ee6d3a7afd04357e11dd096ee40cea987 Mon Sep 17 00:00:00 2001 From: Julian Prein Date: Thu, 18 Sep 2025 23:54:21 +0200 Subject: [PATCH] bin:no-ansi: Don't just match CSI sequences Try to match all ANSI escape sequences, not just CSI (i.e. `\e[`) - also the ones that are followed by additional bytes (e.g. CSI, OSC). --- .local/bin/no-ansi | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/.local/bin/no-ansi b/.local/bin/no-ansi index 46f54d8..76cd61c 100755 --- a/.local/bin/no-ansi +++ b/.local/bin/no-ansi @@ -3,5 +3,21 @@ # Copyright (c) 2025 Julian Prein # # Remove ANSI escape sequences. +# +# Additionally to the general form `\e[ -/]*[0-~]` this also tries to cover +# sequences that are followed by additional bytes (e.g. CSI, OSC). This script +# covers no C1 sequences. +# +# See: +# - https://en.wikipedia.org/wiki/ANSI_escape_code +# - https://www.ecma-international.org/wp-content/uploads/ECMA-35_6th_edition_december_1994.pdf +# +# TODO: which bytes do DCS, OSC, SOS, PM & APC (second pattern) actually allow? +# Find some documentation, since `[ -~]` is guessed. -env LC_ALL=C sed "$(printf 's/\033\\[[0-?]*[ -/]*[@-~]//g')" +env LC_ALL=C sed -E "$(printf "%b" \ + 's/' \ + '\033\\[[0-?]*[ -/]*[@-~]' '|' \ + '\033[]PX^_][ -~]*\033\\\\' '|' \ + '\033[ -/]*[0-~]' \ + '//g')"