From e2e4e3ced75bb5e9db28cc35b6ebf371e8e5be94 Mon Sep 17 00:00:00 2001 From: Simon Johnsson Date: Wed, 18 Feb 2026 15:37:48 +0100 Subject: Scale history-list to screen width --- apt-private/private-history.cc | 98 +++++++++++++++++++++++++++++------------- test/integration/test-history | 73 ++++++++++++++++++++++++------- 2 files changed, 125 insertions(+), 46 deletions(-) diff --git a/apt-private/private-history.cc b/apt-private/private-history.cc index 31b3b1333..edce7b227 100644 --- a/apt-private/private-history.cc +++ b/apt-private/private-history.cc @@ -16,6 +16,7 @@ #include #include #include +#include #include #include @@ -103,10 +104,14 @@ Change APT::Internal::InvertChange(const Change &change) static std::string ShortenCommand(const std::string &cmd, const std::size_t maxLen) { std::string shortenedCmd = cmd; - if (shortenedCmd.starts_with("apt ")) + if (shortenedCmd.starts_with("/usr/bin/")) + shortenedCmd = shortenedCmd.substr(9); + else if (shortenedCmd.starts_with("apt ")) shortenedCmd = shortenedCmd.substr(4); + if (shortenedCmd.length() > maxLen - 3) return shortenedCmd.substr(0, maxLen - 4) + "..."; + return shortenedCmd; } @@ -191,48 +196,79 @@ static std::string GetKindString(const Entry &entry) // -static void PrintHistoryVector(const HistoryBuffer buf, int columnWidth) +static void PrintHistoryVector(const HistoryBuffer &buf) { - // Calculate the width of the ID column, esentially the number of digits - int id = 0; - size_t sizeFrac = buf.size(); - int idWidth = 2; - while (sizeFrac) + // TRANSLATORS: a number used as identifier. + const std::string idHeader = _("ID"); + // TRANSLATORS: input on the terminal console. + const std::string cmdHeader = _("Command line"); + // TRANSLATORS: indicates when the entry occurred. + const std::string dateHeader = _("Date and Time"); + // TRANSLATORS: the type of operation that was performed. + const std::string actionHeader = _("Action"); + // TRANSLATORS: changes as a quantity. + const std::string changesHeader = _("Changes"); + + constexpr size_t pad = 2; + size_t idWidth = std::to_string(buf.size()).size(); + idWidth = std::max(idHeader.size(), idWidth) + pad; + size_t cmdLineWidth = cmdHeader.size() + pad; + size_t dateWidth = dateHeader.size() + pad; + size_t actionWidth = actionHeader.size() + pad; + size_t changesWidth = changesHeader.size(); + + // Scale to screen width + // ID and Changes are numerical and cannot be shortened + size_t remainingWidth = ::ScreenWidth - idWidth - changesWidth; + size_t minRequiredCmdWidth = cmdLineWidth; + std::vector kindStrings; + std::vector changeCounts; + kindStrings.reserve(buf.size()); + changeCounts.reserve(buf.size()); + if (!buf.empty()) { - sizeFrac /= 10; - idWidth++; + dateWidth = std::max(buf.front().startDate.size() + pad, dateWidth); + for (const auto &entry : buf) + { + std::string kindString = GetKindString(entry); + if (kindString.size() > actionWidth) + actionWidth = kindString.size(); + kindStrings.push_back(kindString); + + size_t numChanges = 0; + for (const auto &[_, changes] : entry.changeMap) + numChanges += changes.size(); + changeCounts.push_back(numChanges); + + if (entry.cmdLine.length() > minRequiredCmdWidth) + minRequiredCmdWidth = entry.cmdLine.length() + pad; + } + remainingWidth -= (dateWidth + actionWidth); + cmdLineWidth = std::min(remainingWidth, minRequiredCmdWidth); } - // Print headers - auto writeEntry = [](std::string entry, size_t width) + constexpr auto writeEntry = [](const auto &entry, size_t width) { std::cout << std::left << std::setw(width) << entry; }; - writeEntry(_("ID"), idWidth); - writeEntry(_("Command line"), columnWidth); - // NOTE: if date format is different, - // this width needs to change - writeEntry(_("Date and Time"), 23); // width for datestring - writeEntry(_("Action"), 10); // 9 chars longest action type - writeEntry(_("Changes"), columnWidth); + writeEntry(idHeader, idWidth); + writeEntry(cmdHeader, cmdLineWidth); + writeEntry(dateHeader, dateWidth); + writeEntry(actionHeader, actionWidth); + writeEntry(changesHeader, changesWidth); std::cout << "\n\n"; // Each entry corresponds to a row - for (auto entry : buf) + for (size_t id = 0; id < buf.size(); ++id) { - writeEntry(std::to_string(id), idWidth); - writeEntry(ShortenCommand(entry.cmdLine, columnWidth), columnWidth); - writeEntry(entry.startDate, 23); + const auto &entry = buf[id]; + writeEntry(id, idWidth); + writeEntry(ShortenCommand(entry.cmdLine, cmdLineWidth), cmdLineWidth); + writeEntry(entry.startDate, dateWidth); // If there are multiple actions, we want to group them - writeEntry(GetKindString(entry), 10); - - // Count the number of packages changed - size_t numChanges = 0; - for (const auto &[_, changes] : entry.changeMap) - numChanges += changes.size(); - writeEntry(std::to_string(numChanges), columnWidth); + writeEntry(kindStrings[id], actionWidth); + writeEntry(changeCounts[id], changesWidth); std::cout << "\n"; - id++; } } @@ -463,7 +499,7 @@ bool DoHistoryList(CommandLine &Cmd) if (not ParseLogDir(buf)) return _error->Error(_("Could not read: %s"), _config->FindFile("Dir::Log::History").data()); - PrintHistoryVector(buf, 25); + PrintHistoryVector(buf); return true; } diff --git a/test/integration/test-history b/test/integration/test-history index 34108e357..48966d011 100755 --- a/test/integration/test-history +++ b/test/integration/test-history @@ -54,20 +54,7 @@ Command line: apt-get install pkg1 Packages changed: Install pkg1:amd64 (1) " normalizedinfo - -# replace aribtrary command line and date with "dummy". -normalizedlist() { - apt history-list | sed -E 's/^([0-9]+)[[:space:]]+'\ -'.*'\ -'[0-9]{4}-[0-9]{2}-[0-9]{2}[[:space:]]+'\ -'[0-9]{2}:[0-9]{2}:[0-9]{2}/'\ -'\1 dummy dummy /' -} - -testsuccessequal "ID Command line Date and Time Action Changes -0 dummy dummy Install 1 -1 dummy dummy Install 1 " normalizedlist ################################################################################ # Read previous history for manipulation @@ -118,9 +105,9 @@ Commandline: install nothing End-Date: dummy EOF -testsuccessequal "ID Command line Date and Time Action Changes +testsuccessequal "ID Command line Date and Time Action Changes -0 install nothing dummy 0 " normalizedlist +0 install nothing dummy 0 " apt history-list testsuccessequal "Transaction ID: 0 Start time: dummy @@ -129,6 +116,62 @@ Requested by: Command line: install nothing Packages changed:" normalizedinfo +cat > rootdir/var/log/apt/history.log << EOF +Start-Date: 1970-01-01 00:00:00 +Commandline: install nothing +Install: nothing +End-Date: 1970-01-01 00:00:00 + +Start-Date: 1971-01-01 00:00:00 +Commandline: remove something +Remove: something +End-Date: 1971-01-01 00:00:00 +EOF + +testsuccessequal "ID Command line Date and Time Action Changes + +0 install nothing 1970-01-01 00:00:00 Install 1 +1 remove something 1971-01-01 00:00:00 Remove 1 " apt history-list + +################################################################################ +# Test different column widths +################################################################################ +settestcolumnwidth() { + local columns=$1 + local A_chars + A_chars=$(printf 'a%.0s' $(seq 1 "$columns")) + + cat > rootdir/var/log/apt/history.log << EOF +Start-Date: dummy +Commandline: $A_chars +Install: nothing +End-Date: dummy +EOF + + export COLUMNS="$columns" +} + +settestcolumnwidth 80 +testsuccessequal "ID Command line Date and Time Action Changes + +0 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...dummy Install 1 " apt history-list + +settestcolumnwidth 50 +testsuccessequal "ID Command line Date and Time Action Changes + +0 aaaaaaaaaaaaa...dummy Install 1 " apt history-list + +settestcolumnwidth 100 +testsuccessequal "ID Command line Date and Time Action Changes + +0 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...dummy Install 1 " apt history-list + +settestcolumnwidth 60 +testsuccessequal "ID Command line Date and Time Action Changes + +0 aaaaaaaaaaaaaaaaaaaaaaa...dummy Install 1 " apt history-list + + testfailureequal "E: Incorrect usage, no ID was given." apt history-redo testfailureequal "E: Incorrect usage, no ID was given." apt history-undo testfailureequal "E: Incorrect usage, no ID was given." apt history-rollback -- cgit v1.2.3-70-g09d2