Use the window size for the entire length

This commit is contained in:
Jeremy Kolb 2024-08-08 14:41:25 -04:00
parent 9bf6684b08
commit abbaba9122
2 changed files with 30 additions and 14 deletions

View File

@ -1264,22 +1264,38 @@ struct CmdFlakeShow : FlakeCommand, MixJSON
"package"; "package";
if (description && !description->empty()) { if (description && !description->empty()) {
// Maximum length to print // Maximum length to print
size_t maxLength = getWindowSize().second; size_t maxLength = getWindowSize().second > 0 ? getWindowSize().second : 80;
if (maxLength == 0)
maxLength = 77; // Trim the description and only use the first line
// Trim the string and only display the first line of the description. auto trimmed = 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.length(); auto length = newLinePos != std::string::npos ? newLinePos : trimmed.length();
// Resize/sanitize the string and if it's too long add ellipses // Sanitize the description and calculate the two parts of the line
std::string desc = filterANSIEscapes(trimmed, false, length); // In order to get the length of the printable characters we need to
if (desc.length() > maxLength) { // filter out escape sequences.
desc.resize(maxLength); auto beginningOfLine = fmt("%s: %s '%s'", headerPrefix, type, name);
desc = desc.append("..."); auto beginningOfLineLength = filterANSIEscapes(beginningOfLine, true).length();
} auto restOfLine = fmt(" - '%s'", filterANSIEscapes(trimmed, false, length));
logger->cout("%s: %s '%s' - '%s'", headerPrefix, type, name, desc); // If we are already over the maximum length then do not trim
// and don't print the description (preserves existing behavior)
if (beginningOfLineLength >= maxLength) {
logger->cout("%s", beginningOfLine);
}
else {
auto line = beginningOfLine + restOfLine;
// FIXME: Specifying `true` here gives the correct length
// BUT removes colors/bold so something is not quite right here.
line = filterANSIEscapes(line, true, maxLength);
// NOTE: This test might be incorrect since I get things like:
// 168 or 161 > maxLength.
if (line.length() > maxLength) {
line = line.replace(line.length() - 3, 3, "...");
}
logger->cout("%s", line);
}
} }
else { else {
logger->cout("%s: %s '%s'", headerPrefix, type, name); logger->cout("%s: %s '%s'", headerPrefix, type, name);

View File

@ -110,5 +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' - '01234567890123456789012345678901234567890123456789012345678901234567890123456...'" test "$(awk -F '[:] ' '/dLongDescription/{print $NF}' ./show-output.txt)" = "package 'simple' - '012345678901234567890123456..."
test "$(awk -F '[:] ' '/eEmptyDescription/{print $NF}' ./show-output.txt)" = "package 'simple'" test "$(awk -F '[:] ' '/eEmptyDescription/{print $NF}' ./show-output.txt)" = "package 'simple'"