Commit 15a0fb44 authored by Alice Mitchell's avatar Alice Mitchell

Add simple ANSI-256 colour sequences with &ff& and &ff:ff& sequences

implements #38
parent 3b97891f
Pipeline #546 passed with stage
in 7 minutes and 47 seconds
......@@ -28,6 +28,27 @@ static void copy_in(char *dest, int len, const char *input)
snprintf(dest, len, "%s", input);
}
static int hex_decode(const char *input, int len)
{
int i=0;
int total=0;
while (i<len && *input) {
total <<= 4;
if (*input >= '0' && *input <= '9')
total |= *input - '0';
else
if (*input >= 'a' && *input <= 'f')
total |= (*input - 'a') + 10;
else
if (*input >= 'A' && *input <= 'F')
total |= (*input - 'a') + 10;
input++;
i++;
}
return total;
}
/* return colour code sequence */
int colour(const char *input, char *output, int outlen)
{
......@@ -123,6 +144,43 @@ int colour(const char *input, char *output, int outlen)
output[i++] = 'm';
output[i] = '\0';
}
if (*input == '&') {
/* first let us test this is a valid colour string */
const char *p = input + 1;
/* skip past all valid chars */
while (*p && strchr("0123456789AbBcCdDeEfF:", *p)) p++;
/* not the ending we were expecting, ignore it then */
if (*p != '&') return 0;
char hex[7];
int i = 0;
consume = 1;
while (i < 7 && input[consume] && strchr("0123456789AbBcCdDeEfF", input[consume]))
hex[i++] = input[consume++];
hex[i] = '\0';
if (i == 2) {
int col = hex_decode(hex, 2);
snprintf(output, outlen, "\033[38;5;%dm", col);
}
if (input[consume] == ':') {
consume++;
i=0;
while (i < 7 && input[consume] && strchr("0123456789AbBcCdDeEfF", input[consume]))
hex[i++] = input[consume++];
hex[i] = '\0';
if (i == 2) {
int col = hex_decode(hex, 2);
int off = strlen(output);
snprintf(&output[off], outlen-off, "\033[48;5;%dm", col);
}
}
while (input[consume] && strchr("0123456789AbBcCdDeEfF:", input[consume]))
consume++;
if (input[consume] == '&') consume++;
}
return(consume);
}
......
......@@ -1086,7 +1086,7 @@ void display_message(const char *text, int beeps, int newline)
if (UseRL && disable_rl(1)) charcount = 0;
while (*text) {
if (*text == 033) {
if (*text == 033 || *text == '&') {
/* escape sequence, skip next two chars */
text += colour(text, colr, sizeof(colr));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment