Skip to content

Commit

Permalink
Handle long strings, embedded new lines and empty descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
kjeremy committed Jul 8, 2024
1 parent 868f759 commit 5e2c7c5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
21 changes: 16 additions & 5 deletions src/nix/flake.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1258,12 +1258,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();

// 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, sanitized_description);
logger->cout("%s: %s '%s' - '%s'", headerPrefix, type, name, desc);
}
else {
logger->cout("%s: %s '%s'", headerPrefix, type, name);
Expand Down
8 changes: 7 additions & 1 deletion tests/functional/flakes/show.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""; };
};
};
}
Expand All @@ -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'"

0 comments on commit 5e2c7c5

Please sign in to comment.