[code] [update] My prompt patch's inline color system...

From: Daniel Koepke (dkoepke@california.com)
Date: 03/02/97


For those of you using my improved prompt patch and/or my inline
color system, this will be of some minor signifigance to you. I
posted a while back that I had a few ideas for cutting down the
length of the ansi codes produced by the interpret_colors() inline
color parser. Well, I believe I have suceeded to one extent or
another.

Originally, each code would be 10bytes long at the MINIMUM. Most
likely they'd be 12bytes. Not good. After the revision the minimum
length is 3 bytes and the maximum length is still 12 bytes (although
mostly you'll see 5-8byte codes). Here goes the updated code, only
sparsely tested:

  // replacement for is_color() routine
  int get_color(char c) {
    if (c >= '0' && c <= '7')
      return ((c-'0')+40);

    switch (c) {
    case 'x': // x and l stand for black
    case 'l': return 30; break;
    case 'r': return 31; break;
    case 'g': return 32; break;
    case 'y': return 33; break;
    case 'b': return 34; break;
    case 'p': // p and m stand for magenta (purple)
    case 'm': return 35; break;
    case 'c': return 36; break;
    case 'w': return 37; break;
    default : return  0; break;
    }
  }

  char *interpret_colors(char *str, bool parse) {
    bool flash = 0, kill_flash = 0, show_bg = 0, show_flash = 0;
    int clr=37, bg_clr=40, low = -1, lclr = -1, lbg=bg_clr;
    static char cbuf[MAX_STRING_LENGTH];
    register char *cp, *tmp;
    char i[256];

    if (!strchr(str, '&'))
      return (str);

    cp = cbuf;

    for (;;) {
      if (*str == '&') {
        str++;

        if (*str == '&') {
          *(cp++) = '&';
          str++;
          continue;
        } else if (*str == 'f') {
          show_flash = flash = !flash;
          if (!flash) kill_flash = TRUE;
          str++;
          continue;
        } else if ((clr = get_color(LOWER(*str))) > 0 && parse) {
          strcpy(i, "\x1b[");

          // now it starts to get weird so we can cut down on the
          // size of the produced ansi codes
          if (islower(*str) && !low)
            kill_flash = FALSE;
          else if (kill_flash) {
            strcat(i, "0;");
            if (show_bg) sprintf(i, "%s%d;", i, bg_clr);
            kill_flash = FALSE;
          }

          if (isupper(*str) && low) {
            strcat(i, "1");
            low = 0;
          } else if (islower(*str) && low <= 0) {
            strcat(i, "0");
            low = 1;
            show_flash = flash;
            show_bg = TRUE;
          }

          if (clr >= 40) {
            if (lbg != clr) show_bg = TRUE;
            lbg = bg_clr;
            bg_clr = clr;
            str++;
            continue;
          }

          if (show_bg) {
            sprintf(i, "%s;%d", i, bg_clr);
            show_bg = FALSE;
          }

          if (show_flash) {
            strcat(i, ";5");
            show_flash = FALSE;
          }

          if (clr == lcr && i[strlen(i)-1] != ';') strcat(i, "m");
          else sprintf(i, "%s%s%dm", i, (i[strlen(i)-1]=='['?"":";"), clr);
          tmp = i;
        } else {
          str++;
          continue;
        }
        while ((*cp = *(tmp++)))
          cp++;
        str++;
      } else if (!(*(cp++) = *(str++)))
        break;
    }

    *cp = '\0';
    return (cbuf);
  }

There... :) Okay, for the cool things:

Given the following passed to interpret_codes:
 &rHello &Rworld
In the old version this would have produced (20): 
 \x1b[0;40;31m, \x1b[1;40;31m
In this new version...(10):
 \x1b[0;31m, \x1b[1m

Given the following passed to interpret_colors:
 &rHi, &wBob, &bhow &Bare &Wyou &Mtoday?
In the old version this would have produced (60!):
  \x1b[0;40;31m, \x1b[0;40;37m, \x1b[0;40;34m, \x1b[1;40;34m,
  \x1b[1;40;37m, \x1b[1;40;35m
In this new version...(30):
  \x1b[0;31m, \x1b[37m, \x1b[34m, \x1b[1m, \x1b[37m, \x1b[35m


As you can see, the new version is producing codes that are
smaller by *half*. Thus strings with a lot of color codes in them
aren't so large (so your players can stuff even more color codes
into them ;P).


--
Daniel Koepke
dkoepke@california.com
Forgive me father, for I am sin.


+-----------------------------------------------------------+
| Ensure that you have read the CircleMUD Mailing List FAQ: |
|   http://cspo.queensu.ca/~fletcher/Circle/list_faq.html   |
|    Or send 'info circle' to majordomo@cspo.queensu.ca     |
+-----------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/18/00 PST