diff --git a/common/include/util.hpp b/common/include/util.hpp index 2d00f799e4777cc2898c66f06774afa08cc47b00..49965fe9050e89f22ac292bd581b6f4d87cc7202 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 1542c89046a8cea68ae122753f98f87fe3eda2e8..d8c16540181c3204ac9d1c70f6a946191ff914e8 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 96500a71d690bb690765b66189bd6647fe8551c2..0f7a2bcb9d5b85487e055dc31847ffce4fcf289d 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"); }