Skip to content

Commit cbb3424

Browse files
committed
tree(): support multi-line labels
1 parent afda742 commit cbb3424

File tree

3 files changed

+115
-2
lines changed

3 files changed

+115
-2
lines changed

R/tree.R

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ tree <- function(
141141
num_root <- match(root, data[[1]])
142142
if (is.na(num_root)) return()
143143

144-
level <- length(n) - 1
145144
prefix <- vcapply(seq_along(n), function(i) {
146145
if (n[i] < mx[i]) {
147146
if (i == length(n)) {
@@ -159,7 +158,37 @@ tree <- function(
159158
root_seen <- root %in% seen
160159
root_lab <- if (trim && root_seen) trimlabs[[num_root]] else
161160
labels[[num_root]]
162-
res <<- c(res, paste0(paste(prefix, collapse = ""), root_lab))
161+
162+
# multi-line labels
163+
prefix2 <- if (grepl("\n", root_lab, fixed = TRUE)) {
164+
root_lab <- ansi_strsplit(root_lab, "\n", fixed = TRUE)[[1]]
165+
vcapply(seq_along(n), function(i) {
166+
if (n[i] < mx[i]) {
167+
if (i == length(n)) {
168+
paste0(style$v, " ")
169+
} else {
170+
paste0(style$v, " ")
171+
}
172+
} else if (n[i] == mx[i] && i == length(n)) {
173+
" "
174+
} else {
175+
" "
176+
}
177+
})
178+
}
179+
180+
res <<- c(
181+
res,
182+
paste0(
183+
c(
184+
paste(prefix, collapse = ""),
185+
if (length(root_lab) > 1) {
186+
rep(paste(prefix2, collapse = ""), length(root_lab) - 1)
187+
}
188+
),
189+
root_lab
190+
)
191+
)
163192

164193
# Detect infinite loops
165194
if (!trim && root %in% used) {

tests/testthat/_snaps/tree.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,25 @@
2222
\-rprojroot
2323
\-backports
2424

25+
---
26+
27+
Code
28+
tree(data, root = "callr")
29+
Output
30+
callr
31+
+-processx
32+
| +-assertthat
33+
| | still going
34+
| | third row
35+
| +-crayon
36+
| | and some more
37+
| +-debugme
38+
| | more debug
39+
| | \-crayon
40+
| | and some more
41+
| \-R6
42+
\-R6
43+
2544
# tree [ansi]
2645

2746
Code
@@ -46,6 +65,25 @@
4665
\-rprojroot
4766
\-backports
4867

68+
---
69+
70+
Code
71+
tree(data, root = "callr")
72+
Output
73+
callr
74+
+-processx
75+
| +-assertthat
76+
| | still going
77+
| | third row
78+
| +-crayon
79+
| | and some more
80+
| +-debugme
81+
| | more debug
82+
| | \-crayon
83+
| | and some more
84+
| \-R6
85+
\-R6
86+
4987
# tree [unicode]
5088

5189
Code
@@ -70,6 +108,25 @@
70108
└─rprojroot
71109
└─backports
72110

111+
---
112+
113+
Code
114+
tree(data, root = "callr")
115+
Output
116+
callr
117+
├─processx
118+
│ ├─assertthat
119+
│ │ still going
120+
│ │ third row
121+
│ ├─crayon
122+
│ │ and some more
123+
│ ├─debugme
124+
│ │ more debug
125+
│ │ └─crayon
126+
│ │ and some more
127+
│ └─R6
128+
└─R6
129+
73130
# tree [fancy]
74131

75132
Code
@@ -94,6 +151,25 @@
94151
└─rprojroot
95152
└─backports
96153

154+
---
155+
156+
Code
157+
tree(data, root = "callr")
158+
Output
159+
callr
160+
├─processx
161+
│ ├─assertthat
162+
│ │ still going
163+
│ │ third row
164+
│ ├─crayon
165+
│ │ and some more
166+
│ ├─debugme
167+
│ │ more debug
168+
│ │ └─crayon
169+
│ │ and some more
170+
│ └─R6
171+
└─R6
172+
97173
# trimming [plain]
98174

99175
Code

tests/testthat/test-tree.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ test_that_cli("tree", {
6161

6262
expect_snapshot(tree(data, root = "desc"))
6363

64+
# multi-line labels
65+
data$label <- data$package
66+
data$label[3] <- "assertthat\nstill going\nthird row"
67+
data$label[13] <- col_red("crayon\nand some more")
68+
data$label[14] <- "debugme\nmore debug"
69+
70+
expect_snapshot(tree(data, root = "callr"))
71+
6472
# Check that trees with apparent circularity error nicely
6573
data <- data.frame(
6674
stringsAsFactors = FALSE,

0 commit comments

Comments
 (0)