commit 676bed49555fb32f87aae6b5e4f13633a7beda56
parent d4d0a5e1549a19fcb12e9de3fd0b76d954db3112
Author: cfillion <cfillion@users.noreply.github.com>
Date: Wed, 13 Sep 2017 22:40:20 -0400
receipt: sort installed packages alphabetically
Diffstat:
6 files changed, 59 insertions(+), 12 deletions(-)
diff --git a/src/listview.cpp b/src/listview.cpp
@@ -545,10 +545,9 @@ int ListView::Column::compare(const ListView::Cell &cl, const ListView::Cell &cr
switch(dataType) {
case UserType: { // arbitrary data or no data: sort by visible text
- string l = cl.value;
- boost::algorithm::to_lower(l);
+ string l = cl.value, r = cr.value;
- string r = cr.value;
+ boost::algorithm::to_lower(l);
boost::algorithm::to_lower(r);
return l.compare(r);
diff --git a/src/receipt.cpp b/src/receipt.cpp
@@ -19,6 +19,7 @@
#include "index.hpp"
+#include <boost/algorithm/string/case_conv.hpp>
#include <boost/range/adaptor/reversed.hpp>
#include <sstream>
@@ -40,7 +41,7 @@ bool Receipt::empty() const
void Receipt::addInstall(const Version *ver, const Registry::Entry &entry)
{
- m_installs.emplace_back(InstallTicket{ver, entry});
+ m_installs.emplace(InstallTicket{ver, entry});
if(ver->package()->type() == Package::ExtensionType)
m_flags |= RestartNeeded;
@@ -94,6 +95,17 @@ InstallTicket::InstallTicket(const Version *ver, const Registry::Entry &previous
m_index = ver->package()->category()->index()->shared_from_this();
}
+bool InstallTicket::operator<(const InstallTicket &o) const
+{
+ string l = m_version->package()->displayName(),
+ r = o.m_version->package()->displayName();
+
+ boost::algorithm::to_lower(l);
+ boost::algorithm::to_lower(r);
+
+ return l < r;
+}
+
ostream &operator<<(ostream &os, const InstallTicket &t)
{
if(os.tellp() > 0)
diff --git a/src/receipt.hpp b/src/receipt.hpp
@@ -56,7 +56,7 @@ public:
private:
int m_flags;
- std::vector<InstallTicket> m_installs;
+ std::multiset<InstallTicket> m_installs;
std::set<Path> m_removals;
std::set<Path> m_exports;
std::vector<ErrorInfo> m_errors;
@@ -93,6 +93,8 @@ class InstallTicket {
public:
InstallTicket(const Version *ver, const Registry::Entry &previousEntry);
+ bool operator<(const InstallTicket &) const;
+
private:
friend std::ostream &operator<<(std::ostream &, const InstallTicket &);
diff --git a/src/source.cpp b/src/source.cpp
@@ -20,7 +20,7 @@
#include "errors.hpp"
#include "index.hpp"
-#include <boost/algorithm/string.hpp>
+#include <boost/algorithm/string/case_conv.hpp>
using namespace std;
diff --git a/src/string.cpp b/src/string.cpp
@@ -42,7 +42,7 @@ string String::format(const char *fmt, ...)
string String::indent(const string &text)
{
- ostringstream output;
+ string output;
istringstream input(text);
string line;
bool first = true;
@@ -51,13 +51,16 @@ string String::indent(const string &text)
if(first)
first = false;
else
- output << "\r\n";
+ output += "\r\n";
boost::algorithm::trim(line);
- if(!line.empty())
- output << "\x20\x20" << line;
+ if(line.empty())
+ continue;
+
+ output += "\x20\x20";
+ output += line;
}
- return output.str();
+ return output;
}
diff --git a/test/receipt.cpp b/test/receipt.cpp
@@ -79,7 +79,7 @@ TEST_CASE("format receipt page title", M) {
}
}
-TEST_CASE("format install ticket") {
+TEST_CASE("format install ticket", M) {
IndexPtr ri = make_shared<Index>("Index Name");
Category cat("Category Name", ri.get());
Package pkg(Package::ScriptType, "Package Name", &cat);
@@ -130,3 +130,34 @@ TEST_CASE("format install ticket") {
Contains("v1.0") && !Contains("v2.0") && !Contains("v3.0"));
}
}
+
+TEST_CASE("sort InstallTickets (case insensitive)", M) {
+ IndexPtr ri = make_shared<Index>("Index Name");
+ Category cat("Category Name", ri.get());
+ Package pkg1(Package::ScriptType, "a test", &cat);
+ Version ver1("1.0", &pkg1);
+
+ Package pkg2(Package::ScriptType, "Uppercase Name", &cat);
+ Version ver2("1.0", &pkg2);
+
+ Package pkg3(Package::ScriptType, "unused name", &cat);
+ pkg3.setDescription("z is the last letter");
+ Version ver3("1.0", &pkg3);
+
+ REQUIRE(InstallTicket(&ver1, {}) < InstallTicket(&ver2, {}));
+ REQUIRE(InstallTicket(&ver2, {}) < InstallTicket(&ver3, {}));
+ REQUIRE_FALSE(InstallTicket(&ver1, {}) < InstallTicket(&ver1, {}));
+ REQUIRE_FALSE(InstallTicket(&ver2, {}) < InstallTicket(&ver1, {}));
+
+ Receipt r;
+ r.addInstall(&ver1, {}); // a test
+ r.addInstall(&ver3, {}); // z is the last letter
+ r.addInstall(&ver1, {}); // a test (duplicate)
+ r.addInstall(&ver2, {}); // Uppercase Name
+ const string page = r.pages()[0].contents();
+ REQUIRE(page.find(pkg1.name()) < page.find(pkg2.name()));
+ REQUIRE(page.find(pkg2.name()) < page.find(pkg3.name()));
+
+ // duplicate should be preserved
+ REQUIRE(page.find(pkg1.name()) < page.rfind(pkg1.name()));
+}