Skip to content

Commit c80ba6e

Browse files
committed
[main] rootls: improve recursive printing
The current algorithm didn't properly handle certain cases when the -r flag is used (namely, directories were not printed in a newline) (cherry picked from commit 2d8b749)
1 parent 5bbd061 commit c80ba6e

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

main/src/rootls.cxx

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ static void PrintRNTuple(std::ostream &stream, const ROOT::RNTupleDescriptor &de
281281
std::size_t maxNameLen = 0, maxTypeLen = 0;
282282
std::vector<const ROOT::RFieldDescriptor *> fields;
283283
fields.reserve(rootField.GetLinkIds().size());
284-
for (const auto &field: desc.GetFieldIterable(rootField.GetId())) {
284+
for (const auto &field : desc.GetFieldIterable(rootField.GetId())) {
285285
fields.push_back(&field);
286286
maxNameLen = std::max(maxNameLen, field.GetFieldName().length());
287287
maxTypeLen = std::max(maxTypeLen, field.GetTypeName().length());
@@ -481,11 +481,11 @@ static void PrintNodesInColumns(std::ostream &stream, const RootLsTree &tree,
481481

482482
const bool isTerminal = terminalSize.x + terminalSize.y > 0;
483483

484-
bool mustIndent = false;
484+
auto curCol = 0u;
485485
for (auto i = 0u; i < nNodes; ++i) {
486486
NodeIdx childIdx = nodesBegin[i];
487487
const auto &child = tree.fNodes[childIdx];
488-
if ((i % nCols) == 0 || mustIndent) {
488+
if (curCol == 0) {
489489
PrintIndent(stream, indent);
490490
}
491491

@@ -498,22 +498,39 @@ static void PrintNodesInColumns(std::ostream &stream, const RootLsTree &tree,
498498
stream << Color(kAnsiGreen);
499499
}
500500

501-
const bool isExtremal = !(((i + 1) % nCols) != 0 && i != nNodes - 1);
501+
// Handle line breaks. Lines are broken in the following situations:
502+
// - when the current column number reaches the max number of columns
503+
// - when we are in recursive mode and the item is a directory with children
504+
// - when we are in recursive mode and the NEXT item is a directory with children
505+
506+
const bool isDirWithRecursiveDisplay = isDir && (flags & RootLsArgs::kRecursiveListing) && child.fNChildren > 0;
507+
508+
bool nextIsDirWithRecursiveDisplay = false;
509+
if ((flags & RootLsArgs::kRecursiveListing) && i < nNodes - 1) {
510+
NodeIdx nextChildIdx = nodesBegin[i + 1];
511+
const auto &nextChild = tree.fNodes[nextChildIdx];
512+
nextIsDirWithRecursiveDisplay =
513+
nextChild.fNChildren > 0 && ClassInheritsFrom(nextChild.fClassName.c_str(), "TDirectory");
514+
}
515+
516+
const bool isExtremal = (((curCol + 1) % nCols) == 0) || (i == nNodes - 1) || isDirWithRecursiveDisplay ||
517+
nextIsDirWithRecursiveDisplay;
502518
if (!isExtremal) {
503-
stream << std::left << std::setw(colWidths[i % nCols]) << child.fName;
519+
stream << std::left << std::setw(colWidths[curCol % nCols]) << child.fName;
504520
} else {
505521
stream << std::setw(1) << child.fName;
506522
}
507523
stream << Color(kAnsiNone);
508524

509-
if (isExtremal)
510-
stream << "\n";
525+
if (isExtremal) {
526+
stream << '\n';
527+
curCol = 0;
528+
} else {
529+
++curCol;
530+
}
511531

512-
if (isDir && (flags & RootLsArgs::kRecursiveListing)) {
513-
if (!isExtremal)
514-
stream << "\n";
532+
if (isDirWithRecursiveDisplay) {
515533
PrintChildrenInColumns(stream, tree, childIdx, flags, indent + 2);
516-
mustIndent = true;
517534
}
518535
}
519536
}

roottest/main/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ ROOTTEST_ADD_TEST(RecursiveRootls2
9191
OUTREF RecursiveRootls2.ref
9292
ENVIRONMENT ${test_env})
9393

94+
ROOTTEST_ADD_TEST(RecursiveRootls3
95+
COMMAND ${TOOLS_PREFIX}/rootls${exeext} -r subdirs.root
96+
OUTREF RecursiveRootls3.ref
97+
ENVIRONMENT ${test_env})
98+
9499
ROOTTEST_ADD_TEST(RootlsRNTuple
95100
COMMAND ${TOOLS_PREFIX}/rootls${exeext} -R RNTuple.root
96101
OUTREF RootlsRNTuple.ref

roottest/main/RecursiveRootls3.ref

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
sub
2+
s1 s2
3+
s3
4+
t1
5+
c
6+
sub2 sub3

0 commit comments

Comments
 (0)