2024-11-14 23:11:55 +00:00
|
|
|
#include "../../print.h"
|
|
|
|
|
|
|
|
|
|
#include <Windows.h>
|
2024-11-14 16:56:12 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
int z__b_stream_is_tty(FILE *fp)
|
|
|
|
|
{
|
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO csbi;
|
|
|
|
|
HANDLE console = (HANDLE)_get_osfhandle(fileno(fp));
|
|
|
|
|
BOOL status = GetConsoleScreenBufferInfo(console, &csbi);
|
|
|
|
|
|
|
|
|
|
return status == TRUE ? 1 : 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int z__b_stream_dimensions(FILE *fp, unsigned int *w, unsigned int *h)
|
|
|
|
|
{
|
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO csbi;
|
|
|
|
|
HANDLE console = (HANDLE)_get_osfhandle(fileno(fp));
|
|
|
|
|
BOOL status = GetConsoleScreenBufferInfo(console, &csbi);
|
|
|
|
|
|
|
|
|
|
if (status == FALSE) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (w) {
|
2024-11-14 22:01:40 +00:00
|
|
|
*w = csbi.srWindow.Right - csbi.srWindow.Left;
|
2024-11-14 16:56:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (h) {
|
2024-11-14 22:01:40 +00:00
|
|
|
*h = csbi.srWindow.Bottom - csbi.srWindow.Top;
|
2024-11-14 16:56:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (x) {
|
|
|
|
|
*x = csbi.dwCursorPosition.X;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (y) {
|
|
|
|
|
*y = csbi.dwCursorPosition.Y;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2024-11-14 22:01:40 +00:00
|
|
|
|
2024-11-14 23:11:55 +00:00
|
|
|
#define APPLY_FLAG(mod, attrib, x, y) \
|
2024-11-14 22:01:40 +00:00
|
|
|
if ((mod) & (x)) { \
|
|
|
|
|
(attrib) |= (y); \
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-14 23:11:55 +00:00
|
|
|
#define APPLY_FLAG_X(mod, attrib, x, y) \
|
|
|
|
|
if ((mod) & (x)) { \
|
|
|
|
|
(attrib) |= (y); \
|
|
|
|
|
} else { \
|
|
|
|
|
(attrib) &= ~(y); \
|
2024-11-14 22:01:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
2024-11-14 23:11:55 +00:00
|
|
|
|
2024-11-14 22:01:40 +00:00
|
|
|
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) {
|
2024-11-14 23:11:55 +00:00
|
|
|
attrib
|
|
|
|
|
&= ~(FOREGROUND_RED | FOREGROUND_GREEN
|
|
|
|
|
| FOREGROUND_BLUE);
|
2024-11-14 22:01:40 +00:00
|
|
|
} else {
|
2024-11-14 23:11:55 +00:00
|
|
|
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);
|
2024-11-14 22:01:40 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Z__B_STREAM_MOD_GET_BG_COLOUR(mod) != 0) {
|
|
|
|
|
if (mod & Z__B_STREAM_MOD_BG_BLACK) {
|
2024-11-14 23:11:55 +00:00
|
|
|
attrib
|
|
|
|
|
&= ~(BACKGROUND_RED | BACKGROUND_GREEN
|
|
|
|
|
| BACKGROUND_BLUE);
|
2024-11-14 22:01:40 +00:00
|
|
|
} else {
|
2024-11-14 23:11:55 +00:00
|
|
|
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);
|
2024-11-14 22:01:40 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
2024-11-14 23:11:55 +00:00
|
|
|
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);
|
2024-11-14 22:01:40 +00:00
|
|
|
|
|
|
|
|
SetConsoleTextAttribute(console, attrib);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|