Skip to content

Commit

Permalink
Use cli in format method for googlesheets4_spreadsheet
Browse files Browse the repository at this point in the history
Closes #230
  • Loading branch information
jennybc committed Jun 4, 2023
1 parent 6bcbb26 commit a9102de
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 60 deletions.
73 changes: 44 additions & 29 deletions R/schema_Spreadsheet.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,56 +63,71 @@ new_googlesheets4_spreadsheet <- function(x = list()) {

#' @export
format.googlesheets4_spreadsheet <- function(x, ...) {
meta <- tibble::tribble(
~col1, ~col2,
"Spreadsheet name", x$name,
"ID", as.character(x$spreadsheet_id),
"Locale", x$locale,
"Time zone", x$time_zone,
"# of sheets", if (rlang::has_name(x, "sheets")) {
as.character(nrow(x$sheets))
} else {
"<unknown>"
}
cli::cli_div(theme = gs4_theme())
meta <- list(
`Spreadsheet name` = cli::format_inline("{.s_sheet {x$name}}"),
ID = as.character(x$spreadsheet_id),
Locale = x$locale,
`Time zone` = x$time_zone,
`# of sheets` = if (rlang::has_name(x, "sheets")) {
as.character(nrow(x$sheets))
} else {
"<unknown>"
}
)
if (!is.null(x$named_ranges)) {
meta <- tibble::add_row(
meta,
col1 = "# of named ranges", col2 = as.character(nrow(x$named_ranges))
)
meta <- c(meta, `# of named ranges` = as.character(nrow(x$named_ranges)))
}
if (!is.null(x$protected_ranges)) {
meta <- tibble::add_row(
meta,
col1 = "# of protected ranges", col2 = as.character(nrow(x$protected_ranges))
)
meta <- c(meta, `# of protected ranges` = as.character(nrow(x$protected_ranges)))
}
meta <- glue_data(meta, "{fr(col1)}: {col2}")
out <- c(
cli::cli_format_method(
cli::cli_h1("<googlesheets4_spreadsheet>")
),
glue("{fr(names(meta))}: {fl(meta)}")
)

if (!is.null(x$sheets)) {
col1 <- fr(c("(Sheet name)", x$sheets$name))
col1 <- fr(c(
"(Sheet name)",
sapply(
gargle::gargle_map_cli(x$sheets$name, template = "{.w_sheet <<x>>}"),
cli::format_inline
)
))
col2 <- c(
"(Nominal extent in rows x columns)",
glue_data(x$sheets, "{grid_rows} x {grid_columns}")
)
meta <- c(
meta,
"",
out <- c(
out,
cli::cli_format_method(
cli::cli_h1("<sheets>")
),
glue_data(list(col1 = col1, col2 = col2), "{col1}: {col2}")
)
}

if (!is.null(x$named_ranges)) {
col1 <- fr(c("(Named range)", x$named_ranges$name))
col1 <- fr(c(
"(Named range)",
sapply(
gargle::gargle_map_cli(x$named_ranges$name, template = "{.range <<x>>}"),
cli::format_inline
)
))
col2 <- fl(c("(A1 range)", x$named_ranges$A1_range))
meta <- c(
meta,
"",
out <- c(
out,
cli::cli_format_method(
cli::cli_h1("<named ranges>")
),
glue_data(list(col1 = col1, col2 = col2), "{col1}: {col2}")
)
}

meta
out
}

#' @export
Expand Down
17 changes: 15 additions & 2 deletions R/utils-ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,21 @@ message <- function(...) {
.internal = TRUE)
}

fr <- function(x) format(x, justify = "right")
fl <- function(x) format(x, justify = "left")
fr <- function(x) {
cli::ansi_align(
as.character(x),
align = "right",
width = max(cli::ansi_nchar(x))
)
}

fl <- function(x) {
cli::ansi_align(
as.character(x),
align = "left",
width = max(cli::ansi_nchar(x))
)
}

gs4_quiet <- function() {
getOption("googlesheets4_quiet", default = NA)
Expand Down
69 changes: 40 additions & 29 deletions tests/testthat/_snaps/sheets_id-class.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,33 +42,39 @@
Code
print(gs4_example("gapminder"))
Output
Spreadsheet name: gapminder
-- <googlesheets4_spreadsheet> -------------------------------------------------
Spreadsheet name: "gapminder"
ID: 1U6Cf_qEOhiR9AZqTqS3mbMF3zt2db48ZP5v3rkrAEJY
Locale: en_US
Time zone: America/Los_Angeles
# of sheets: 5
# of named ranges: 1
Locale: en_US
Time zone: America/Los_Angeles
# of sheets: 5
# of named ranges: 1
-- <sheets> --------------------------------------------------------------------
(Sheet name): (Nominal extent in rows x columns)
Africa: 625 x 6
Americas: 301 x 6
Asia: 397 x 6
Europe: 361 x 6
Oceania: 25 x 6
'Africa': 625 x 6
'Americas': 301 x 6
'Asia': 397 x 6
'Europe': 361 x 6
'Oceania': 25 x 6
-- <named ranges> --------------------------------------------------------------
(Named range): (A1 range)
canada: 'Americas'!A38:F49
'canada': 'Americas'!A38:F49

# sheets_id print method doesn't error for nonexistent ID

Code
as_sheets_id("12345")
Output
Spreadsheet name: <unknown>
ID: 12345
Locale: <unknown>
Time zone: <unknown>
# of sheets: <unknown>
-- <googlesheets4_spreadsheet> -------------------------------------------------
Spreadsheet name: "<unknown>"
ID: 12345
Locale: <unknown>
Time zone: <unknown>
# of sheets: <unknown>
Unable to get metadata for this Sheet. Error details:
Client error: (404) NOT_FOUND
Expand All @@ -81,29 +87,34 @@
Code
print(gs4_example("mini-gap"))
Output
Spreadsheet name: mini-gap
-- <googlesheets4_spreadsheet> -------------------------------------------------
Spreadsheet name: "mini-gap"
ID: 1k94ZVVl6sdj0AXfK9MQOuQ4rOhd1PULqpAu2_kr9MAU
Locale: en_US
Time zone: America/Los_Angeles
# of sheets: 5
Locale: en_US
Time zone: America/Los_Angeles
# of sheets: 5
-- <sheets> --------------------------------------------------------------------
(Sheet name): (Nominal extent in rows x columns)
Africa: 6 x 6
Americas: 6 x 6
Asia: 6 x 6
Europe: 6 x 6
Oceania: 6 x 6
'Africa': 6 x 6
'Americas': 6 x 6
'Asia': 6 x 6
'Europe': 6 x 6
'Oceania': 6 x 6

# sheets_id print does not error for lack of cred

Code
print(gs4_example("mini-gap"))
Output
Spreadsheet name: <unknown>
-- <googlesheets4_spreadsheet> -------------------------------------------------
Spreadsheet name: "<unknown>"
ID: 1k94ZVVl6sdj0AXfK9MQOuQ4rOhd1PULqpAu2_kr9MAU
Locale: <unknown>
Time zone: <unknown>
# of sheets: <unknown>
Locale: <unknown>
Time zone: <unknown>
# of sheets: <unknown>
Unable to get metadata for this Sheet. Error details:
Can't get Google credentials.
Expand Down

0 comments on commit a9102de

Please sign in to comment.