Selaa lähdekoodia

Include string, notes on screen positions seen.

Steve Thielemann 4 vuotta sitten
vanhempi
commit
a9087f362c
1 muutettua tiedostoa jossa 29 lisäystä ja 24 poistoa
  1. 29 24
      hharry.cpp

+ 29 - 24
hharry.cpp

@@ -1,4 +1,3 @@
-#include <assert.h>
 #include <fcntl.h>
 #include <pty.h>
 #include <stdio.h>
@@ -49,7 +48,7 @@
 */
 
 // When debugging low-level, use this:
-//ZF_LOG_LEVEL=ZF_LOG_VERBOSE
+// ZF_LOG_LEVEL=ZF_LOG_VERBOSE
 
 // LOGGING with file output
 #include "zf_log.h"
@@ -77,6 +76,9 @@ static void file_output_open(const char *const log_path) {
 
 // END LOGGING
 
+#include <string>
+using namespace std;
+
 #include "terminal.h"
 
 struct console_details console;
@@ -535,6 +537,7 @@ int mangle(int fd, const char *buffer, int len) {
                       {wolf, sizeof(wolf) / sizeof(char *), 1, 0},
                       {panther, sizeof(panther) / sizeof(char *), 1, 0},
                       {bat, sizeof(bat) / sizeof(char *), 1, 0},
+                      {icu, sizeof(icu) / sizeof(char *), 0, 20},
                       {skull, sizeof(skull) / sizeof(char *), 0, 19},
                       {skullblink, sizeof(skullblink) / sizeof(char *), 0, 19}};
 
@@ -545,7 +548,7 @@ int mangle(int fd, const char *buffer, int len) {
           r = randint((sizeof(images) / sizeof(image)));
         } while (last_files.seen_before(r));
 
-        char fgoto[10] = "";
+        char fgoto[32];
 
         if (!images[r].cls) {
           int x = 0, y = 0;
@@ -554,7 +557,8 @@ int mangle(int fd, const char *buffer, int len) {
           y = randint(24 - images[r].size);
 
           int slen;
-          slen = snprintf(fgoto, sizeof(fgoto), "^f%02d%02d", x, y);
+          // render image, home cursor
+          slen = snprintf(fgoto, sizeof(fgoto), "^f%02d%02d\x1b[1;1H", x, y);
           if (slen >= sizeof(fgoto)) {
             ZF_LOGE("snprintf %d > size %d", slen, (int)sizeof(fgoto));
             fgoto[0] = 0;
@@ -576,10 +580,9 @@ int mangle(int fd, const char *buffer, int len) {
 
         // Ok, yes, there's no filename being sent.  :P
         render_image(images[r].lines, images[r].size);
-        
-        slen = snprintf(display, sizeof(display), "%s%s^P3%s",
-                        needs_cls ? "\x1b[2J" : "", fgoto,
-                        restore_color);
+
+        slen = snprintf(display, sizeof(display), "%s%s%s^P3",
+                        needs_cls ? "\x1b[2J" : "", fgoto, restore_color);
         if (slen >= sizeof(display)) {
           ZF_LOGE("snprintf %d > size %d", slen, (int)sizeof(display));
           display[0] = 0;
@@ -623,11 +626,12 @@ int mangle(int fd, const char *buffer, int len) {
           It is possible that it can get mangled/wrangled!
           */
           const char *phrasing[] = {
-              "^R1Haha^P1ha^P1ha",
-              "Poof!",
-              "Got U",
-              "Anyone there?",
+              "^R1Haha^P1ha^P1ha", "Poof!", "Got U", "Anyone there?",
               "^R1Knock, ^P1Knock",
+              /*
+              This picks random color and position -- then
+              homes cursor and changes to another color.  (This can be seen.)
+               */
               "^G0101^C07^S9Segmentation fault (core dumped)^P2"};
           static LastSeen last_phrasing(2);
 
@@ -722,7 +726,7 @@ int mangle(int fd, const char *buffer, int len) {
     if (ansi) {
       work[x] = replace_with;
       if (tc.ansi != START) {
-        ZF_LOGD("ANSI type %d at %d", tc.ansi, x );
+        ZF_LOGD("ANSI type %d at %d", tc.ansi, x);
       }
     }
     // fixup "work" so it's a valid C string
@@ -734,7 +738,7 @@ int mangle(int fd, const char *buffer, int len) {
   // (required for regex to work.)
   work[len] = 0;
 
-  ZF_LOGI_MEM(work, len, "Work now:");
+  ZF_LOGV_MEM(work, len, "Work now:");
 
   /*
    (random) Locate words (in work), and possibly flip them around.
@@ -790,17 +794,14 @@ int mangle(int fd, const char *buffer, int len) {
    Print some characters slowly.  Delay.
    */
 
+  if (mangled)
+    ZF_LOGI("Mangled %d word, %d chars (render %d)", mangled, mangled_chars,
+            need_render);
+
   if (need_render) {
     ZF_LOGD_MEM(play, len, "Ready to render:");
     // ZF_LOGD("HH %d : (%d) %s", need_render, (int)strlen(buffer),
     // repr(buffer));
-  } else {
-    if (mangled) {
-      ZF_LOGD_MEM(play, len, "Mangled %d words, %d chars:", mangled,
-                  mangled_chars);
-      /* ZF_LOGD("Mangled %d words, %d chars : %s", mangled, mangled_chars,
-              repr(buffer)); */
-    }
   }
 
   if (need_render) {
@@ -1045,7 +1046,11 @@ int main(int argc, char *argv[]) {
         if (time_idle) {
           harry_idle_event(STDOUT_FILENO);
         } else {
-          ZF_LOGV_MEM(buffer, size, "TIMEOUT buffer size=%d", size);
+          if (ZF_LOG_ON_VERBOSE) {
+            ZF_LOGV_MEM(buffer, size, "TIMEOUT buffer size=%d", size);
+          } else {
+            ZF_LOGI("TIMEOUT buffer size=%d", size);
+          }
           console_receive(&console, buffer, size);
           write(STDOUT_FILENO, buffer, size);
           size = 0;
@@ -1074,8 +1079,8 @@ int main(int argc, char *argv[]) {
             mangle(STDOUT_FILENO, buffer, pos);
             memmove(buffer, buffer + pos, size - pos);
             size -= pos;
-          // } else {
-          //   ZF_LOGV("position of /r/n not found.");
+            // } else {
+            //   ZF_LOGV("position of /r/n not found.");
           }
 
         } else