On Wed, Sep 21, 2005 at 05:46:55PM +0200, Florian Ernst wrote: > On Wed, Sep 21, 2005 at 03:19:46PM +0200, Yuri D'Elia wrote: > > Yes, I saw the patch, I know it's not your fault, but enabling > > Oh, in a way it is, as I forwarded the corresponding wishlist bug to > the upstream maintainer... ;) > > > features conditionally is really poor programming. What if I provide > > a patch that adds a "--color" switch instead, like most other utilities? > > That, of course, would be the most splendid solution.
So here's the patch-o-splendor. Incidentally, this patch also fixes "hexedit --". Upstream cc'ed.
diff -rud hexedit-1.2.11/configure.in hexedit/configure.in --- hexedit-1.2.11/configure.in 2005-09-20 13:39:19.000000000 +0200 +++ hexedit/configure.in 2005-09-21 23:29:01.328032000 +0200 @@ -21,11 +21,6 @@ AC_SUBST(SRCS) AC_SUBST(OTHER) -AC_ARG_ENABLE(colors, - [ --enable-colors enable colors (fruit salad)], - AC_DEFINE(ENABLE_COLORS,1,[Define if you want a colored (fruit salad) display]) -) - dnl Enable GNU extensions on systems that have them. AH_VERBATIM([_GNU_SOURCE], [/* Enable GNU extensions on systems that have them. */ diff -rud hexedit-1.2.11/display.c hexedit/display.c --- hexedit-1.2.11/display.c 2005-09-20 14:36:29.000000000 +0200 +++ hexedit/display.c 2005-09-21 23:50:37.847911388 +0200 @@ -52,7 +52,7 @@ int move_base(INT delta) { - if (option == bySector) { + if (mode == bySector) { if (delta > 0 && delta < page) delta = page; else if (delta < 0 && delta > -page) @@ -69,7 +69,7 @@ base = loc; readFile(); - if (option != bySector && nbBytes < page - lineLength && base != 0) { + if (mode != bySector && nbBytes < page - lineLength && base != 0) { base -= myfloor(page - nbBytes - lineLength, lineLength); if (base < 0) base = 0; readFile(); @@ -103,29 +103,31 @@ void initCurses(void) { initscr(); -#ifdef ENABLE_COLORS - start_color(); - use_default_colors(); - init_pair(1, COLOR_RED, -1); /* null zeros */ - init_pair(2, COLOR_GREEN, -1); /* control chars */ - init_pair(3, COLOR_BLUE, -1); /* extended chars */ -#endif + + if (colored) { + start_color(); + use_default_colors(); + init_pair(1, COLOR_RED, -1); /* null zeros */ + init_pair(2, COLOR_GREEN, -1); /* control chars */ + init_pair(3, COLOR_BLUE, -1); /* extended chars */ + } + refresh(); raw(); noecho(); keypad(stdscr, TRUE); - if (option == bySector) { - lineLength = options[bySector].lineLength; - page = options[bySector].page; + if (mode == bySector) { + lineLength = modes[bySector].lineLength; + page = modes[bySector].page; page = myfloor((LINES - 1) * lineLength, page); - blocSize = options[bySector].blocSize; + blocSize = modes[bySector].blocSize; if (computeLineSize() > COLS) DIE("%s: term is too small for sectored view (width)\n"); if (page == 0) DIE("%s: term is too small for sectored view (height)\n"); - } else { /* option == maximized */ + } else { /* mode == maximized */ if (LINES <= 4) DIE("%s: term is too small (height)\n"); - blocSize = options[maximized].blocSize; + blocSize = modes[maximized].blocSize; for (lineLength = blocSize; computeLineSize() <= COLS; lineLength += blocSize); lineLength -= blocSize; if (lineLength == 0) DIE("%s: term is too small (width)\n"); @@ -170,26 +172,31 @@ else i = '-'; printw("-%c%c %s --0x%llX", i, i, baseName, base + cursor); if (MAX(fileSize, lastEditedLoc)) printw("/0x%llX", getfilesize()); - if (option == bySector) printw("--sector %d", (base + cursor) / SECTOR_SIZE); + if (mode == bySector) printw("--sector %d", (base + cursor) / SECTOR_SIZE); move(cursor / lineLength, computeCursorXCurrentPos()); } void displayLine(int offset, int max) { - int i; + int i, attr; PRINTW(("%08lX ", (int) (base + offset))); for (i = offset; i < offset + lineLength; i++) { if (i > offset) MAXATTRPRINTW(bufferAttr[i] & MARKED, (((i - offset) % blocSize) ? " " : " ")); if (i < max) { - ATTRPRINTW( -#ifdef ENABLE_COLORS - (buffer[i] == 0 ? COLOR_PAIR(1) : - buffer[i] < ' ' ? COLOR_PAIR(2) : - buffer[i] >= 127 ? COLOR_PAIR(3) : 0) | -#endif - bufferAttr[i], ("%02X", buffer[i])); + attr = bufferAttr[i]; + + if (colored) { + if (buffer[i] == 0) + attr |= COLOR_PAIR(1); + else if (buffer[i] < ' ') + attr |= COLOR_PAIR(2); + else if (buffer[i] >= 127) + attr |= COLOR_PAIR(3); + } + + ATTRPRINTW(attr, ("%02X", buffer[i])); } else PRINTW((" ")); } diff -rud hexedit-1.2.11/hexedit.1 hexedit/hexedit.1 --- hexedit-1.2.11/hexedit.1 2005-09-20 14:49:03.000000000 +0200 +++ hexedit/hexedit.1 2005-09-22 00:23:36.399951181 +0200 @@ -3,7 +3,7 @@ hexedit \- view and edit files in hexadecimal or in ASCII .SH SYNOPSIS .I hexedit -[\-s | \-\-sector] [\-m | \-\-maximize] [\-h | \-\-help] [filename] +[\-s | \-\-sector] [\-m | \-\-maximize] [\-\-color] [\-h | \-\-help] [filename] .SH DESCRIPTION .LP .I hexedit @@ -17,6 +17,9 @@ .I "\-m, \-\-maximize" Try to maximize the display. .TP +.I "\-\-color" +Enable the patent pending Fruit-Salad(tm) color mode. +.TP .I "\-h, \-\-help" Show the usage. .SH COMMANDS (quickly) diff -rud hexedit-1.2.11/hexedit.c hexedit/hexedit.c --- hexedit-1.2.11/hexedit.c 2004-01-20 10:25:58.000000000 +0100 +++ hexedit/hexedit.c 2005-09-22 00:11:25.291544981 +0200 @@ -34,12 +34,12 @@ char *lastFindFile = NULL, *lastYankToAFile = NULL, *lastAskHexString = NULL, *lastAskAsciiString = NULL, *lastFillWithStringHexa = NULL, *lastFillWithStringAscii = NULL; -optionParams options[LAST] = { - { 8, 16, 256, "-s", "--sector" }, - { 4, 0, 0, "-m", "--maximize" }, - { 0, 0, 0, "-h", "--help" }, +modeParams modes[LAST] = { + { 8, 16, 256 }, + { 4, 0, 0 }, }; -optionType option = maximized; +modeType mode = maximized; +int colored = FALSE; /*******************************************************************************/ @@ -53,16 +53,18 @@ for (; argc > 0; argv++, argc--) { - recognized = FALSE; - for (i = 0; !recognized && i < LAST; i++) { - if (streq(*argv, options[i].shortOptionName) || - streq(*argv, options[i].longOptionName)) { - if (i == helpOption) DIE(usage); - option = i; - recognized = TRUE; - } - } - if (!recognized) break; + if (streq(*argv, "-s") || streq(*argv, "--sector")) + mode = bySector; + else if (streq(*argv, "-m") || streq(*argv, "--maximize")) + mode = maximized; + else if (streq(*argv, "--color")) + colored = TRUE; + else if (streq(*argv, "--")) { + argv++; argc--; + break; + } else if (*argv[0] == '-') + DIE(usage) + else break; } if (argc > 1) DIE(usage); diff -rud hexedit-1.2.11/hexedit.h hexedit/hexedit.h --- hexedit-1.2.11/hexedit.h 2005-09-20 14:36:48.000000000 +0200 +++ hexedit/hexedit.h 2005-09-21 23:26:02.287924000 +0200 @@ -58,16 +58,18 @@ /*******************************************************************************/ /* Configuration parameters */ /*******************************************************************************/ -typedef enum { bySector, maximized, helpOption, LAST } optionType; +typedef enum { bySector, maximized, LAST } modeType; typedef struct { int blocSize, lineLength, page; - char *shortOptionName, *longOptionName; -} optionParams; +} modeParams; -extern optionParams options[LAST]; -extern optionType option; +extern modeParams modes[LAST]; +extern modeType mode; +extern int colored; -#define usage "usage: %s [-s | --sector] [-m | --maximize] [-h | --help] filename\n" +#define usage "usage: %s [-s | --sector] [-m | --maximize] [--color] [-h | --help] filename\n" + +#define usage "usage: %s [-s | --sector] [-m | --maximize] [--color] [-h | --help] filename\n" #define pressAnyKey "(press any key)" diff -rud hexedit-1.2.11/interact.c hexedit/interact.c --- hexedit-1.2.11/interact.c 2005-09-20 14:57:11.000000000 +0200 +++ hexedit/interact.c 2005-09-21 23:50:59.275645129 +0200 @@ -112,7 +112,7 @@ { INT s = getfilesize(); cursorOffset = 0; - if (option == bySector) set_base(myfloor(s, page)); + if (mode == bySector) set_base(myfloor(s, page)); set_cursor(s); } @@ -449,7 +449,7 @@ case '\n': case '\r': case KEY_ENTER: - if (option == bySector) goto_sector(); else goto_char(); + if (mode == bySector) goto_sector(); else goto_char(); break; case CTRL('W'):