Kaynağa Gözat

"Working" write_color.

Working in the sense that it outputs proper colors
^Cnn:  0-7, 8-15, 16-23, 24-31

Which match what Mystic says you should get from these
numbers.  (Uh, what?)  These "numbers" don't match the
standard ANSI color code output. We translate them.
bugz 5 yıl önce
ebeveyn
işleme
998ff80568
1 değiştirilmiş dosya ile 113 ekleme ve 4 silme
  1. 113 4
      mystic.c

+ 113 - 4
mystic.c

@@ -194,10 +194,119 @@ void render_sleep(void) {
   }
 }
 
-// This needs work.
+/*
+Well SNAP!  Mystic numbers don't remotely match ANSI color codes.
+
+ 00 : Sets the current foreground to Black          0;30
+ 01 : Sets the current foreground to Dark Blue      0;34
+ 02 : Sets the current foreground to Dark Green     0;32
+ 03 : Sets the current foreground to Dark Cyan      0;36
+ 04 : Sets the current foreground to Dark Red       0;31
+ 05 : Sets the current foreground to Dark Magenta   0;35
+ 06 : Sets the current foreground to Brown          0;33
+ 07 : Sets the current foreground to Grey           0;37
+ 08 : Sets the current foreground to Dark Grey      1;30
+ 09 : Sets the current foreground to Light Blue     1;34
+ 10 : Sets the current foreground to Light Green    1;32
+ 11 : Sets the current foreground to Light Cyan     1;36
+ 12 : Sets the current foreground to Light Red      1;31
+ 13 : Sets the current foreground to Light Magenta  1;35
+ 14 : Sets the current foreground to Yellow         1;33
+ 15 : Sets the current foreground to White          1;37
+
+ 16 : Sets the current background to Black          40
+ 17 : Sets the current background to Blue           44
+ 18 : Sets the current background to Green          42
+ 19 : Sets the current background to Cyan           46
+ 20 : Sets the current background to Red            41
+ 21 : Sets the current background to Magenta        45
+ 22 : Sets the current background to Brown          43
+ 23 : Sets the current background to Grey           47
+
+ 24 : Sets the current background to black with blinking foreground     5;40
+ 25 : Sets the current background to blue with blinking foreground      5;44
+ 26 : Sets the current background to green with blinking foreground     5;42
+ 27 : Sets the current background to cyan with blinking foreground      5;46
+ 28 : Sets the current background to red with blinking foreground       5;41
+ 29 : Sets the current background to magenta with blinking foreground   5;45
+ 30 : Sets the current background to brown with blinking foreground     5;43
+ 31 : Sets the current background to grey with blinking foreground      5;47
+
+Other things that Mystic does ... 
+
+  [A## - Move the cursor up ## lines
+  [B## - Move the cursor down ## lines
+  [C## - Move the cursor forward (to the right) ## columns
+  [D## - Move the cursor backwards (to the left) ## columns
+  [K   - Clear from the current cursor position to the end of the line
+  [L   - Move cursor and erase data backwards from current column to column ##
+  [X## - Move cursor to X coordinate ##
+  [Y## - Move cursor to Y coordinate ##
+  BS   - Sends 1 destructive backspace sequence (ASCII 8-32-8)
+  CL   - Clears the screen (ANSI 1,1 locate and [2J or ASCII 12)
+  CR   - Send a carrage return and line feed (move to next line)
+  RA   - Restore the saved text attribute color
+  RS   - Restore the saved user's terminal screen
+  SA   - Save the current text attribute color
+  SS   - Save the entire user's terminal screen
+
+ */
+
+// Covert MYSTIC color to (Proper) ANSI COLOR.
+
+const int MYSTIC[] = { 0, 4, 2, 6, 1, 5, 3, 7};
+
+// ANSI_color = MYSTIC[ odd_mystic_color % 8 ]
+
 void write_color(int fd, int color) {
   char buffer[10];
-  sprintf(buffer, "\x1b[%dm", color);
+
+  switch (color) {
+    case 0:
+    case 1:
+    case 2:
+    case 3:
+    case 4:
+    case 5:
+    case 6:
+    case 7:
+      sprintf(buffer, "\x1b[0;3%dm", MYSTIC[color]);
+      break;
+    case 8:
+    case 9:
+    case 10:
+    case 11:
+    case 12:
+    case 13:
+    case 14:
+    case 15:
+      sprintf(buffer, "\x1b[1;3%dm", MYSTIC[color - 8]);
+      break;
+    case 16:
+    case 17:
+    case 18:
+    case 19:
+    case 20:
+    case 21:
+    case 22:
+    case 23:
+      sprintf(buffer, "\x1b[4%dm", MYSTIC[color - 16]);
+      break;
+    case 24:
+    case 25:
+    case 26:
+    case 27:
+    case 28:
+    case 29:
+    case 30:
+    case 31:
+      sprintf(buffer, "\x1b[5;4%dm", MYSTIC[color - 24]);
+      break;
+    default:
+      buffer[0] = 0;
+      break;
+  }
+  ZF_LOGD("write_color( %d ): %s", color, repr(buffer));
   write(fd, buffer, strlen(buffer));
 }
 
@@ -513,8 +622,8 @@ void harry_event(int fd) {
   ZF_LOGD("%d => %d %d", r, last_seen_harry_event[0], last_seen_harry_event[1]);
 
   cp = phrases[r];
-
-  sprintf(buffer, "^S2%s^P2^D%02d", cp, (int)strlen(cp));
+  int color = random() % 16;
+  sprintf(buffer, "^S2^C%02d%s^P2^D%02d", color, cp, (int)strlen(cp));
 
   ZF_LOGD("harry_event: render(%d, \"%s\")", fd, buffer);