From 495bedd37f60064a0b67e6a481ea6e7576555e58 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sun, 16 May 2021 20:21:14 -0500 Subject: Add black and white mode (x to toggle) --- toys/other/hexedit.c | 52 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 18 deletions(-) (limited to 'toys') diff --git a/toys/other/hexedit.c b/toys/other/hexedit.c index 398ec15d..4124c779 100644 --- a/toys/other/hexedit.c +++ b/toys/other/hexedit.c @@ -24,6 +24,7 @@ config HEXEDIT ^J or : Jump (+/- for relative offset, otherwise absolute address) ^F or / Find string (^G/n: next, ^D/p: previous match) u Undo + x Toggle bw/color display q/^C/^Q/Esc Quit */ @@ -31,14 +32,10 @@ config HEXEDIT #include "toys.h" GLOBALS( - char *data; - long long len, base; - int numlen, undo, undolen; + char *data, *search, keybuf[16], input[80]; + long long len, base, pos; + int numlen, undo, undolen, mode; unsigned rows, cols; - long long pos; - char keybuf[16]; - char input[80]; - char *search; ) #define UNDO_LEN (sizeof(toybuf)/(sizeof(long long)+1)) @@ -71,13 +68,10 @@ static int prompt(char *prompt, char *initial_value) break; } - if (key == 0x7f) { - if (len > 0) TT.input[--len] = 0; - } else if (key == 'U'-'@') { - while (len > 0) TT.input[--len] = 0; - } else if (key >= ' ' && key < 0x7f && len < sizeof(TT.input)) { + if (key == 0x7f && (len > 0)) TT.input[--len] = 0; + else if (key == 'U'-'@') while (len > 0) TT.input[--len] = 0; + else if (key >= ' ' && key < 0x7f && len < sizeof(TT.input)) TT.input[len++] = key; - } } tty_esc("?25l"); @@ -87,12 +81,27 @@ static int prompt(char *prompt, char *initial_value) // Render all characters printable, using color to distinguish. static void draw_char(int ch) { - if (ch >= ' ' && ch < 0x7f) putchar(ch); - else { + if (ch >= ' ' && ch < 0x7f) { + putchar(ch); + return; + } + + if (TT.mode) { + if (ch>127) { + tty_esc("2m"); + ch &= 127; + } + if (ch<32 || ch==127) { + tty_esc("7m"); + if (ch==127) ch = 32; + else ch += 64; + } + xputc(ch); + } else { if (ch < ' ') printf("\e[31m%c", ch + '@'); - else printf("\e[35m?"); + else tty_esc("35m?"); } - printf("\e[0m"); + tty_esc("0m"); } static void draw_status(void) @@ -121,7 +130,7 @@ static void draw_line(long long yy) if (yy+xx>=TT.len) xx = TT.len-yy; if (yy