Handle long strings, embedded new lines and empty descriptions

This commit is contained in:
Jeremy Kolb 2024-07-08 16:31:33 -04:00
parent 59b6aafadb
commit f22cf1fd38
2 changed files with 23 additions and 6 deletions

View file

@ -1261,12 +1261,23 @@ struct CmdFlakeShow : FlakeCommand, MixJSON
attrPath.size() == 3 && attrPathS[0] == "checks" ? "derivation" : attrPath.size() == 3 && attrPathS[0] == "checks" ? "derivation" :
attrPath.size() >= 1 && attrPathS[0] == "hydraJobs" ? "derivation" : attrPath.size() >= 1 && attrPathS[0] == "hydraJobs" ? "derivation" :
"package"; "package";
if (description) { if (description && !description->empty()) {
// Handle new lines in descriptions. // Trim the string and only display the first line of the description.
auto index = description->find('\n'); auto trimmed = nix::trim(*description);
std::string_view sanitized_description(description->data(), index != std::string::npos ? index : description->size()); auto newLinePos = trimmed.find('\n');
auto length = newLinePos != std::string::npos ? newLinePos : trimmed.size();
logger->cout("%s: %s '%s' - '%s'", headerPrefix, type, name, sanitized_description); // If the string is too long then resize add ellipses
std::string desc;
if (length > 80) {
trimmed.resize(80);
desc = trimmed.append("...");
}
else {
desc = trimmed.substr(0, length);
}
logger->cout("%s: %s '%s' - '%s'", headerPrefix, type, name, desc);
} }
else { else {
logger->cout("%s: %s '%s'", headerPrefix, type, name); logger->cout("%s: %s '%s'", headerPrefix, type, name);

View file

@ -95,9 +95,13 @@ cat >flake.nix<<EOF
aNoDescription = import ./simple.nix; aNoDescription = import ./simple.nix;
bOneLineDescription = import ./simple.nix // { meta.description = "one line"; }; bOneLineDescription = import ./simple.nix // { meta.description = "one line"; };
cMultiLineDescription = import ./simple.nix // { meta.description = '' cMultiLineDescription = import ./simple.nix // { meta.description = ''
line one line one
line two line two
''; }; ''; };
dLongDescription = import ./simple.nix // { meta.description = ''
01234567890123456789012345678901234567890123456789012345678901234567890123456789abcdefg
''; };
eEmptyDescription = import ./simple.nix // { meta.description = ""; };
}; };
}; };
} }
@ -106,3 +110,5 @@ nix flake show > ./show-output.txt
test "$(awk -F '[:] ' '/aNoDescription/{print $NF}' ./show-output.txt)" = "package 'simple'" test "$(awk -F '[:] ' '/aNoDescription/{print $NF}' ./show-output.txt)" = "package 'simple'"
test "$(awk -F '[:] ' '/bOneLineDescription/{print $NF}' ./show-output.txt)" = "package 'simple' - 'one line'" test "$(awk -F '[:] ' '/bOneLineDescription/{print $NF}' ./show-output.txt)" = "package 'simple' - 'one line'"
test "$(awk -F '[:] ' '/cMultiLineDescription/{print $NF}' ./show-output.txt)" = "package 'simple' - 'line one'" test "$(awk -F '[:] ' '/cMultiLineDescription/{print $NF}' ./show-output.txt)" = "package 'simple' - 'line one'"
test "$(awk -F '[:] ' '/dLongDescription/{print $NF}' ./show-output.txt)" = "package 'simple' - '01234567890123456789012345678901234567890123456789012345678901234567890123456789...'"
test "$(awk -F '[:] ' '/eEmptyDescription/{print $NF}' ./show-output.txt)" = "package 'simple'"