From b9d431ccac1b94a8bd1e40c3398a1ec58000ac0e Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 27 Apr 2023 14:13:32 +0300 Subject: [PATCH] tg_cli: ignore Terminal input sequences. --- td/telegram/cli.cpp | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 22afce284..7d04ee2a9 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -2031,7 +2031,35 @@ class CliClient final : public Actor { } void on_cmd(string cmd) { - // TODO: need to remove https://en.wikipedia.org/wiki/ANSI_escape_code from cmd + for (size_t i = 0; i < cmd.size();) { + // https://en.wikipedia.org/wiki/ANSI_escape_code#Terminal_input_sequences + if (cmd[i] == 27 && cmd[i + 1] == '[') { + // likely an ANSI escape code + int j = i + 2; + if ('1' <= cmd[j] && cmd[j] <= '9') { + while ('0' <= cmd[j] && cmd[j] <= '9') { + j++; + } + } + if ('A' <= cmd[j] && cmd[j] <= 'Z') { + // xterm sequence + cmd = cmd.substr(0, i) + cmd.substr(j + 1); + continue; + } + if (cmd[j] == ';' && '1' <= cmd[j + 1] && cmd[j + 1] <= '9') { + j += 2; + while ('0' <= cmd[j] && cmd[j] <= '9') { + j++; + } + } + if (cmd[j] == '~') { + // vt sequence + cmd = cmd.substr(0, i) + cmd.substr(j + 1); + continue; + } + } + i++; + } td::remove_if(cmd, [](unsigned char c) { return c < 32; }); LOG(INFO) << "CMD:[" << cmd << "]";