term: implement bright background colour; linux/darwin support for format codes

This commit is contained in:
2024-11-14 23:11:55 +00:00
parent 64a246e6f7
commit 8a78d9c94a
5 changed files with 405 additions and 63 deletions

View File

@@ -1,11 +1,12 @@
#include "print.h"
#include <blue/term.h>
#include <blue/core/hash.h>
#include <blue/term.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#define F_BLACK "[black]"
#define F_RED "[red]"
@@ -101,7 +102,8 @@ static void apply_modifier(FILE *fp, const char *modifier, unsigned int *mod_fla
if (COMPARE_MOD_NAME(modifier, "white", mod_hash, MOD_HASH_WHITE)) {
mod_flags = Z__B_STREAM_MOD_CLEAR_FG_COLOUR(mod_flags);
mod_flags |= Z__B_STREAM_MOD_RED | Z__B_STREAM_MOD_GREEN | Z__B_STREAM_MOD_BLUE;
mod_flags |= Z__B_STREAM_MOD_RED | Z__B_STREAM_MOD_GREEN
| Z__B_STREAM_MOD_BLUE;
}
if (COMPARE_MOD_NAME(modifier, "bg_black", mod_hash, MOD_HASH_BG_BLACK)) {
@@ -141,7 +143,8 @@ static void apply_modifier(FILE *fp, const char *modifier, unsigned int *mod_fla
if (COMPARE_MOD_NAME(modifier, "bg_white", mod_hash, MOD_HASH_BG_WHITE)) {
mod_flags = Z__B_STREAM_MOD_CLEAR_BG_COLOUR(mod_flags);
mod_flags |= Z__B_STREAM_MOD_BG_RED | Z__B_STREAM_MOD_BG_GREEN | Z__B_STREAM_MOD_BG_BLUE;
mod_flags |= Z__B_STREAM_MOD_BG_RED | Z__B_STREAM_MOD_BG_GREEN
| Z__B_STREAM_MOD_BG_BLUE;
}
if (COMPARE_MOD_NAME(modifier, "bold", mod_hash, MOD_HASH_BOLD)) {
@@ -177,7 +180,7 @@ static int print(FILE *fp, const char *str)
char ctrl_buf[128];
unsigned int ctrl_len = 0;
for (size_t i = 0; str[i]; ) {
for (size_t i = 0; str[i];) {
char c = str[i++];
if (c != '[') {

View File

@@ -5,36 +5,44 @@
#include <blue/term.h>
#include <stdio.h>
enum {
enum z__b_stream_modifier {
Z__B_STREAM_MOD_BLACK = 0x1,
Z__B_STREAM_MOD_RED = 0x2,
Z__B_STREAM_MOD_GREEN = 0x4,
Z__B_STREAM_MOD_BLUE = 0x8,
Z__B_STREAM_MOD_BRIGHT = 0x10,
Z__B_STREAM_MOD_BG_BLACK = 0x10,
Z__B_STREAM_MOD_BG_RED = 0x20,
Z__B_STREAM_MOD_BG_GREEN = 0x40,
Z__B_STREAM_MOD_BG_BLUE = 0x80,
Z__B_STREAM_MOD_BG_BLACK = 0x20,
Z__B_STREAM_MOD_BG_RED = 0x40,
Z__B_STREAM_MOD_BG_GREEN = 0x80,
Z__B_STREAM_MOD_BG_BLUE = 0x100,
Z__B_STREAM_MOD_BG_BRIGHT = 0x200,
Z__B_STREAM_MOD_BOLD = 0x100,
Z__B_STREAM_MOD_BRIGHT = 0x200,
Z__B_STREAM_MOD_ITALIC = 0x400,
Z__B_STREAM_MOD_ULINE = 0x800,
Z__B_STREAM_MOD_INVERT = 0x1000,
Z__B_STREAM_MOD_BOLD = 0x400,
Z__B_STREAM_MOD_ITALIC = 0x800,
Z__B_STREAM_MOD_ULINE = 0x1000,
Z__B_STREAM_MOD_INVERT = 0x2000,
Z__B_STREAM_MOD_RESET = 0x2000,
Z__B_STREAM_MOD_RESET = 0x4000,
};
#define Z__B_STREAM_MOD_GET_FG_COLOUR(x) \
((x) & (Z__B_STREAM_MOD_BLACK | Z__B_STREAM_MOD_RED | Z__B_STREAM_MOD_GREEN | Z__B_STREAM_MOD_BLUE))
((x) \
& (Z__B_STREAM_MOD_BLACK | Z__B_STREAM_MOD_RED \
| Z__B_STREAM_MOD_GREEN | Z__B_STREAM_MOD_BLUE))
#define Z__B_STREAM_MOD_CLEAR_FG_COLOUR(x) \
((x) & ~(Z__B_STREAM_MOD_BLACK | Z__B_STREAM_MOD_RED | Z__B_STREAM_MOD_GREEN | Z__B_STREAM_MOD_BLUE))
((x) \
& ~(Z__B_STREAM_MOD_BLACK | Z__B_STREAM_MOD_RED \
| Z__B_STREAM_MOD_GREEN | Z__B_STREAM_MOD_BLUE))
#define Z__B_STREAM_MOD_GET_BG_COLOUR(x) \
((x) & (Z__B_STREAM_MOD_BG_BLACK | Z__B_STREAM_MOD_BG_RED | Z__B_STREAM_MOD_BG_GREEN | Z__B_STREAM_MOD_BG_BLUE))
((x) \
& (Z__B_STREAM_MOD_BG_BLACK | Z__B_STREAM_MOD_BG_RED \
| Z__B_STREAM_MOD_BG_GREEN | Z__B_STREAM_MOD_BG_BLUE))
#define Z__B_STREAM_MOD_CLEAR_BG_COLOUR(x) \
((x) & ~(Z__B_STREAM_MOD_BG_BLACK | Z__B_STREAM_MOD_BG_RED | Z__B_STREAM_MOD_BG_GREEN | Z__B_STREAM_MOD_BG_BLUE))
((x) \
& ~(Z__B_STREAM_MOD_BG_BLACK | Z__B_STREAM_MOD_BG_RED \
| Z__B_STREAM_MOD_BG_GREEN | Z__B_STREAM_MOD_BG_BLUE))
BLUE_API int z__b_stream_is_tty(FILE *fp);
BLUE_API int z__b_stream_dimensions(FILE *fp, unsigned int *w, unsigned int *h);

View File

@@ -1,3 +1,5 @@
#include "../../print.h"
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
@@ -63,3 +65,158 @@ int z__b_stream_cursorpos(FILE *in, FILE *out, unsigned int *x, unsigned int *y)
return 0;
}
int z__b_stream_set_modifier(FILE *fp, enum z__b_stream_modifier mod)
{
char buf[128];
int buf_i = 0;
int nr_codes = 0;
buf[buf_i++] = '\033';
buf[buf_i++] = '[';
if (mod & Z__B_STREAM_MOD_RESET) {
buf[buf_i++] = '0';
buf[buf_i++] = 'm';
buf[buf_i++] = '\0';
fputs(buf, fp);
return 0;
}
if (mod & Z__B_STREAM_MOD_BOLD) {
if (nr_codes > 0) {
buf[buf_i++] = ';';
}
buf[buf_i++] = '1';
nr_codes++;
}
if (mod & Z__B_STREAM_MOD_ITALIC) {
if (nr_codes > 0) {
buf[buf_i++] = ';';
}
buf[buf_i++] = '3';
nr_codes++;
}
if (mod & Z__B_STREAM_MOD_ULINE) {
if (nr_codes > 0) {
buf[buf_i++] = ';';
}
buf[buf_i++] = '4';
nr_codes++;
}
if (mod & Z__B_STREAM_MOD_INVERT) {
if (nr_codes > 0) {
buf[buf_i++] = ';';
}
buf[buf_i++] = '7';
nr_codes++;
}
if (Z__B_STREAM_MOD_GET_FG_COLOUR(mod) != 0) {
if (nr_codes > 0) {
buf[buf_i++] = ';';
}
buf[buf_i++] = mod & Z__B_STREAM_MOD_BRIGHT ? '9' : '3';
}
switch (Z__B_STREAM_MOD_GET_FG_COLOUR(mod)) {
case Z__B_STREAM_MOD_BLACK:
buf[buf_i++] = '0';
nr_codes++;
break;
case Z__B_STREAM_MOD_RED:
buf[buf_i++] = '1';
nr_codes++;
break;
case Z__B_STREAM_MOD_GREEN:
buf[buf_i++] = '2';
nr_codes++;
break;
case Z__B_STREAM_MOD_BLUE:
nr_codes++;
break;
case Z__B_STREAM_MOD_RED | Z__B_STREAM_MOD_GREEN:
buf[buf_i++] = '3';
nr_codes++;
break;
case Z__B_STREAM_MOD_RED | Z__B_STREAM_MOD_BLUE:
buf[buf_i++] = '5';
nr_codes++;
break;
case Z__B_STREAM_MOD_GREEN | Z__B_STREAM_MOD_BLUE:
buf[buf_i++] = '6';
nr_codes++;
break;
case Z__B_STREAM_MOD_RED | Z__B_STREAM_MOD_GREEN | Z__B_STREAM_MOD_BLUE:
buf[buf_i++] = '7';
nr_codes++;
break;
default:
break;
}
if (Z__B_STREAM_MOD_GET_BG_COLOUR(mod) != 0) {
if (nr_codes > 0) {
buf[buf_i++] = ';';
}
if (mod & Z__B_STREAM_MOD_BG_BRIGHT) {
buf[buf_i++] = '1';
buf[buf_i++] = '0';
} else {
buf[buf_i++] = '9';
}
}
switch (Z__B_STREAM_MOD_GET_BG_COLOUR(mod)) {
case Z__B_STREAM_MOD_BG_BLACK:
buf[buf_i++] = '0';
nr_codes++;
break;
case Z__B_STREAM_MOD_BG_RED:
buf[buf_i++] = '1';
nr_codes++;
break;
case Z__B_STREAM_MOD_BG_GREEN:
buf[buf_i++] = '2';
nr_codes++;
break;
case Z__B_STREAM_MOD_BG_BLUE:
nr_codes++;
break;
case Z__B_STREAM_MOD_BG_RED | Z__B_STREAM_MOD_BG_GREEN:
buf[buf_i++] = '3';
nr_codes++;
break;
case Z__B_STREAM_MOD_BG_RED | Z__B_STREAM_MOD_BG_BLUE:
buf[buf_i++] = '5';
nr_codes++;
break;
case Z__B_STREAM_MOD_BG_GREEN | Z__B_STREAM_MOD_BG_BLUE:
buf[buf_i++] = '6';
nr_codes++;
break;
case Z__B_STREAM_MOD_BG_RED | Z__B_STREAM_MOD_BG_GREEN
| Z__B_STREAM_MOD_BG_BLUE:
buf[buf_i++] = '7';
nr_codes++;
break;
default:
break;
}
buf[buf_i++] = 'm';
buf[buf_i++] = '\0';
fputs(buf, fp);
return 0;
}

View File

@@ -1,3 +1,5 @@
#include "../../print.h"
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
@@ -63,3 +65,158 @@ int z__b_stream_cursorpos(FILE *in, FILE *out, unsigned int *x, unsigned int *y)
return 0;
}
int z__b_stream_set_modifier(FILE *fp, enum z__b_stream_modifier mod)
{
char buf[128];
int buf_i = 0;
int nr_codes = 0;
buf[buf_i++] = '\033';
buf[buf_i++] = '[';
if (mod & Z__B_STREAM_MOD_RESET) {
buf[buf_i++] = '0';
buf[buf_i++] = 'm';
buf[buf_i++] = '\0';
fputs(buf, fp);
return 0;
}
if (mod & Z__B_STREAM_MOD_BOLD) {
if (nr_codes > 0) {
buf[buf_i++] = ';';
}
buf[buf_i++] = '1';
nr_codes++;
}
if (mod & Z__B_STREAM_MOD_ITALIC) {
if (nr_codes > 0) {
buf[buf_i++] = ';';
}
buf[buf_i++] = '3';
nr_codes++;
}
if (mod & Z__B_STREAM_MOD_ULINE) {
if (nr_codes > 0) {
buf[buf_i++] = ';';
}
buf[buf_i++] = '4';
nr_codes++;
}
if (mod & Z__B_STREAM_MOD_INVERT) {
if (nr_codes > 0) {
buf[buf_i++] = ';';
}
buf[buf_i++] = '7';
nr_codes++;
}
if (Z__B_STREAM_MOD_GET_FG_COLOUR(mod) != 0) {
if (nr_codes > 0) {
buf[buf_i++] = ';';
}
buf[buf_i++] = mod & Z__B_STREAM_MOD_BRIGHT ? '9' : '3';
}
switch (Z__B_STREAM_MOD_GET_FG_COLOUR(mod)) {
case Z__B_STREAM_MOD_BLACK:
buf[buf_i++] = '0';
nr_codes++;
break;
case Z__B_STREAM_MOD_RED:
buf[buf_i++] = '1';
nr_codes++;
break;
case Z__B_STREAM_MOD_GREEN:
buf[buf_i++] = '2';
nr_codes++;
break;
case Z__B_STREAM_MOD_BLUE:
nr_codes++;
break;
case Z__B_STREAM_MOD_RED | Z__B_STREAM_MOD_GREEN:
buf[buf_i++] = '3';
nr_codes++;
break;
case Z__B_STREAM_MOD_RED | Z__B_STREAM_MOD_BLUE:
buf[buf_i++] = '5';
nr_codes++;
break;
case Z__B_STREAM_MOD_GREEN | Z__B_STREAM_MOD_BLUE:
buf[buf_i++] = '6';
nr_codes++;
break;
case Z__B_STREAM_MOD_RED | Z__B_STREAM_MOD_GREEN | Z__B_STREAM_MOD_BLUE:
buf[buf_i++] = '7';
nr_codes++;
break;
default:
break;
}
if (Z__B_STREAM_MOD_GET_BG_COLOUR(mod) != 0) {
if (nr_codes > 0) {
buf[buf_i++] = ';';
}
if (mod & Z__B_STREAM_MOD_BG_BRIGHT) {
buf[buf_i++] = '1';
buf[buf_i++] = '0';
} else {
buf[buf_i++] = '9';
}
}
switch (Z__B_STREAM_MOD_GET_BG_COLOUR(mod)) {
case Z__B_STREAM_MOD_BG_BLACK:
buf[buf_i++] = '0';
nr_codes++;
break;
case Z__B_STREAM_MOD_BG_RED:
buf[buf_i++] = '1';
nr_codes++;
break;
case Z__B_STREAM_MOD_BG_GREEN:
buf[buf_i++] = '2';
nr_codes++;
break;
case Z__B_STREAM_MOD_BG_BLUE:
nr_codes++;
break;
case Z__B_STREAM_MOD_BG_RED | Z__B_STREAM_MOD_BG_GREEN:
buf[buf_i++] = '3';
nr_codes++;
break;
case Z__B_STREAM_MOD_BG_RED | Z__B_STREAM_MOD_BG_BLUE:
buf[buf_i++] = '5';
nr_codes++;
break;
case Z__B_STREAM_MOD_BG_GREEN | Z__B_STREAM_MOD_BG_BLUE:
buf[buf_i++] = '6';
nr_codes++;
break;
case Z__B_STREAM_MOD_BG_RED | Z__B_STREAM_MOD_BG_GREEN
| Z__B_STREAM_MOD_BG_BLUE:
buf[buf_i++] = '7';
nr_codes++;
break;
default:
break;
}
buf[buf_i++] = 'm';
buf[buf_i++] = '\0';
fputs(buf, fp);
return 0;
}

View File

@@ -1,7 +1,8 @@
#include "../../print.h"
#include <Windows.h>
#include <stdio.h>
#include <string.h>
#include <Windows.h>
#include "../../print.h"
int z__b_stream_is_tty(FILE *fp)
{
@@ -39,7 +40,6 @@ int z__b_stream_cursorpos(FILE *in, FILE *out, unsigned int *x, unsigned int *y)
HANDLE console = (HANDLE)_get_osfhandle(fileno(in));
BOOL status = GetConsoleScreenBufferInfo(console, &csbi);
if (status == FALSE) {
return -1;
}
@@ -89,27 +89,44 @@ int z__b_stream_set_modifier(FILE *fp, unsigned int mod)
if (Z__B_STREAM_MOD_GET_FG_COLOUR(mod) != 0) {
if (mod & Z__B_STREAM_MOD_BLACK) {
attrib &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
attrib
&= ~(FOREGROUND_RED | FOREGROUND_GREEN
| FOREGROUND_BLUE);
} else {
APPLY_FLAG_X(mod, attrib, Z__B_STREAM_MOD_RED, FOREGROUND_RED);
APPLY_FLAG_X(mod, attrib, Z__B_STREAM_MOD_GREEN, FOREGROUND_GREEN);
APPLY_FLAG_X(mod, attrib, Z__B_STREAM_MOD_BLUE, FOREGROUND_BLUE);
APPLY_FLAG_X(
mod, attrib, Z__B_STREAM_MOD_RED, FOREGROUND_RED);
APPLY_FLAG_X(
mod, attrib, Z__B_STREAM_MOD_GREEN,
FOREGROUND_GREEN);
APPLY_FLAG_X(
mod, attrib, Z__B_STREAM_MOD_BLUE, FOREGROUND_BLUE);
}
}
if (Z__B_STREAM_MOD_GET_BG_COLOUR(mod) != 0) {
if (mod & Z__B_STREAM_MOD_BG_BLACK) {
attrib &= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
attrib
&= ~(BACKGROUND_RED | BACKGROUND_GREEN
| BACKGROUND_BLUE);
} else {
APPLY_FLAG_X(mod, attrib, Z__B_STREAM_MOD_BG_RED, BACKGROUND_RED);
APPLY_FLAG_X(mod, attrib, Z__B_STREAM_MOD_BG_GREEN, BACKGROUND_GREEN);
APPLY_FLAG_X(mod, attrib, Z__B_STREAM_MOD_BG_BLUE, BACKGROUND_BLUE);
APPLY_FLAG_X(
mod, attrib, Z__B_STREAM_MOD_BG_RED,
BACKGROUND_RED);
APPLY_FLAG_X(
mod, attrib, Z__B_STREAM_MOD_BG_GREEN,
BACKGROUND_GREEN);
APPLY_FLAG_X(
mod, attrib, Z__B_STREAM_MOD_BG_BLUE,
BACKGROUND_BLUE);
}
}
APPLY_FLAG(mod, attrib, Z__B_STREAM_MOD_ULINE, COMMON_LVB_UNDERSCORE);
APPLY_FLAG(mod, attrib, Z__B_STREAM_MOD_INVERT, COMMON_LVB_REVERSE_VIDEO);
APPLY_FLAG(mod, attrib, Z__B_STREAM_MOD_BRIGHT | Z__B_STREAM_MOD_BOLD, FOREGROUND_INTENSITY);
APPLY_FLAG(
mod, attrib, Z__B_STREAM_MOD_BRIGHT | Z__B_STREAM_MOD_BOLD,
FOREGROUND_INTENSITY);
APPLY_FLAG(mod, attrib, Z__B_STREAM_MOD_BG_BRIGHT, BACKGROUND_INTENSITY);
SetConsoleTextAttribute(console, attrib);
return 0;