Przeglądaj źródła

* Re-add wrapping functionality to dumplots() via bdlib String

Bryan Drewery 16 lat temu
rodzic
commit
3fec1bcc9f
1 zmienionych plików z 23 dodań i 3 usunięć
  1. 23 3
      src/dccutil.c

+ 23 - 3
src/dccutil.c

@@ -219,9 +219,29 @@ void dumplots(int idx, const char *prefix, const bd::String data)
 
   const size_t max_data_len = 120 - strlen(prefix);
   bd::Array<bd::String> lines = data.split('\n');
-
-  for (size_t i = 0; i < lines.length(); ++i) {
-    dprintf(idx, "%s%s\n", prefix, bd::String(lines[i]).c_str());
+  size_t i = 0;
+
+  while (i < lines.length()) {
+    bd::String line(lines[i]);
+    if (line.length() > max_data_len) {
+      // Truncate at last space if possible
+      size_t pos = std::min(line.length() - 1, max_data_len);
+      while (line.length() - pos > 0 && line[pos] != ' ')
+        --pos;
+      // Trim out the space
+      if (line[pos] == ' ' && pos > 0 && pos < line.length())
+        ++pos;
+
+      if (bd::String(line(pos)).find("\n") != bd::String::npos) // Newline in remaining: dump it
+        dprintf(idx, "%s%s\n", prefix, bd::String(line(pos)).c_str());
+      else if (lines.length() - (i + 1) > 0) // Wrapped text: prepend to next line if possible
+        lines[i + 1] = line(pos) + lines[i + 1];
+      else // Wrapped text (last line): Add onto list of lines
+        lines << line(pos);
+      line = line(0, pos);
+    }
+    dprintf(idx, "%s%s\n", prefix, line.c_str());
+    ++i;
   }
 }