From f2251d0e39d359117a1b7466ae500c3692dd5e44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E9=82=A3=E5=87=A0?= <2078634206@qq.com> Date: Wed, 30 Sep 2026 13:18:44 +0800 Subject: [PATCH] fix(cli): stop upgrade list column widths growing with item count MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit printUpgradeList added the +2 column padding inside the per-item loop, so each extra package widened the ID and Installed columns by 2 regardless of content (100 identical 14-char ids produced a 214-char column); compute the maxima in the loop and add the padding once afterwards; add a unit test asserting the column offset is independent of the item count. Signed-off-by: 陈那几 <2078634206@qq.com> --- .../linglong/src/linglong/cli/cli_printer.cpp | 6 ++- .../src/linglong/cli/cli_printer_test.cpp | 54 +++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/libs/linglong/src/linglong/cli/cli_printer.cpp b/libs/linglong/src/linglong/cli/cli_printer.cpp index c4d209879..3d00acd12 100644 --- a/libs/linglong/src/linglong/cli/cli_printer.cpp +++ b/libs/linglong/src/linglong/cli/cli_printer.cpp @@ -309,9 +309,11 @@ void CLIPrinter::printUpgradeList(std::vector std::size_t idLen{ 0 }, installedLen{ 0 }; std::for_each(list.cbegin(), list.cend(), [&idLen, &installedLen](const auto &info) { - idLen = std::max(idLen, info.id.size()) + 2; - installedLen = std::max(installedLen, info.oldVersion.size()) + 2; + idLen = std::max(idLen, info.id.size()); + installedLen = std::max(installedLen, info.oldVersion.size()); }); + idLen += 2; + installedLen += 2; std::cout << "\033[38;5;214m" << std::left << std::setw(idLen) << adjustDisplayWidth(qUtf8Printable(_("ID")), idLen) << std::setw(installedLen) diff --git a/libs/linglong/tests/ll-tests/src/linglong/cli/cli_printer_test.cpp b/libs/linglong/tests/ll-tests/src/linglong/cli/cli_printer_test.cpp index 2e01174f2..9b9eaad19 100644 --- a/libs/linglong/tests/ll-tests/src/linglong/cli/cli_printer_test.cpp +++ b/libs/linglong/tests/ll-tests/src/linglong/cli/cli_printer_test.cpp @@ -239,6 +239,60 @@ TEST(CLIPrinter, PrintUpgradeList) EXPECT_THAT(out, ::testing::HasSubstr("2.0.0")); } +TEST(CLIPrinter, PrintUpgradeListColumnWidthIndependentOfItemCount) +{ + UpgradeListResult item; + item.id = "org.deepin.app"; + item.oldVersion = "1.0.0"; + item.newVersion = "2.0.0"; + + // the old version must always start right after the id column: + // max id width plus the two-space column padding + const std::size_t expectedOffset = item.id.size() + 2; + + linglong::cli::CLIPrinter printer; + + auto oldVersionOffsets = [&expectedOffset, &item](const std::string &out) { + std::vector offsets; + std::istringstream lines(out); + std::string line; + while (std::getline(lines, line)) { + if (line.empty() || line.find('\033') != std::string::npos) { + continue; // skip the colored header line + } + offsets.push_back(line.find(item.oldVersion)); + } + return offsets; + }; + + std::vector hundred(100, item); + std::string hundredOut; + { + CaptureStdout capture; + printer.printUpgradeList(hundred); + hundredOut = capture.str(); + } + + std::vector single{ item }; + std::string singleOut; + { + CaptureStdout capture; + printer.printUpgradeList(single); + singleOut = capture.str(); + } + + const auto hundredOffsets = oldVersionOffsets(hundredOut); + ASSERT_EQ(hundredOffsets.size(), std::size_t{ 100 }); + for (const auto offset : hundredOffsets) { + EXPECT_EQ(offset, expectedOffset); + } + + const auto singleOffsets = oldVersionOffsets(singleOut); + ASSERT_EQ(singleOffsets.size(), std::size_t{ 1 }); + // the column offset must not depend on the number of listed packages + EXPECT_EQ(singleOffsets.front(), expectedOffset); +} + TEST(CLIPrinter, PrintInspect) { InspectResult result;