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

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() >= 1 && attrPathS[0] == "hydraJobs" ? "derivation" :
"package";
if (description) {
// Handle new lines in descriptions.
auto index = description->find('\n');
std::string_view sanitized_description(description->data(), index != std::string::npos ? index : description->size());
if (description && !description->empty()) {
// Trim the string and only display the first line of the description.
auto trimmed = nix::trim(*description);
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 {
logger->cout("%s: %s '%s'", headerPrefix, type, name);

View file

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