CLI: Add 'flatten' option to the 'ls' command (#3276)
* Fixes #925 * Add 'flatten' option to CLI ls command * Add test for Group::hierarchy() and man page for ls --flatten * Rename group sort test to align with others
This commit is contained in:
committed by
Jonathan White
parent
1e915eef89
commit
05c11d1b7c
@@ -504,16 +504,25 @@ void Group::setParent(Database* db)
|
||||
QObject::setParent(db);
|
||||
}
|
||||
|
||||
QStringList Group::hierarchy() const
|
||||
QStringList Group::hierarchy(int height) const
|
||||
{
|
||||
QStringList hierarchy;
|
||||
const Group* group = this;
|
||||
const Group* parent = m_parent;
|
||||
|
||||
if (height == 0) {
|
||||
return hierarchy;
|
||||
}
|
||||
|
||||
hierarchy.prepend(group->name());
|
||||
|
||||
while (parent) {
|
||||
int level = 1;
|
||||
bool heightReached = level == height;
|
||||
|
||||
while (parent && !heightReached) {
|
||||
group = group->parentGroup();
|
||||
parent = group->parentGroup();
|
||||
heightReached = ++level == height;
|
||||
|
||||
hierarchy.prepend(group->name());
|
||||
}
|
||||
@@ -720,25 +729,34 @@ Group* Group::findGroupByPathRecursive(const QString& groupPath, const QString&
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
QString Group::print(bool recursive, int depth)
|
||||
QString Group::print(bool recursive, bool flatten, int depth)
|
||||
{
|
||||
|
||||
QString response;
|
||||
QString indentation = QString(" ").repeated(depth);
|
||||
QString prefix;
|
||||
|
||||
if (flatten) {
|
||||
const QString separator("/");
|
||||
prefix = hierarchy(depth).join(separator);
|
||||
if (!prefix.isEmpty()) {
|
||||
prefix += separator;
|
||||
}
|
||||
} else {
|
||||
prefix = QString(" ").repeated(depth);
|
||||
}
|
||||
|
||||
if (entries().isEmpty() && children().isEmpty()) {
|
||||
response += indentation + tr("[empty]", "group has no children") + "\n";
|
||||
response += prefix + tr("[empty]", "group has no children") + "\n";
|
||||
return response;
|
||||
}
|
||||
|
||||
for (Entry* entry : entries()) {
|
||||
response += indentation + entry->title() + "\n";
|
||||
response += prefix + entry->title() + "\n";
|
||||
}
|
||||
|
||||
for (Group* innerGroup : children()) {
|
||||
response += indentation + innerGroup->name() + "/\n";
|
||||
response += prefix + innerGroup->name() + "/\n";
|
||||
if (recursive) {
|
||||
response += innerGroup->print(recursive, depth + 1);
|
||||
response += innerGroup->print(recursive, flatten, depth + 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -143,7 +143,7 @@ public:
|
||||
Group* parentGroup();
|
||||
const Group* parentGroup() const;
|
||||
void setParent(Group* parent, int index = -1);
|
||||
QStringList hierarchy() const;
|
||||
QStringList hierarchy(int height = -1) const;
|
||||
bool hasChildren() const;
|
||||
|
||||
Database* database();
|
||||
@@ -163,7 +163,7 @@ public:
|
||||
CloneFlags groupFlags = DefaultCloneFlags) const;
|
||||
|
||||
void copyDataFrom(const Group* other);
|
||||
QString print(bool recursive = false, int depth = 0);
|
||||
QString print(bool recursive = false, bool flatten = false, int depth = 0);
|
||||
|
||||
void addEntry(Entry* entry);
|
||||
void removeEntry(Entry* entry);
|
||||
|
||||
Reference in New Issue
Block a user