Completely nail inverse video
This commit is contained in:
parent
e1e296f8ee
commit
bd5268cd62
1 changed files with 34 additions and 12 deletions
46
main.c
46
main.c
|
@ -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,18 +110,26 @@ 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];
|
|
||||||
|
|
||||||
size_t len = utf8_encode(sequence, codepoint);
|
|
||||||
|
|
||||||
/* XXX: Implement support for inverse colours */
|
|
||||||
if (fwrite(sequence, len, 1, stdout) < 1) {
|
|
||||||
goto error_io;
|
goto error_io;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (putchar('.') < 0) {
|
if (c >= 0xa0 && c <= 0xbf) {
|
||||||
goto error_io;
|
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,
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue