Kaynağa Gözat

Working ^CR (Color Restore).

Note: When using stackem/dosemu, the characters
come in 1 or 2 at a time!  Mangling is almost
impossible at that rate with such few characters.

I am thinking of trying to do up processing by
line.  With a very small select wait time, so
I won't buffer things for too long...

But, stackem works/plays.  It didn't get lost.

The menus didn't get lost either.  There would
sometimes be a break between blocks, that
would break the ANSI escape code.  Which would
always end badly if it gets mangled.
(not anymore).
Steve Thielemann 5 yıl önce
ebeveyn
işleme
d794d9d356
1 değiştirilmiş dosya ile 120 ekleme ve 13 silme
  1. 120 13
      mystic.c

+ 120 - 13
mystic.c

@@ -203,9 +203,9 @@ struct console_details {
   int savedx, savedy;
   char ansi[20]; // current ANSI command being processed.
   int in_ansi;
-  int fgcolor; // 0-15
+  int fgcolor; // 0-7 // not 0-15
   int bgcolor; // 0-7
-  int status;  // 0 or 5 (Blink)
+  int status;  // 0, 1 or 5 (Blink)
 } console;
 
 void console_init(struct console_details *cdp) {
@@ -219,6 +219,44 @@ void console_init(struct console_details *cdp) {
   cdp->status = 0;
 }
 
+void ansi_color(int color) {
+  ZF_LOGD("ansi_color(%d)", color);
+  if (color == 0) {
+    console.status = 0;
+    console.fgcolor = 0;
+    console.bgcolor = 0;
+    return;
+  }
+  if (color == 1) {
+    console.status = 1;
+    return;
+  }
+  if (color == 5) {
+    console.status = 5;
+    return;
+  }
+  if ((color >= 30) && (color <= 37)) {
+    console.fgcolor = color - 30;
+    return;
+  }
+  if ((color >= 40) && (color <= 47)) {
+    console.bgcolor = color - 40;
+    return;
+  }
+  ZF_LOGD("ansi_color( %d ) is unknown to me.", color);
+}
+
+const char *color_restore() {
+  static char buffer[30];
+  if (console.status == 0) {
+    sprintf(buffer, "\x1b[0;3%d;4%dm", console.fgcolor, console.bgcolor);
+  } else {
+    sprintf(buffer, "\x1b[0;%d;3%d;4%dm", console.status, console.fgcolor,
+            console.bgcolor);
+  };
+  return buffer;
+}
+
 void console_ansi(const char *ansi) {
   int understood = 0;
   const char *cp = ansi;
@@ -381,6 +419,41 @@ void console_ansi(const char *ansi) {
         understood = 1;
         break;
 
+      case 'm':
+        // color
+        if (cp == last) {
+          // nothing given, default to 0.
+          number = 0;
+          ansi_color(number);
+        } else {
+          while (cp != last) {
+            number = atoi(cp);
+            ansi_color(number);
+            cp++;
+
+            while ((cp != last) && (isdigit(*cp))) {
+              cp++;
+            };
+
+            if (cp != last) {
+              if (*cp == ';') {
+                cp++;
+              }
+            }
+          }
+        }
+        understood = 1;
+        break;
+
+      case 't':
+      case 'r':
+      case 'h':
+      case '!':
+        // These are ones that I don't care about.
+      case 'n': // This is terminal detect -- give me cursor position
+        understood = 1;
+        break;
+
       default:
         // unsure -- possibly not important
         ZF_LOGD("console_ansi( %s ): ???", repr(ansi));
@@ -394,7 +467,11 @@ void console_ansi(const char *ansi) {
   }
 }
 
-void console_char(char ch) {
+/*
+ * console_char()
+ *  return whether or not we are still in_ansi
+ */
+int console_char(char ch) {
   char *cp;
 
   if (console.in_ansi) {
@@ -408,8 +485,9 @@ void console_char(char ch) {
       console_ansi(console.ansi);
       console.in_ansi = 0;
       console.ansi[0] = 0;
-      return;
+      return 1;
     }
+    return 1;
   } else {
     if (ch == '\x1b') {
       cp = console.ansi;
@@ -417,31 +495,31 @@ void console_char(char ch) {
       cp++;
       *cp = 0;
       console.in_ansi = 1;
-      return;
+      return 1;
     }
     if (ch == '\r') {
       // Carriage return
       console.posx = 0;
-      return;
+      return 0;
     }
     if (ch == '\n') {
       console.posy++;
       // check range/"scroll"
-      return;
+      return 0;
     }
     if (ch == '\b') {
       // Backspace.
       if (console.posx > 0) {
         console.posx--;
       }
-      return;
+      return 0;
     }
     if (ch == '\f') {
       // form feed
       // treat as clear screen
       console.posx = 0;
       console.posy = 0;
-      return;
+      return 0;
     }
 
     /*
@@ -456,6 +534,7 @@ void console_char(char ch) {
       console.posy++;
       // check range/"scroll"
     }
+    return 0;
   }
 }
 
@@ -560,7 +639,7 @@ void write_color(int fd, int color) {
   case 13:
   case 14:
   case 15:
-    sprintf(buffer, "\x1b[1;3%dm", MYSTIC[color - 8]);
+    sprintf(buffer, "\x1b[0;1;3%dm", MYSTIC[color - 8]);
     break;
   case 16:
   case 17:
@@ -629,6 +708,12 @@ const char *process_trigger(int fd, const char *cp) {
 
   case 'C':
     i = 0;
+    if (*cp == 'R') {
+      cp++;
+      const char *restore = color_restore();
+      write(fd, restore, strlen(restore));
+      break;
+    }
     if (isdigit(*cp)) {
       i = (*cp) - '0';
       cp++;
@@ -906,7 +991,7 @@ void harry_event(int fd) {
   if (color == 0) {
     color++;
   } // If it's 0 let's make it 1.
-  sprintf(buffer, "^S2^C%02d%s^P2^D%02d", color, cp, (int)strlen(cp));
+  sprintf(buffer, "^S2^C%02d%s^P2^CR^D%02d", color, cp, (int)strlen(cp));
 
   ZF_LOGD("harry_event: render(%d, \"%s\")", fd, buffer);
 
@@ -915,7 +1000,9 @@ void harry_event(int fd) {
 
 void init_harry() {
   init_have_seen(last_seen_harry_event, MAX_HARRY_EVENT_DUPS);
-  ZF_LOGD("init => %d %d", last_seen_harry_event[0], last_seen_harry_event[1]);
+  // ZF_LOGD("init => %d %d", last_seen_harry_event[0],
+  // last_seen_harry_event[1]);
+  console_init(&console);
 }
 
 /*
@@ -1195,8 +1282,10 @@ int mangle(int fd, char *buffer) {
   // or possibly transpose words
   char work[(BSIZE * 4) + 1];
 
+  // Use terminal - clean out ANSI
+
   ZF_LOGI("mangle(%s)", repr(buffer));
-  strcpy(work, buffer);
+  // strcpy(work, buffer);
 
   /*
   NOTE:  We copy the buffer, so we can clear out ANSI codes, etc.
@@ -1204,6 +1293,8 @@ int mangle(int fd, char *buffer) {
   process.
   */
 
+#ifdef NO_NOT_YET
+
   /*
    (random) Look for ANSI CLS and:
 
@@ -1267,6 +1358,21 @@ int mangle(int fd, char *buffer) {
     };
   }
 
+#endif
+
+  strcpy(work, buffer); // sure.
+
+  for (x = 0; x < strlen(buffer); x++) {
+    int ansi = console_char(buffer[x]);
+    if (ansi) {
+      work[x] = ' ';
+    }
+  }
+
+  ZF_LOGD("Work Now: [%s]", repr(work));
+
+#ifdef OLD_WAY
+
   /* work -- clear out ANSI so we don't mangle ANSI codes. */
   x = rx_match(&ANSI, work);
   char replace_with = ' ';
@@ -1278,6 +1384,7 @@ int mangle(int fd, char *buffer) {
     };
     ZF_LOGD("Work Now: [%s]", repr(work));
   }
+#endif
 
   // ZF_LOGI("mangle: %s", repr(work));