From 12d4cff9f220631ff00881c90832ae21a4892de1 Mon Sep 17 00:00:00 2001
From: Franck Dary <franck.dary@lis-lab.fr>
Date: Mon, 13 Jan 2020 11:37:41 +0100
Subject: [PATCH] Made debug printing for Config easier to read

---
 common/include/util.hpp        |  2 ++
 common/src/util.cpp            | 28 ++++++++++++++++++++++++++++
 reading_machine/src/Config.cpp |  5 +++--
 3 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/common/include/util.hpp b/common/include/util.hpp
index 2d00f79..49965fe 100644
--- a/common/include/util.hpp
+++ b/common/include/util.hpp
@@ -41,6 +41,8 @@ utf8string splitAsUtf8(std::string_view s);
 
 std::string int2HumanStr(int number);
 
+std::string shrink(const std::string & s, int printedSize);
+
 int printedLength(std::string_view s);
 
 bool isSeparator(utf8char c);
diff --git a/common/src/util.cpp b/common/src/util.cpp
index 1542c89..d8c1654 100644
--- a/common/src/util.cpp
+++ b/common/src/util.cpp
@@ -75,6 +75,34 @@ util::utf8string util::splitAsUtf8(std::string_view s)
   return result;
 }
 
+std::string util::shrink(const std::string & s, int printedSize)
+{
+  static const std::string filler = "..";
+
+  if (printedLength(s) <= printedSize)
+    return s;
+
+  auto splited = splitAsUtf8(s);
+
+  std::string result;
+  std::string begin, end;
+  int nbLoop = 0;
+
+  while (printedLength(begin)+printedLength(end)+2 <= printedSize)
+  {
+    result = begin + filler + end;
+
+    if (nbLoop % 2)
+      end = fmt::format("{}{}", splited[splited.size()-1-(nbLoop/2)], end);
+    else
+      begin = fmt::format("{}{}", begin, splited[nbLoop/2]);
+
+    ++nbLoop;
+  }
+
+  return result;
+}
+
 void util::warning(std::string_view message, const std::experimental::source_location & location)
 {
   fmt::print(stderr, "WARNING ({}) : {}\n", location, message);
diff --git a/reading_machine/src/Config.cpp b/reading_machine/src/Config.cpp
index 96500a7..0f7a2bc 100644
--- a/reading_machine/src/Config.cpp
+++ b/reading_machine/src/Config.cpp
@@ -80,6 +80,7 @@ void Config::printForDebug(FILE * dest) const
 {
   static constexpr int windowSize = 5;
   static constexpr int lettersWindowSize = 40;
+  static constexpr int maxWordLength = 7;
 
   int firstLineToPrint = wordIndex;
   int lastLineToPrint = wordIndex;
@@ -102,7 +103,7 @@ void Config::printForDebug(FILE * dest) const
     toPrint.emplace_back();
     toPrint.back().emplace_back(line == (int)wordIndex ? "=>" : "");
     for (unsigned int i = 0; i < getNbColumns(); i++)
-      toPrint.back().emplace_back(getLastNotEmptyConst(i, line));
+      toPrint.back().emplace_back(util::shrink(getLastNotEmptyConst(i, line), maxWordLength));
   }
 
   std::vector<std::size_t> colLength(toPrint[0].size(), 0);
@@ -146,7 +147,7 @@ void Config::printForDebug(FILE * dest) const
     if (line == 1)
       fmt::print(dest, "{}\n", longLine);
     for (unsigned int col = 0; col < toPrint[line].size()-1; col++)
-      fmt::print(dest, "{}{:>{}}{}", toPrint[line][col], "", colLength[col]-util::printedLength(toPrint[line][col]), col == toPrint[line].size()-2 ? "\n" : "\t");
+      fmt::print(dest, "{}{:>{}}{}", toPrint[line][col], "", colLength[col]-util::printedLength(toPrint[line][col]), col == toPrint[line].size()-2 ? "\n" : "  ");
     if (toPrint[line].back() == EOSSymbol1)
       fmt::print(dest, "\n");
   }
-- 
GitLab