term: implement platform-independent console text colour/formatting
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <Windows.h>
|
||||
#include "../../print.h"
|
||||
|
||||
int z__b_stream_is_tty(FILE *fp)
|
||||
{
|
||||
@@ -22,11 +23,11 @@ int z__b_stream_dimensions(FILE *fp, unsigned int *w, unsigned int *h)
|
||||
}
|
||||
|
||||
if (w) {
|
||||
*w = csbi.dwMaximumWindowSize.X;
|
||||
*w = csbi.srWindow.Right - csbi.srWindow.Left;
|
||||
}
|
||||
|
||||
if (h) {
|
||||
*h = csbi.dwMaximumWindowSize.Y;
|
||||
*h = csbi.srWindow.Bottom - csbi.srWindow.Top;
|
||||
}
|
||||
|
||||
return 0;
|
||||
@@ -37,6 +38,7 @@ int z__b_stream_cursorpos(FILE *in, FILE *out, unsigned int *x, unsigned int *y)
|
||||
CONSOLE_SCREEN_BUFFER_INFO csbi;
|
||||
HANDLE console = (HANDLE)_get_osfhandle(fileno(in));
|
||||
BOOL status = GetConsoleScreenBufferInfo(console, &csbi);
|
||||
|
||||
|
||||
if (status == FALSE) {
|
||||
return -1;
|
||||
@@ -52,3 +54,63 @@ int z__b_stream_cursorpos(FILE *in, FILE *out, unsigned int *x, unsigned int *y)
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define APPLY_FLAG(mod, attrib, x, y) \
|
||||
if ((mod) & (x)) { \
|
||||
(attrib) |= (y); \
|
||||
}
|
||||
|
||||
#define APPLY_FLAG_X(mod, attrib, x, y) \
|
||||
if ((mod) & (x)) { \
|
||||
(attrib) |= (y); \
|
||||
} else { \
|
||||
(attrib) &= ~(y); \
|
||||
}
|
||||
|
||||
int z__b_stream_set_modifier(FILE *fp, unsigned int mod)
|
||||
{
|
||||
WORD attrib = 0;
|
||||
CONSOLE_SCREEN_BUFFER_INFO csbi;
|
||||
|
||||
HANDLE console = (HANDLE)_get_osfhandle(fileno(fp));
|
||||
BOOL status = GetConsoleScreenBufferInfo(console, &csbi);
|
||||
|
||||
if (status == FALSE) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
attrib = csbi.wAttributes;
|
||||
|
||||
if (mod & Z__B_STREAM_MOD_RESET) {
|
||||
attrib = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
|
||||
SetConsoleTextAttribute(console, attrib);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (Z__B_STREAM_MOD_GET_FG_COLOUR(mod) != 0) {
|
||||
if (mod & Z__B_STREAM_MOD_BLACK) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
} 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(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);
|
||||
|
||||
SetConsoleTextAttribute(console, attrib);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user