@@ -85,7 +85,7 @@ ctl_new_pillar.SE_print_abstraction <- function(controller, x, width, ..., title
8585
8686
8787# ' Format covariate header by distributing label across covariate columns
88- # '
88+ # '
8989# ' @importFrom rlang names2
9090# ' @importFrom pillar align
9191# ' @importFrom pillar get_extent
@@ -95,7 +95,7 @@ ctl_new_pillar.SE_print_abstraction <- function(controller, x, width, ..., title
9595# ' @importFrom tibble as_tibble
9696# ' @importFrom stringr str_replace_all
9797# ' @importFrom purrr map2_chr
98- # '
98+ # '
9999# ' @param separator_row The separator row with column widths
100100# ' @param printed_colnames The printed column names
101101# ' @param covariate_names The names of covariate columns
@@ -120,10 +120,10 @@ format_covariate_header <- function(separator_row, printed_colnames, covariate_n
120120 label ,
121121 paste(rep(" -" , right_pad ), collapse = " " )
122122 )
123-
123+
124124 # Add '|' at the beginning and end
125125 merged_label <- paste0(" |" , merged_label , " |" )
126-
126+
127127 # Guarantee the merged_label is exactly total_covariate_width + 2
128128 merged_label <- substr(merged_label , 1 , total_covariate_width + 2 )
129129
@@ -133,64 +133,84 @@ format_covariate_header <- function(separator_row, printed_colnames, covariate_n
133133
134134 # remove the other covariate columns
135135 header_row [covariate_indices [- 1 ]] <- " "
136-
136+
137137 # Add row ID spacing at the beginning
138138 header_row <- c(paste(rep(" " , number_of_total_rows | > nchar() - 4 ), collapse = " " ), header_row )
139139
140140 # Step 2: Collapse everything with space
141141 paste(header_row , collapse = " " )
142142
143-
143+
144144}
145145
146+ # ' Custom header for SE_print_abstraction tibbles
147+ # '
148+ # ' Draws a banner aligned to the first rendered body line, and prints a
149+ # ' one-line summary with feature/sample/assay counts.
150+ # '
151+ # ' @importFrom pillar tbl_format_header align style_subtle
152+ # ' @importFrom cli col_br_blue col_br_black
153+ # ' @importFrom stringr str_locate_all
154+ # ' @importFrom rlang names2
155+ # ' @importFrom magrittr %>%
146156# ' @export
147157tbl_format_header.SE_print_abstraction <- function (x , setup , ... ) {
158+
148159 number_of_features <- x | > attr(" number_of_features" )
149160 number_of_samples <- x | > attr(" number_of_samples" )
150161 named_header <- x | > attr(" named_header" )
151162 assay_names <- x | > attr(" assay_names" )
152163 separator_row <- x | > attr(" separator_row" )
153164 covariate_names <- x | > attr(" covariate_names" )
154-
165+
166+ x <<- x
167+
168+
155169 number_of_total_rows = (x | > attr(" number_of_features" )) * (x | > attr(" number_of_samples" ))
156-
157- printed_colnames <- x | > attr(" printed_colnames" )
158-
170+
171+ # printed_colnames <- x |> attr("printed_colnames")
172+ printed_colnames <- pillar :: tbl_format_setup(x )$ body [1 ] | > as.character()
173+
159174 # Find the positions of all '|' characters in the string
160175 pipe_positions <- stringr :: str_locate_all(printed_colnames , " \\ |" )[[1 ]][, " start" ]
161-
176+
162177 # Calculate character length to the start of the second '|'
163- chars_to_second_pipe <- pipe_positions [2 ] - 2
164-
178+ chars_to_second_pipe <- pipe_positions [2 ] - 3
179+
165180 # Check if there's a third pipe
166181 if (length(pipe_positions ) > = 3 ) {
167182 # Calculate character length between second and third pipe
168- chars_to_third_pipe <- pipe_positions [3 ] - pipe_positions [2 ] - 2
183+ chars_to_third_pipe <- pipe_positions [3 ] - pipe_positions [2 ] - 1
169184 } else {
170185 # Calculate character length to the end of the line
171- chars_to_third_pipe <- nchar(printed_colnames ) - pipe_positions [2 ]
186+ chars_to_third_pipe <- nchar(printed_colnames ) - pipe_positions [2 ] - 1
172187 }
173188
174189 label = " COVARIATES "
175190 label_length <- nchar(label )
176191
177192 # Center the label in the total covariate width, using only dashes and the label
178193 left_pad <- floor((chars_to_third_pipe - label_length ) / 2 )
179- right_pad <- chars_to_third_pipe - label_length - left_pad
180- merged_label <- paste0(
181- paste(rep(" -" , left_pad ), collapse = " " ),
182- label ,
183- paste(rep(" -" , right_pad ), collapse = " " )
184- )
185-
186- # Add '|' at the beginning and end
187- merged_label <- paste0(" |" , merged_label , " |" )
188-
189- # Pad with the spaces until chars to second pipe
190- merged_label <- c(paste(rep(" " , chars_to_second_pipe ), collapse = " " ), merged_label ) | >
191- paste0(collapse = " " )
194+ right_pad <- chars_to_third_pipe - label_length - left_pad - 1
195+
196+ if (left_pad > 0 & right_pad > 0 ){
197+ merged_label <- paste0(
198+ paste(rep(" -" , left_pad ), collapse = " " ),
199+ label ,
200+ paste(rep(" -" , right_pad ), collapse = " " )
201+ )
202+
203+ # Add '|' at the beginning and end
204+ merged_label <- paste0(" |-" , merged_label , " |" )
192205
193- covariate_header <- cli :: col_br_blue(merged_label )
206+ # Pad with the spaces until chars to second pipe
207+ merged_label <- c(paste(rep(" " , chars_to_second_pipe ), collapse = " " ), merged_label ) | >
208+ paste0(collapse = " " )
209+
210+ covariate_header <- cli :: col_br_blue(merged_label )
211+ }else {
212+ covariate_header = NULL
213+ }
194214
195215
196216 # Compose the main header as before
@@ -217,8 +237,10 @@ tbl_format_header.SE_print_abstraction <- function(x, setup, ...) {
217237 if (! is.null(covariate_header )) {
218238 header <- c(header , covariate_header )
219239 }
220-
240+
241+
221242 style_subtle(pillar___format_comment(header , width = setup $ width , strip.spaces = FALSE ))
243+
222244}
223245
224246# type_sum.sep <- function(x, ...) {
0 commit comments