Skip to content

Commit fb89590

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)
1 parent 6d3e9ed commit fb89590

File tree

5 files changed

+54
-11
lines changed

5 files changed

+54
-11
lines changed

main/src/rootls.cxx

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

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

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

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

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

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

513-
if (isDir && (flags & RootLsArgs::kRecursiveListing)) {
514-
if (!isExtremal)
515-
stream << "\n";
533+
if (isDirWithRecursiveDisplay) {
516534
PrintChildrenInColumns(stream, tree, childIdx, flags, indent + 2);
517-
mustIndent = true;
518535
}
519536
}
520537
}

roottest/main/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ configure_file(multipleNameCycles.root . COPYONLY)
44
configure_file(MakeNameCyclesRootmvInput.C . COPYONLY)
55
configure_file(subdirs.root . COPYONLY)
66
configure_file(nested.root . COPYONLY)
7+
configure_file(rootlsrecursivetest.root . COPYONLY)
78
configure_file(RNTuple.root . COPYONLY)
89

910
if(MSVC)
@@ -91,6 +92,16 @@ ROOTTEST_ADD_TEST(RecursiveRootls2
9192
OUTREF RecursiveRootls2.ref
9293
ENVIRONMENT ${test_env})
9394

95+
ROOTTEST_ADD_TEST(RecursiveRootls3
96+
COMMAND ${TOOLS_PREFIX}/rootls${exeext} -r subdirs.root
97+
OUTREF RecursiveRootls3.ref
98+
ENVIRONMENT ${test_env})
99+
100+
ROOTTEST_ADD_TEST(RecursiveRootls4
101+
COMMAND ${TOOLS_PREFIX}/rootls${exeext} -r rootlsrecursivetest.root
102+
OUTREF RecursiveRootls4.ref
103+
ENVIRONMENT ${test_env})
104+
94105
ROOTTEST_ADD_TEST(RootlsRNTuple
95106
COMMAND ${TOOLS_PREFIX}/rootls${exeext} -R RNTuple.root
96107
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

roottest/main/RecursiveRootls4.ref

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
dir hprof hpx hpxpy
2+
tof
3+
h0_0N h0_1N h0_2N h0_3N h0_4N h0_5N h0_6N h0_7N h0_8N h0_9N h1_0N h1_1N h1_2N h1_3N h1_4N h1_5N h1_6N h1_7N h1_8N h1_9N
4+
plane0
5+
h0_10N h0_11N h0_12N h0_13N h0_14N h0_15N h0_16N h0_17N h0_18N h0_19N h0_20N h0_21N h0_22N h0_23N h0_24N h0_25N h0_26N h0_27N h0_28N h0_29N h0_30N h0_31N h0_32N h0_33N
6+
h0_34N h0_35N h0_36N h0_37N h0_38N h0_39N h0_40N h0_41N h0_42N h0_43N h0_44N h0_45N h0_46N h0_47N h0_48N h0_49N
7+
plane1
8+
h1_10N h1_11N h1_12N h1_13N h1_14N h1_15N h1_16N h1_17N h1_18N h1_19N h1_20N h1_21N h1_22N h1_23N h1_24N h1_25N h1_26N h1_27N h1_28N h1_29N h1_30N h1_31N h1_32N h1_33N
9+
h1_34N h1_35N h1_36N h1_37N h1_38N h1_39N h1_40N h1_41N h1_42N h1_43N h1_44N h1_45N h1_46N h1_47N h1_48N h1_49N
73.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)