Re: on the fly ansi codes

From: Michael Buselli (mhbusell@midway.uchicago.edu)
Date: 03/13/95


Okay, here's what I've done for on the fly ansi codes.  I call this
function from process output, and I've been using the codes in my main
menu, object short descriptions, and player titles, mostly.  I put this
function in comm.c, which doesn't include "screen.h" in standard circle,
so you'll need to add it in as well as add this dependancy to your
Makefile.

Feel free to change the special characters that are used for escaping to
the color codes.  It works by using a dollar sign followed by the first
letter of the color.  This code also uses backslashes to make a sort of
"verbatim" mode, which will show everything as (ignore color codes) is to
the end of the line unless another backslash is used.  To print a 
backslash, just enter it double.

So below is the part of the code in process_output where the call to 
strip_color_codes is made, and below that is the definition of 
strip_color_codes itself.

I hope this solves your color problems. :)

Michael Buselli
mhbusell@midway.uchicago.edu

(Note: I made some conversion of the code below from what I actually use
so that it could work in standard circle again.  If you have any problems
you can't figure out, please tell me.)

============================
  /* add the extra CRLF if the person isn't in compact mode */
  if (!t->connected && t->character && !PRF_FLAGGED(t->character, PRF_COMPACT))
    strcat(i + 2, "\r\n");

  /* here is where we convert color codes into actual color */
  if (t->character)
    strip_color_codes(i + 2, t->character);

  /*
   * now, send the output.  If this is an 'interruption', use the prepended
   * CRLF, otherwise send the straight output sans CRLF.
   */
=============================
 /*
 * All instances of $N are changed to the color represented by N unless
 * enclosed by backslashes.  Code added by Michael Buselli.
 */
void strip_color_codes(char *cbuf, struct char_data * ch)
{
  static char temp[LARGE_BUFSIZE + GARBAGE_SPACE];
  char verbatim = 0, is_color = 0;
  char pr_char = '\0';
  char *bbuf;

  bbuf = cbuf;
  for (*temp = '\0'; *cbuf; pr_char = *(cbuf++)) {
    if (*cbuf == '\\') {   /* enter and exit "verbatim" mode */
      verbatim = !verbatim;
      if (pr_char == *cbuf) {
        sprintf(temp, "%s%c", temp, *cbuf);
        *cbuf = pr_char + 1;  /* This line is to get *cbuf != pr_char */
      }
    }
    else if (!verbatim && *cbuf == '$') {
      cbuf++;
      switch (*cbuf) {
      case 'N':
        strcat(temp, CCNRM(ch, C_NRM));
        is_color = 0;
        break;
      case 'R':
        strcat(temp, CCRED(ch, C_NRM));
        is_color = 1;
        break;
      case 'G':
        strcat(temp, CCGRN(ch, C_NRM));
        is_color = 1;
        break;
      case 'Y':
        strcat(temp, CCYEL(ch, C_NRM));
        is_color = 1;
        break;
      case 'B':
        strcat(temp, CCBLU(ch, C_NRM));
        is_color = 1;
        break;
      case 'M':
        strcat(temp, CCMAG(ch, C_NRM));
        is_color = 1;
        break;
      case 'C':
        strcat(temp, CCCYN(ch, C_NRM));
        is_color = 1;
        break;
      case 'W':
        strcat(temp, CCWHT(ch, C_NRM));
        is_color = 1;
        break;
      default:
        cbuf--;
        sprintf(temp, "%s%c", temp, *cbuf);
      }
    }
    else if (is_color && *cbuf == '\n') {
      verbatim = 0; is_color = 0;
      sprintf(temp, "%s\n%s", temp, CCNRM(ch, C_NRM));
    }
    else sprintf(temp, "%s%c", temp, *cbuf);
  }

  strcpy(bbuf, temp);
}



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