From e8326d0e9b501f57b15ead04f905dfd76348a7b1 Mon Sep 17 00:00:00 2001 From: SuperNovaa41 Date: Fri, 7 Feb 2025 11:27:42 -0500 Subject: [PATCH] improves the colour changes a bit --- src/hex.c | 25 ++++++++++++++++--------- src/include/hex.h | 12 ++++++++---- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/hex.c b/src/hex.c index 64a54f6..b098dbf 100644 --- a/src/hex.c +++ b/src/hex.c @@ -50,23 +50,30 @@ void convert_text_to_hex(hex_chunk_t* chunk) void display_hex_chunk(hex_chunk_t* chunk, FILE* stream) { uint i, j; + bool newline; - fprintf(stream, "%08x: \x1b[32m", chunk->line * flags.cols); + fprintf(stream, "%08x: %s", chunk->line * flags.cols, GREEN_TEXT_STR); + newline = false; for (i = 0; i < (flags.cols * 2); i += (flags.octets * 2)) { for (j = 0; j < (flags.octets * 2); j += 2) { - if (((chunk->hex + i) + j)[0] == '0' && ((chunk->hex + i) + j)[1] == 'a') - fprintf(stream, "\x1b[33m%2.2s\x1b[32m", chunk->hex + i + j); - else - fprintf(stream, "%2.2s", chunk->hex + i + j); + if (((chunk->hex + i) + j)[0] == '0' && + ((chunk->hex + i) + j)[1] == 'a') + newline = true; + + fprintf(stream, "%s%2.2s%s", (newline ? YELLOW_TEXT_STR : ""), chunk->hex + i + j, (newline ? GREEN_TEXT_STR : "")); + } fprintf(stream, " "); + newline = false; } for (i = 0; i < flags.cols; i++) { if (chunk->text[i] == '\n' || chunk->text[i] == EOF) - fprintf(stream, "\x1b[33m.\x1b[32m"); - else - fprintf(stream, "%c", chunk->text[i]); + newline = true; + + fprintf(stream, "%s%c%s", (newline ? YELLOW_TEXT_STR : ""), + (newline ? '.' : chunk->text[i]), (newline ? GREEN_TEXT_STR : "")); + newline = false; } - fprintf(stream, "\x1b[0m\n"); + fprintf(stream, RESET_TEXT_STR); } diff --git a/src/include/hex.h b/src/include/hex.h index 1659291..4907b97 100644 --- a/src/include/hex.h +++ b/src/include/hex.h @@ -8,6 +8,10 @@ #define TEXT_LINE_LEN 16 #define HEX_LINE_LEN 32 +#define GREEN_TEXT_STR "\x1b[32m" +#define YELLOW_TEXT_STR "\x1b[33m" +#define RESET_TEXT_STR "\x1b[0m\n" + typedef struct { int line; char* text; @@ -17,11 +21,11 @@ typedef struct { struct flags { bool autoskip; // should we condense null lines into * bool binary; // do binary dump instead of hex - uint cols; // choose the amount of columns to display (default 16) + uint cols; // choose the amount of columns to display (default 16) // done - uint octets; // number of octets per line (default 2) - int len; // max len to stop at - bool uppercase; // do uppercase hex chars + uint octets; // number of octets per line (default 2) // done + int len; // max len to stop at //done + bool uppercase; // do uppercase hex chars // done bool decimaloffset; // do decimal offset instead of hex };