From edb1c85cd3bfbdd3900d71d59f1fcb3079d873f5 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Thu, 7 May 2020 21:56:09 +0200 Subject: [PATCH] fix colored readline prompt bug --- client/src/proxmark3.h | 13 ++++++------- client/src/ui.c | 4 ++++ include/ansi.h | 14 ++++++++++++-- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/client/src/proxmark3.h b/client/src/proxmark3.h index 9f4c767e2..6402baea9 100644 --- a/client/src/proxmark3.h +++ b/client/src/proxmark3.h @@ -18,15 +18,14 @@ #define PROXPROMPT_COMPOSE "[" "%s%s" "] pm3 --> " -#define PROXPROMPT_CTX_SCRIPTFILE "|" _GREEN_("script") -#define PROXPROMPT_CTX_SCRIPTCMD "|" _GREEN_("script") -#define PROXPROMPT_CTX_STDIN "|" _GREEN_("script") +#define PROXPROMPT_CTX_SCRIPTFILE "|" _RL_GREEN_("script") +#define PROXPROMPT_CTX_SCRIPTCMD "|" _RL_GREEN_("script") +#define PROXPROMPT_CTX_STDIN "|" _RL_GREEN_("script") #define PROXPROMPT_CTX_INTERACTIVE "" -#define PROXPROMPT_DEV_USB _BOLD_GREEN_("usb") -#define PROXPROMPT_DEV_FPC _BOLD_GREEN_("fpc") -#define PROXPROMPT_DEV_OFFLINE _BOLD_RED_("offline") - +#define PROXPROMPT_DEV_USB _RL_BOLD_GREEN_("usb") +#define PROXPROMPT_DEV_FPC _RL_BOLD_GREEN_("fpc") +#define PROXPROMPT_DEV_OFFLINE _RL_BOLD_RED_("offline") #define PROXHISTORY "history.txt" #define PROXLOG "log_%Y%m%d.txt" diff --git a/client/src/ui.c b/client/src/ui.c index 0b0ffdb56..58cd1606a 100644 --- a/client/src/ui.c +++ b/client/src/ui.c @@ -358,6 +358,10 @@ void memcpy_filter_ansi(void *dest, const void *src, size_t n, bool filter) { uint8_t *rsrc = (uint8_t *)src; uint16_t si = 0; for (uint16_t i = 0; i < n; i++) { + if ((i < n) + && ((rsrc[i] == '\001') || (rsrc[i] == '\002'))) + // skip readline special markers + continue; if ((i < n - 1) && (rsrc[i] == '\x1b') && (rsrc[i + 1] >= 0x40) diff --git a/include/ansi.h b/include/ansi.h index e86a36ee4..28784ba8c 100644 --- a/include/ansi.h +++ b/include/ansi.h @@ -5,12 +5,22 @@ #define _BLUE_(s) "\x1b[34m" s AEND #define _RED_(s) "\x1b[31m" s AEND -#define _BOLD_RED_(s) "\x1b[1;31m" s AEND #define _GREEN_(s) "\x1b[32m" s AEND -#define _BOLD_GREEN_(s) "\x1b[1;32m" s AEND #define _YELLOW_(s) "\x1b[33m" s AEND #define _MAGENTA_(s) "\x1b[35m" s AEND #define _CYAN_(s) "\x1b[36m" s AEND #define _WHITE_(s) "\x1b[37m" s AEND +// https://wiki.hackzine.org/development/misc/readline-color-prompt.html +// Applications may indicate that the prompt contains +// characters that take up no physical screen space when displayed by +// bracketing a sequence of such characters with the special markers +// RL_PROMPT_START_IGNORE = '\001' and RL_PROMPT_END_IGNORE = '\002' +#define RL_ESC(a) "\001" a "\002" + +#define _RL_RED_(s) RL_ESC("\x1b[31m") s RL_ESC(AEND) +#define _RL_GREEN_(s) RL_ESC("\x1b[32m") s RL_ESC(AEND) +#define _RL_BOLD_RED_(s) RL_ESC("\x1b[1;31m") s RL_ESC(AEND) +#define _RL_BOLD_GREEN_(s) RL_ESC("\x1b[1;32m") s RL_ESC(AEND) + #endif