Completely nail inverse video

This commit is contained in:
XANTRONIX Development 2023-09-10 23:34:52 -04:00
parent e1e296f8ee
commit bd5268cd62

38
main.c
View file

@ -12,7 +12,7 @@
#define ZXDUMP_STRIDE_GROUP 2 #define ZXDUMP_STRIDE_GROUP 2
#define ZXDUMP_CHARSET_LEN 64 #define ZXDUMP_CHARSET_LEN 64
static uint16_t charset[ZXDUMP_CHARSET_LEN] = { static uint32_t zx_charset[ZXDUMP_CHARSET_LEN] = {
0x0020, 0x2598, 0x259d, 0x2580, 0x2596, 0x258c, 0x259e, 0x259b, 0x0020, 0x2598, 0x259d, 0x2580, 0x2596, 0x258c, 0x259e, 0x259b,
0x2592, '.', '.', '"', 0x00a3, '$', ':', '?', 0x2592, '.', '.', '"', 0x00a3, '$', ':', '?',
'(', ')', '>', '<', '=', '+', '-', '*', '(', ')', '>', '<', '=', '+', '-', '*',
@ -63,7 +63,21 @@ static inline size_t utf8_encode(uint8_t *buf, uint32_t codepoint) {
} }
} }
static ssize_t dump_line(off_t offset, void *buf, size_t len) { static inline int zx_putchar(uint8_t c) {
uint8_t sequence[4];
size_t len = utf8_encode(sequence, zx_charset[c]);
if (fwrite(sequence, len, 1, stdout) < 1) {
goto error_io;
}
return 0;
error_io:
return -1;
}
static ssize_t dump_line(off_t offset, void *buf, size_t len, int tty) {
size_t i; size_t i;
if (printf("%08zx: ", offset) < 0) { if (printf("%08zx: ", offset) < 0) {
@ -96,13 +110,20 @@ static ssize_t dump_line(off_t offset, void *buf, size_t len) {
uint8_t c = ((uint8_t *)buf)[offset+i]; uint8_t c = ((uint8_t *)buf)[offset+i];
if (c < ZXDUMP_CHARSET_LEN) { if (c < ZXDUMP_CHARSET_LEN) {
uint16_t codepoint = charset[c]; if (zx_putchar(c) < 0) {
uint8_t sequence[4]; goto error_io;
}
} else {
if (c >= 0xa0 && c <= 0xbf) {
if (tty && printf("\033[7m") < 0) {
goto error_io;
}
size_t len = utf8_encode(sequence, codepoint); if (zx_putchar(c - 0xa0) < 0) {
goto error_io;
}
/* XXX: Implement support for inverse colours */ if (tty && printf("\033[27m") < 0) {
if (fwrite(sequence, len, 1, stdout) < 1) {
goto error_io; goto error_io;
} }
} else { } else {
@ -111,6 +132,7 @@ static ssize_t dump_line(off_t offset, void *buf, size_t len) {
} }
} }
} }
}
if (putchar('\n') < 0) { if (putchar('\n') < 0) {
goto error_io; goto error_io;
@ -148,7 +170,7 @@ static ssize_t dump_fd(int fd) {
size_t left = len - i, size_t left = len - i,
linesz = left < ZXDUMP_STRIDE_LINE? left: ZXDUMP_STRIDE_LINE; linesz = left < ZXDUMP_STRIDE_LINE? left: ZXDUMP_STRIDE_LINE;
if (dump_line(offset, buf, linesz) < 0) { if (dump_line(offset, buf, linesz, isatty(fileno(stdout))) < 0) {
goto error_dump_line; goto error_dump_line;
} }