Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions libs/linglong/src/linglong/cli/cli_printer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,11 @@ void CLIPrinter::printUpgradeList(std::vector<api::types::v1::UpgradeListResult>
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)
Expand Down
54 changes: 54 additions & 0 deletions libs/linglong/tests/ll-tests/src/linglong/cli/cli_printer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::size_t> 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<UpgradeListResult> hundred(100, item);
std::string hundredOut;
{
CaptureStdout capture;
printer.printUpgradeList(hundred);
hundredOut = capture.str();
}

std::vector<UpgradeListResult> 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;
Expand Down
Loading