Skip to content

Commit

Permalink
Merge pull request #274 from r-lib/f-align-once
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr authored Mar 7, 2021
2 parents 385f844 + 1858d46 commit a2cb096
Show file tree
Hide file tree
Showing 13 changed files with 297 additions and 55 deletions.
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ S3method(format,pillar_1e)
S3method(format,pillar_capital)
S3method(format,pillar_colonnade)
S3method(format,pillar_continuation_shaft)
S3method(format,pillar_empty_shaft)
S3method(format,pillar_ornament)
S3method(format,pillar_rif_shaft)
S3method(format,pillar_rif_title)
Expand Down
18 changes: 14 additions & 4 deletions R/ctl_colonnade.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,30 @@ ctl_colonnade <- function(x, has_row_id = TRUE, width = NULL, controller = new_t
})
}

out <- map(col_widths_tiers, function(tier) {
flat_tiers <- map(col_widths_tiers, function(tier) {
map2(tier$pillar, tier$width, pillar_format_parts_2)
})

out <- map(flat_tiers, format_colonnade_tier_2)

extra_cols <- x[seq2(length(pillars) + 1L, nc)]
new_colonnade_body(out, extra_cols = extra_cols)
}

format_colonnade_tier_2 <- function(x) {
"!!!!!DEBUG format_colonnade_tier_2(`v(x)`)"

if (length(x) == 0) {
return(character())
}

exec(paste, !!!x)
}

new_colonnade_body <- function(x, extra_cols) {
"!!!!!DEBUG new_colonnade_body()"

formatted_tiers <- map(x, format_colonnade_tier)
formatted <- new_vertical(as.character(unlist(formatted_tiers)))

formatted <- new_vertical(as.character(unlist(x)))
new_vertical(formatted, extra_cols = extra_cols)
}

Expand Down
6 changes: 3 additions & 3 deletions R/ctl_compound.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ new_data_frame_pillar <- function(x, controller, width, title) {
new_packed_pillars <- function(x, controller, width, title) {
"!!!!!DEBUG new_packed_pillars(`v(width)`, `v(title)`)"

if (length(x) == 0) {
if (ncol(x) == 0) {
return(compact(list(pillar_from_shaft(
new_pillar_title(prepare_title(title)),
new_pillar_type(x),
new_empty_shaft(),
new_empty_shaft(nrow(x)),
width
))))
}
Expand Down Expand Up @@ -63,7 +63,7 @@ new_matrix_pillar <- function(x, controller, width, title) {
return(pillar_from_shaft(
new_pillar_title(prepare_title(title)),
new_pillar_type(x),
new_empty_shaft(),
new_empty_shaft(nrow(x)),
width
))
}
Expand Down
3 changes: 3 additions & 0 deletions R/ctl_new_pillar.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
#' for the titles and types of the sub-pillar.
#' Users will only rarely need to override this method if ever.
#'
#' All components must be of the same height.
#' This restriction may be levied in the future.
#'
#' Implementations should return `NULL` if none of the data
#' fits the available width.
#'
Expand Down
4 changes: 1 addition & 3 deletions R/ctl_pillar.R
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,7 @@ format.pillar <- function(x, width = NULL, ...) {
width <- sum(widths) - length(widths) + 1L
}

out <- pillar_format_parts_2(x, width)

new_vertical(unlist(unname(out)))
new_vertical(pillar_format_parts_2(x, width))
}

#' @export
Expand Down
2 changes: 1 addition & 1 deletion R/ctl_pillar_component.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,5 @@ pillar_format_parts_2 <- function(x, width) {

alignment <- attr(formatted[["data"]], "align") %||% "left"

map(formatted, align, width = width, align = alignment)
align(unlist(formatted), width = width, align = alignment)
}
12 changes: 2 additions & 10 deletions R/empty-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@ style_empty <- function(n) {
""
}

new_empty_shaft <- function() {
new_pillar_shaft(list(), class = "pillar_empty_shaft", width = 0)
}

#' @export
format.pillar_empty_shaft <- function(x, width, ...) {
new_ornament(
style_empty(),
width = width
)
new_empty_shaft <- function(height = 1) {
new_pillar_shaft_simple(rep("", height), width = 0)
}
5 changes: 0 additions & 5 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

## Next steps

- Reduce calls to `align()`:
- align all components vertically
- squash
- only then `align()`
- paste
- Benchmark and profile again
- `bench.R`
- https://github.com/tidyverse/tibble/issues/598
Expand Down
3 changes: 3 additions & 0 deletions man/ctl_new_pillar.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

132 changes: 120 additions & 12 deletions tests/testthat/_snaps/ctl_colonnade.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@
3 NA
Code
ctl_colonnade(list(`
` = c("\n", "\""), `` = factor("\n")), width = 30)
` = c("\n", "\""), `` = factor(c("\n", "\n"))), width = 30)
Output
`\n` `\r`
<chr> <fct>
Expand Down Expand Up @@ -224,6 +224,114 @@
Code
# dummy

# color, options: UTF-8 is TRUE

Code
style_na("NA")
Output
[1] "\033[31mNA\033[39m"
Code
style_neg("-1")
Output
[1] "\033[31m-1\033[39m"

---

Code
xf <- (function() ctl_colonnade(list(x = c((10^(-3:4)) * c(-1, 1), NA))))
print(xf())
Output
x
<dbl>
1 -0.001
2 0.01
3 -0.1
4 1
5 -10
6 100
7 -1000
8 10000
9 NA
Code
with_options(pillar.subtle_num = TRUE, print(xf()))
Output
x
<dbl>
1 -0.001
2 0.01
3 -0.1
4 1
5 -10
6 100
7 -1000
8 10000
9 NA
Code
with_options(pillar.subtle = FALSE, print(xf()))
Output
x
<dbl>
1 -0.001
2 0.01
3 -0.1
4 1
5 -10
6 100
7 -1000
8 10000
9 NA
Code
with_options(pillar.neg = FALSE, print(xf()))
Output
x
<dbl>
1 -0.001
2 0.01
3 -0.1
4 1
5 -10
6 100
7 -1000
8 10000
9 NA
Code
with_options(pillar.subtle = FALSE, pillar.neg = FALSE, print(xf()))
Output
x
<dbl>
1 -0.001
2 0.01
3 -0.1
4 1
5 -10
6 100
7 -1000
8 10000
9 NA
Code
with_options(pillar.bold = TRUE, print(xf()))
Output
x
<dbl>
1 -0.001
2 0.01
3 -0.1
4 1
5 -10
6 100
7 -1000
8 10000
9 NA

---

Code
ctl_colonnade(list(a_very_long_column_name = 0), width = 20)
Output
a_very_long_colum…
<dbl>
1 0

# color, options: UTF-8 is FALSE

Code
Expand Down Expand Up @@ -359,11 +467,11 @@
Code
ctl_colonnade(x, width = 40)
Output
a b$c $d $e
<int> <int> <int> <df[,0]>
1 1 4 7
2 2 5 8
3 3 6 9
a b$c $d $e c
<int> <int> <int> <df[,0]> <int>
1 1 4 7 10
2 2 5 8 11
3 3 6 9 12

# matrix columns (unnamed)

Expand All @@ -390,11 +498,11 @@
# matrix columns (empty)

Code
ctl_colonnade(x, width = 30)
ctl_colonnade(list(a = 1:3, b = matrix(4:6, ncol = 1)[, 0], c = 4:6), width = 30)
Output
a b
<int> <int[,0]>
1 1
2 2
3 3
a b c
<int> <int[,0]> <int>
1 1 4
2 2 5
3 3 6

Loading

0 comments on commit a2cb096

Please sign in to comment.