Debian Codegraph @

R/ctl_colonnade.R

raw
1 # Adapted from squeeze_impl()
2 ctl_colonnade <- function(x, has_row_id = TRUE, width = NULL,
3 controller = new_tbl(), focus = NULL) {
4 "!!!!DEBUG ctl_colonnade()"
5
6 x <- new_data_frame(x, names = names2(x))
7 width <- get_width_print(width)
8
9 stopifnot(all(focus %in% names(x)))
10
11 n <- nrow(x)
12 nc <- ncol(x)
13
14 if (n == 0 || nc == 0) {
15 return(new_colonnade_body(list(), extra_cols = x, abbrev_cols = character(), abbrev_col_idxs = numeric()))
16 }
17
18 if (is_false(has_row_id)) {
19 rowid <- NULL
20 } else {
21 rowid <- ctl_new_rowid_pillar(controller, x, width,
22 title = NULL,
23 type = if (isTRUE(has_row_id)) NULL else has_row_id
24 )
25 }
26
27 # Reserve space for rowid column in each tier
28 if (is.null(rowid)) {
29 rowid_width <- 0L
30 } else {
31 rowid_width <- get_width(rowid)
32 }
33
34 has_star <- identical(has_row_id, "*")
35
36 # FIXME: Support for Inf?
37 tier_widths <- get_tier_widths(width, nc, rowid_width + 1L)
38
39 formatted_tiers <- list()
40 extra_cols <- list(a = 1)[0]
41 abbrev_cols <- character()
42 abbrev_col_idxs <- numeric()
43
44 on_tier <- function(formatted) {
45 # writeLines(formatted)
46 formatted_tiers <<- c(formatted_tiers, list(formatted))
47 }
48
49 on_extra_cols <- function(my_extra_cols) {
50 # print(extra_cols)
51
52 new_extra_cols <- pmap(my_extra_cols, function(x, title, cols) {
53 out <- as.list(x)[cols]
54 if (is.null(title)) {
55 return(out)
56 }
57
58 if (length(out) > 1) {
59 title_empty <- rep_along(title, "")
60 new_names <- paste0(paste0(title_empty, "$", collapse = ""), names(out))
61 new_names[[1]] <- paste0(paste0(title, "$", collapse = ""), names(out)[[1]])
62 names(out) <- new_names
63 } else {
64 # Also account for the case of packed matrices here
65 names(out) <- prepare_title(c(title, names(out)))
66 }
67 out
68 })
69
70 # Called at most once:
71 stopifnot(length(extra_cols) == 0)
72 extra_cols <<- unlist(new_extra_cols, recursive = FALSE)
73 }
74
75 on_abbrev_col <- function(title) {
76 abbrev_cols <<- c(abbrev_cols, title)
77 }
78
79 on_get_n_abbrev_cols <- function() {
80 length(abbrev_cols)
81 }
82
83 cb <- new_emit_tiers_callbacks(
84 controller, rowid, rowid_width, has_star,
85 on_tier, on_extra_cols, on_abbrev_col, on_get_n_abbrev_cols
86 )
87
88 # Side effect: populate formatted_tiers, extra_cols, and abbrev_cols
89 do_emit_tiers(x, tier_widths, length(focus), cb, focus)
90
91 new_colonnade_body(formatted_tiers, extra_cols, abbrev_cols, abbrev_col_idxs)
92 }
93
94 new_emit_tiers_callbacks <- function(controller, rowid, rowid_width, has_star,
95 on_tier, on_extra_cols, on_abbrev_col, on_get_n_abbrev_cols) {
96 list(
97 controller = controller,
98 rowid = rowid,
99 rowid_width = rowid_width,
100 has_star = has_star,
101 on_tier = on_tier,
102 on_extra_cols = on_extra_cols,
103 on_abbrev_col = on_abbrev_col,
104 on_get_n_abbrev_cols = on_get_n_abbrev_cols
105 )
106 }
107
108 do_emit_tiers <- function(x, tier_widths, n_focus, cb, focus) {
109 formatted_list <- NULL
110 extra_cols <- data_frame(x = list(), title = list(), cols = list())
111
112 on_start_tier <- function() {
113 # message("on_start_tier()")
114 formatted_list <<- list()
115 }
116
117 on_end_tier <- function() {
118 # message("on_end_tier()")
119 if (length(formatted_list) > 0) {
120 if (!is.null(cb$rowid)) {
121 formatted_list <- c(list(pillar_format_parts_2(cb$rowid, cb$rowid_width)), formatted_list)
122 }
123
124 aligned <- map(formatted_list, `[[`, "aligned")
125
126 tier <- format_colonnade_tier_2(aligned, bidi = get_pillar_option_bidi())
127
128 cb$on_tier(tier)
129 }
130 formatted_list <<- NULL
131 }
132
133 on_pillar <- function(formatted) {
134 # message("pillar()")
135 # print(formatted)
136 # print(pillar, width = width)
137 formatted_list <<- c(formatted_list, list(formatted))
138 }
139
140 on_extra_cols <- function(x, title, cols) {
141 # message("extra_cols()")
142 # print(title)
143 # print(cols)
144 new_extra_cols <- data_frame(
145 x = list(x), title = list(title), cols = list(cols)
146 )
147 # Add to the back, extra columns are emitted in order:
148 extra_cols <<- vec_rbind(extra_cols, new_extra_cols)
149 }
150
151 cb_pillars <- new_emit_pillars_callbacks(
152 cb$controller,
153 on_start_tier = on_start_tier,
154 on_end_tier = on_end_tier,
155 on_pillar = on_pillar,
156 on_top_level_pillar = function(...) {},
157 on_extra_cols = on_extra_cols,
158 on_abbrev_col = cb$on_abbrev_col,
159 on_get_n_abbrev_cols = cb$on_get_n_abbrev_cols
160 )
161
162 # Side effect: populate `extra_cols`,
163 # aggregate and forward calls to higher-level callbacks in `cb`
164 emit_pillars(x, tier_widths, cb_pillars, focus)
165
166 cb$on_extra_cols(extra_cols)
167 }
168
169 new_emit_pillars_callbacks <- function(controller,
170 on_start_tier,
171 on_end_tier,
172 on_pillar,
173 on_top_level_pillar,
174 on_extra_cols,
175 on_abbrev_col,
176 on_get_n_abbrev_cols) {
177 list(
178 controller = controller,
179 on_start_tier = on_start_tier,
180 on_end_tier = on_end_tier,
181 on_pillar = on_pillar,
182 on_top_level_pillar = on_top_level_pillar,
183 on_extra_cols = on_extra_cols,
184 on_abbrev_col = on_abbrev_col,
185 on_get_n_abbrev_cols = on_get_n_abbrev_cols
186 )
187 }
188
189 emit_pillars <- function(x, tier_widths, cb, focus) {
190 cb$on_start_tier()
191 do_emit_focus_pillars(x, tier_widths, cb, focus)
192 cb$on_end_tier()
193 }
194
195 do_emit_focus_pillars <- function(x, tier_widths, cb, focus) {
196 stopifnot(is.data.frame(x))
197
198 focus <- sort(match(focus, names(x)))
199 # Shortcut
200 if (length(focus) == 0) {
201 do_emit_pillars(x, tier_widths, cb)
202 return()
203 }
204
205 focus_formatted_list <- list()
206 focus_top_level_end_idx <- integer()
207 focus_extra_cols <- list()
208
209 on_focus_pillar <- function(formatted) {
210 # message("pillar()")
211 # print(formatted)
212 # print(pillar, width = width)
213 focus_formatted_list <<- c(focus_formatted_list, list(formatted))
214 }
215
216 on_focus_top_level_pillar <- function() {
217 focus_top_level_end_idx <<- c(focus_top_level_end_idx, length(focus_formatted_list))
218 }
219
220 on_focus_extra_cols <- function(x, title, cols) {
221 # message("extra_cols()")
222 # print(title)
223 # print(cols)
224 new_extra_cols <- data_frame(x = list(x), title = list(title), cols = list(cols))
225 n_focus_formatted_list <- length(focus_formatted_list)
226
227 if (n_focus_formatted_list <= length(focus_extra_cols)) {
228 focus_extra_cols[[n_focus_formatted_list]] <<- vec_rbind(focus_extra_cols[[n_focus_formatted_list]], new_extra_cols)
229 } else {
230 focus_extra_cols[[n_focus_formatted_list]] <<- new_extra_cols
231 }
232 }
233
234 cb_focus <- new_emit_pillars_callbacks(
235 controller = cb$controller,
236 on_start_tier = function(...) {},
237 on_end_tier = function(...) {},
238 on_pillar = on_focus_pillar,
239 on_top_level_pillar = on_focus_top_level_pillar,
240 on_extra_cols = on_focus_extra_cols,
241 on_abbrev_col = cb$on_abbrev_col,
242 on_get_n_abbrev_cols = cb$on_get_n_abbrev_cols
243 )
244
245 # Side effect: populates focus_formatted_list and focus_extra_cols
246 do_emit_pillars(x[focus], tier_widths, cb_focus, is_focus = TRUE)
247
248 # Can't show focus pillars that don't fit, but need to iterate
249 # to emit extra columns in the correct order
250 length(focus_extra_cols) <- length(focus)
251 n_focus_shown <- length(focus_top_level_end_idx)
252
253 before_start_idx <- vec_lag(focus + 1L, default = 1L)
254 before_end_idx <- focus - 1L
255
256 focus_top_level_start_idx <- vec_lag(focus_top_level_end_idx + 1L, default = 1L)
257
258 # Apply similar strategy as in do_emit_pillars(), but ensure that
259 # focus pillars are shown
260 widths_focus <- map_int(focus_formatted_list, `[[`, "max_extent")
261 rev <- distribute_pillars_rev(widths_focus, tier_widths)
262 stopifnot(!anyNA(rev$tier))
263
264 # This indicates the limit until which we expand non-focus column
265 # before the current focus column:
266 rev$offset_before <- pmax(rev$offset_after - rev$width - 1L, 0L)
267 rev_before <- rev[focus_top_level_start_idx, ]
268 stopifnot(nrow(rev_before) == n_focus_shown)
269
270 # This indicates how far the current focus column (with all sub-pillars)
271 # can extend. We use it for convenience to use the same logic as
272 # do_emit_pillars().
273 rev_after <- rev[focus_top_level_end_idx, ]
274 stopifnot(nrow(rev_after) == n_focus_shown)
275
276 x_pos <- 0L
277 tier_pos <- 1L
278
279 for (col in seq_along(focus)) {
280 # Emit extra columns for focus pillars before processing non-focus pillars,
281 # to keep extra columns in order between focus and non-focus pillars:
282 my_extra_cols <- focus_extra_cols[[col]]
283 for (extra_cols_row in seq_len(NROW(my_extra_cols))) {
284 cb$on_extra_cols(
285 my_extra_cols$x[[extra_cols_row]],
286 my_extra_cols$title[[extra_cols_row]],
287 my_extra_cols$cols[[extra_cols_row]]
288 )
289 }
290
291 start <- before_start_idx[[col]]
292 end <- before_end_idx[[col]]
293
294 # Emit non-focus pillars that fit: use offset_before
295 if (start <= end) {
296 sub_tier_widths <- compute_sub_tier_widths(
297 tier_widths, x_pos, tier_pos,
298 rev_before$offset_before[[col]], rev_before$tier[[col]]
299 )
300
301 adv <- advance_emit_pillars(x_pos, tier_pos, x[seq2(start, end)], sub_tier_widths, cb)
302 x_pos <- adv$x_pos
303 tier_pos <- adv$tier_pos
304 }
305
306 # Emit already formatted focus pillar(s):
307 if (col <= length(focus_top_level_start_idx)) {
308 focus_pillars <- seq2(focus_top_level_start_idx[[col]], focus_top_level_end_idx[[col]])
309 } else {
310 focus_pillars <- integer()
311 }
312
313 for (focus_pillar in focus_pillars) {
314 # Deduct widths: use offset_after
315 sub_tier_widths <- compute_sub_tier_widths(
316 tier_widths, x_pos, tier_pos,
317 rev_after$offset_after[[col]], rev_after$tier[[col]]
318 )
319
320 used <- compute_used_width(sub_tier_widths, widths_focus[[focus_pillar]])
321
322 if (used$tiers > 0) {
323 cb$on_end_tier()
324 cb$on_start_tier()
325 }
326
327 cb$on_pillar(focus_formatted_list[[focus_pillar]])
328
329 adv <- advance_pos(x_pos, tier_pos, used)
330 x_pos <- adv$x_pos
331 tier_pos <- adv$tier_pos
332 }
333 }
334
335 # Emit pillars after focus pillar
336 if (length(focus) > 0) {
337 start <- focus[[length(focus)]] + 1L
338 } else {
339 start <- 1L
340 }
341 end <- length(x)
342
343 # Emit non-focus pillars that fit: use offset_before
344 if (start <= end) {
345 sub_tier_widths <- compute_sub_tier_widths(
346 tier_widths, x_pos, tier_pos,
347 tier_widths[[length(tier_widths)]], length(tier_widths)
348 )
349
350 adv <- advance_emit_pillars(x_pos, tier_pos, x[seq2(start, end)], sub_tier_widths, cb)
351 x_pos <- adv$x_pos
352 tier_pos <- adv$tier_pos
353 }
354 }
355
356 advance_emit_pillars <- function(x_pos, tier_pos, ...) {
357 used <- do_emit_pillars(...)
358 advance_pos(x_pos, tier_pos, used)
359 }
360
361 do_emit_pillars <- function(x, tier_widths, cb, title = NULL, first_pillar = NULL, parent_col_idx = NULL, is_focus = FALSE) {
362 top_level <- is.null(first_pillar)
363
364 # Only tweaking sub-title, because full title is needed for extra-cols
365 sub_title <- title
366 if (!is.null(sub_title)) {
367 sub_title[-length(sub_title)][parent_col_idx[-1] != 1] <- ""
368 }
369
370 pillar_list <- ctl_new_pillar_list(cb$controller, x, width = tier_widths, title = sub_title, first_pillar = first_pillar)
371
372 # Extra columns are known early on, and remain fixed
373 extra <- attr(pillar_list, "extra")
374
375 if (length(pillar_list) == 0) {
376 emit_extra_cols(extra, x, title, cb)
377 # Doesn't fit
378 return(NULL)
379 }
380
381 # Simple pillar: fit and proceed
382 if (isTRUE(attr(pillar_list, "simple"))) {
383 pillar <- pillar_list[[1]]
384
385 pillar_title <- pillar[["title"]]
386 title_width <- get_width(pillar_title) %||% 0L
387
388 formatted <- pillar_format_parts_2(
389 pillar,
390 max(tier_widths),
391 is_focus,
392 cb$on_get_n_abbrev_cols() + 1L
393 )
394 true_width <- formatted$max_extent
395 stopifnot(true_width <= max(tier_widths))
396
397 used <- compute_used_width(tier_widths, true_width)
398
399 if (used$tiers > 0) {
400 cb$on_end_tier()
401 cb$on_start_tier()
402 }
403
404 cb$on_pillar(formatted)
405
406 if (true_width < title_width) {
407 cb$on_abbrev_col(format(pillar_title))
408 }
409
410 return(used)
411 }
412
413 # We can proceed cautiously to the next level if space permits.
414 # For each sub-pillar we allow at most as much space so that
415 # we can print all first components of all remaining pillars
416 # with the minimum width
417 min_widths <- map_int(pillar_list, pillar_get_min_width)
418 rev <- distribute_pillars_rev(min_widths, tier_widths)
419 stopifnot(!anyNA(rev$tier))
420
421 x_pos <- 0L
422 tier_pos <- 1L
423
424 # Advance column by column
425 for (col in seq_along(pillar_list)) {
426 sub_tier_widths <- compute_sub_tier_widths(
427 tier_widths, x_pos, tier_pos,
428 rev$offset_after[[col]], rev$tier[[col]]
429 )
430 "!!!!!DEBUG sub_tier_widths"
431
432 # Recurse
433 used <- do_emit_pillars(
434 x[[col]],
435 sub_tier_widths,
436 cb,
437 c(title, tick_if_needed(names(x)[[col]])),
438 pillar_list[[col]],
439 c(parent_col_idx, if (!is.null(names(x))) col),
440 is_focus
441 )
442 "!!!!!DEBUG used"
443
444 adv <- advance_pos(x_pos, tier_pos, used)
445 x_pos <- adv$x_pos
446 tier_pos <- adv$tier_pos
447
448 if (top_level) {
449 cb$on_top_level_pillar()
450 }
451 }
452
453 # We emit late to ensure that extra columns of compound pillars
454 # appear before top-level extra columns.
455 emit_extra_cols(extra, x, title, cb)
456
457 list(tiers = tier_pos - 1L, width = x_pos)
458 }
459
460 compute_used_width <- function(tier_widths, width) {
461 if (width <= max(tier_widths)) {
462 # Handle tier break
463 used_tier <- which(width <= tier_widths)[[1]]
464 } else {
465 used_tier <- which.max(tier_widths)
466 width <- tier_widths[[used_tier]]
467 }
468 list(tiers = used_tier - 1L, width = width)
469 }
470
471 compute_sub_tier_widths <- function(tier_widths, x_pos, tier_pos, x_target, tier_target) {
472 stopifnot(tier_pos <= tier_target)
473
474 if (tier_pos == tier_target) {
475 sub_tier_widths <- x_target - x_pos
476 } else {
477 sub_tier_widths <- c(
478 tier_widths[[tier_pos]] - x_pos,
479 tier_widths[seq2(tier_pos + 1L, tier_target - 1L)],
480 x_target
481 )
482 }
483 if (x_pos > 0) {
484 sub_tier_widths[[1]] <- max(sub_tier_widths[[1]] - 1L, 0L)
485 }
486 stopifnot(sub_tier_widths >= 0)
487 sub_tier_widths
488 }
489
490 advance_pos <- function(x_pos, tier_pos, used) {
491 if (!is.null(used)) {
492 if (used$tiers > 0) {
493 x_pos <- used$width
494 tier_pos <- tier_pos + used$tiers
495 } else {
496 if (x_pos > 0) {
497 x_pos <- x_pos + 1L
498 }
499 x_pos <- x_pos + used$width
500 }
501 }
502 list(x_pos = x_pos, tier_pos = tier_pos)
503 }
504
505 emit_extra_cols <- function(extra, x, title, cb) {
506 if (length(extra) == 0) {
507 return()
508 }
509
510 if (is.numeric(extra)) {
511 if (length(extra) == 1) {
512 extra <- paste0("[", extra, "]")
513 } else {
514 extra <- paste0("[", min(extra), ":", max(extra), "]")
515 }
516 x_extra <- set_names(list(x[1, ]), extra)
517 } else {
518 extra <- tick_if_needed(extra)
519 x_extra <- tick_names_if_needed(x)
520 }
521
522 cb$on_extra_cols(x_extra, title, extra)
523 }
524
525 # Reference: https://www.w3.org/International/questions/qa-bidi-unicode-controls
526 fsi <- function(x) {
527 paste0("\u2068", x, "\u2069")
528 }
529
530 lro <- function(x) {
531 paste0("\u202d", x, "\u202c")
532 }
533
534 format_colonnade_tier_2 <- function(x, bidi = FALSE) {
535 if (length(x) == 0) {
536 return(character())
537 }
538
539 if (bidi) {
540 x <- map(x, fsi)
541 }
542
543 out <- exec(paste, !!!x)
544
545 if (bidi) {
546 out <- lro(out)
547 }
548
549 out
550 }
551
552 new_colonnade_body <- function(x, extra_cols, abbrev_cols, abbrev_col_idxs = numeric()) {
553 "!!!!!DEBUG new_colonnade_body()"
554
555 body <- as_glue(as.character(unlist(x)))
556 extra_cols <- as.list(extra_cols)
557
558 list(
559 body = body,
560 extra_cols = extra_cols,
561 abbrev_cols = abbrev_cols,
562 abbrev_col_idxs = abbrev_col_idxs
563 )
564 }