Skip to content

Commit a9102de

Browse files
committed
Use cli in format method for googlesheets4_spreadsheet
Closes #230
1 parent 6bcbb26 commit a9102de

File tree

3 files changed

+99
-60
lines changed

3 files changed

+99
-60
lines changed

R/schema_Spreadsheet.R

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -63,56 +63,71 @@ new_googlesheets4_spreadsheet <- function(x = list()) {
6363

6464
#' @export
6565
format.googlesheets4_spreadsheet <- function(x, ...) {
66-
meta <- tibble::tribble(
67-
~col1, ~col2,
68-
"Spreadsheet name", x$name,
69-
"ID", as.character(x$spreadsheet_id),
70-
"Locale", x$locale,
71-
"Time zone", x$time_zone,
72-
"# of sheets", if (rlang::has_name(x, "sheets")) {
73-
as.character(nrow(x$sheets))
74-
} else {
75-
"<unknown>"
76-
}
66+
cli::cli_div(theme = gs4_theme())
67+
meta <- list(
68+
`Spreadsheet name` = cli::format_inline("{.s_sheet {x$name}}"),
69+
ID = as.character(x$spreadsheet_id),
70+
Locale = x$locale,
71+
`Time zone` = x$time_zone,
72+
`# of sheets` = if (rlang::has_name(x, "sheets")) {
73+
as.character(nrow(x$sheets))
74+
} else {
75+
"<unknown>"
76+
}
7777
)
7878
if (!is.null(x$named_ranges)) {
79-
meta <- tibble::add_row(
80-
meta,
81-
col1 = "# of named ranges", col2 = as.character(nrow(x$named_ranges))
82-
)
79+
meta <- c(meta, `# of named ranges` = as.character(nrow(x$named_ranges)))
8380
}
8481
if (!is.null(x$protected_ranges)) {
85-
meta <- tibble::add_row(
86-
meta,
87-
col1 = "# of protected ranges", col2 = as.character(nrow(x$protected_ranges))
88-
)
82+
meta <- c(meta, `# of protected ranges` = as.character(nrow(x$protected_ranges)))
8983
}
90-
meta <- glue_data(meta, "{fr(col1)}: {col2}")
84+
out <- c(
85+
cli::cli_format_method(
86+
cli::cli_h1("<googlesheets4_spreadsheet>")
87+
),
88+
glue("{fr(names(meta))}: {fl(meta)}")
89+
)
9190

9291
if (!is.null(x$sheets)) {
93-
col1 <- fr(c("(Sheet name)", x$sheets$name))
92+
col1 <- fr(c(
93+
"(Sheet name)",
94+
sapply(
95+
gargle::gargle_map_cli(x$sheets$name, template = "{.w_sheet <<x>>}"),
96+
cli::format_inline
97+
)
98+
))
9499
col2 <- c(
95100
"(Nominal extent in rows x columns)",
96101
glue_data(x$sheets, "{grid_rows} x {grid_columns}")
97102
)
98-
meta <- c(
99-
meta,
100-
"",
103+
out <- c(
104+
out,
105+
cli::cli_format_method(
106+
cli::cli_h1("<sheets>")
107+
),
101108
glue_data(list(col1 = col1, col2 = col2), "{col1}: {col2}")
102109
)
103110
}
104111

105112
if (!is.null(x$named_ranges)) {
106-
col1 <- fr(c("(Named range)", x$named_ranges$name))
113+
col1 <- fr(c(
114+
"(Named range)",
115+
sapply(
116+
gargle::gargle_map_cli(x$named_ranges$name, template = "{.range <<x>>}"),
117+
cli::format_inline
118+
)
119+
))
107120
col2 <- fl(c("(A1 range)", x$named_ranges$A1_range))
108-
meta <- c(
109-
meta,
110-
"",
121+
out <- c(
122+
out,
123+
cli::cli_format_method(
124+
cli::cli_h1("<named ranges>")
125+
),
111126
glue_data(list(col1 = col1, col2 = col2), "{col1}: {col2}")
112127
)
113128
}
114129

115-
meta
130+
out
116131
}
117132

118133
#' @export

R/utils-ui.R

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,21 @@ message <- function(...) {
5959
.internal = TRUE)
6060
}
6161

62-
fr <- function(x) format(x, justify = "right")
63-
fl <- function(x) format(x, justify = "left")
62+
fr <- function(x) {
63+
cli::ansi_align(
64+
as.character(x),
65+
align = "right",
66+
width = max(cli::ansi_nchar(x))
67+
)
68+
}
69+
70+
fl <- function(x) {
71+
cli::ansi_align(
72+
as.character(x),
73+
align = "left",
74+
width = max(cli::ansi_nchar(x))
75+
)
76+
}
6477

6578
gs4_quiet <- function() {
6679
getOption("googlesheets4_quiet", default = NA)

tests/testthat/_snaps/sheets_id-class.md

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -42,33 +42,39 @@
4242
Code
4343
print(gs4_example("gapminder"))
4444
Output
45-
Spreadsheet name: gapminder
45+
46+
-- <googlesheets4_spreadsheet> -------------------------------------------------
47+
Spreadsheet name: "gapminder"
4648
ID: 1U6Cf_qEOhiR9AZqTqS3mbMF3zt2db48ZP5v3rkrAEJY
47-
Locale: en_US
48-
Time zone: America/Los_Angeles
49-
# of sheets: 5
50-
# of named ranges: 1
49+
Locale: en_US
50+
Time zone: America/Los_Angeles
51+
# of sheets: 5
52+
# of named ranges: 1
5153
54+
-- <sheets> --------------------------------------------------------------------
5255
(Sheet name): (Nominal extent in rows x columns)
53-
Africa: 625 x 6
54-
Americas: 301 x 6
55-
Asia: 397 x 6
56-
Europe: 361 x 6
57-
Oceania: 25 x 6
56+
'Africa': 625 x 6
57+
'Americas': 301 x 6
58+
'Asia': 397 x 6
59+
'Europe': 361 x 6
60+
'Oceania': 25 x 6
5861
62+
-- <named ranges> --------------------------------------------------------------
5963
(Named range): (A1 range)
60-
canada: 'Americas'!A38:F49
64+
'canada': 'Americas'!A38:F49
6165

6266
# sheets_id print method doesn't error for nonexistent ID
6367

6468
Code
6569
as_sheets_id("12345")
6670
Output
67-
Spreadsheet name: <unknown>
68-
ID: 12345
69-
Locale: <unknown>
70-
Time zone: <unknown>
71-
# of sheets: <unknown>
71+
72+
-- <googlesheets4_spreadsheet> -------------------------------------------------
73+
Spreadsheet name: "<unknown>"
74+
ID: 12345
75+
Locale: <unknown>
76+
Time zone: <unknown>
77+
# of sheets: <unknown>
7278
7379
Unable to get metadata for this Sheet. Error details:
7480
Client error: (404) NOT_FOUND
@@ -81,29 +87,34 @@
8187
Code
8288
print(gs4_example("mini-gap"))
8389
Output
84-
Spreadsheet name: mini-gap
90+
91+
-- <googlesheets4_spreadsheet> -------------------------------------------------
92+
Spreadsheet name: "mini-gap"
8593
ID: 1k94ZVVl6sdj0AXfK9MQOuQ4rOhd1PULqpAu2_kr9MAU
86-
Locale: en_US
87-
Time zone: America/Los_Angeles
88-
# of sheets: 5
94+
Locale: en_US
95+
Time zone: America/Los_Angeles
96+
# of sheets: 5
8997
98+
-- <sheets> --------------------------------------------------------------------
9099
(Sheet name): (Nominal extent in rows x columns)
91-
Africa: 6 x 6
92-
Americas: 6 x 6
93-
Asia: 6 x 6
94-
Europe: 6 x 6
95-
Oceania: 6 x 6
100+
'Africa': 6 x 6
101+
'Americas': 6 x 6
102+
'Asia': 6 x 6
103+
'Europe': 6 x 6
104+
'Oceania': 6 x 6
96105

97106
# sheets_id print does not error for lack of cred
98107

99108
Code
100109
print(gs4_example("mini-gap"))
101110
Output
102-
Spreadsheet name: <unknown>
111+
112+
-- <googlesheets4_spreadsheet> -------------------------------------------------
113+
Spreadsheet name: "<unknown>"
103114
ID: 1k94ZVVl6sdj0AXfK9MQOuQ4rOhd1PULqpAu2_kr9MAU
104-
Locale: <unknown>
105-
Time zone: <unknown>
106-
# of sheets: <unknown>
115+
Locale: <unknown>
116+
Time zone: <unknown>
117+
# of sheets: <unknown>
107118
108119
Unable to get metadata for this Sheet. Error details:
109120
Can't get Google credentials.

0 commit comments

Comments
 (0)