1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2024-09-19 10:50:24 -04:00

copy string using filterANSIEscapes and enforce the max length

This commit is contained in:
Jeremy Kolb 2024-08-05 14:15:14 -04:00
parent 930818bb1d
commit 1c5f1de43f

View file

@ -17,6 +17,7 @@
#include "eval-cache.hh" #include "eval-cache.hh"
#include "markdown.hh" #include "markdown.hh"
#include "users.hh" #include "users.hh"
#include "terminal.hh"
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <iomanip> #include <iomanip>
@ -1263,18 +1264,16 @@ struct CmdFlakeShow : FlakeCommand, MixJSON
"package"; "package";
if (description && !description->empty()) { if (description && !description->empty()) {
// Trim the string and only display the first line of the description. // Trim the string and only display the first line of the description.
const size_t maxLength = 77;
auto trimmed = nix::trim(*description); auto trimmed = nix::trim(*description);
auto newLinePos = trimmed.find('\n'); auto newLinePos = trimmed.find('\n');
auto length = newLinePos != std::string::npos ? newLinePos : trimmed.size(); auto length = newLinePos != std::string::npos ? newLinePos : trimmed.length();
// If the string is too long then resize add ellipses // Resize/sanitize the string and if it's too long add ellipses
std::string desc; std::string desc = filterANSIEscapes(trimmed, false, length);
if (length > 77) { if (desc.length() > maxLength) {
trimmed.resize(77); desc.resize(maxLength);
desc = trimmed.append("..."); desc = desc.append("...");
}
else {
desc = trimmed.substr(0, length);
} }
logger->cout("%s: %s '%s' - '%s'", headerPrefix, type, name, desc); logger->cout("%s: %s '%s' - '%s'", headerPrefix, type, name, desc);