From bd5268cd628d75dcf4214e7ee40f23904964baef Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Sun, 10 Sep 2023 23:34:52 -0400 Subject: Completely nail inverse video --- main.c | 46 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/main.c b/main.c index cf258c1..72a7d09 100644 --- a/main.c +++ b/main.c @@ -12,7 +12,7 @@ #define ZXDUMP_STRIDE_GROUP 2 #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, 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; if (printf("%08zx: ", offset) < 0) { @@ -96,18 +110,26 @@ static ssize_t dump_line(off_t offset, void *buf, size_t len) { uint8_t c = ((uint8_t *)buf)[offset+i]; if (c < ZXDUMP_CHARSET_LEN) { - uint16_t codepoint = charset[c]; - uint8_t sequence[4]; - - size_t len = utf8_encode(sequence, codepoint); - - /* XXX: Implement support for inverse colours */ - if (fwrite(sequence, len, 1, stdout) < 1) { + if (zx_putchar(c) < 0) { goto error_io; } } else { - if (putchar('.') < 0) { - goto error_io; + if (c >= 0xa0 && c <= 0xbf) { + if (tty && printf("\033[7m") < 0) { + goto error_io; + } + + if (zx_putchar(c - 0xa0) < 0) { + goto error_io; + } + + if (tty && printf("\033[27m") < 0) { + goto error_io; + } + } else { + if (putchar('.') < 0) { + goto error_io; + } } } } @@ -148,7 +170,7 @@ static ssize_t dump_fd(int fd) { size_t left = len - i, 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; } -- cgit v1.2.3