diff --git a/R/interpolate.R b/R/interpolate.R index 4cce7575..3d03ab57 100644 --- a/R/interpolate.R +++ b/R/interpolate.R @@ -106,6 +106,9 @@ print.ellmer_prompt <- function( max_items = 20, max_lines = max_items * 10 ) { + check_number_whole(max_items, min = 1) + check_number_whole(max_lines, min = 1) + n <- length(x) n_extra <- length(x) - max_items if (n_extra > 0) { @@ -128,13 +131,18 @@ print.ellmer_prompt <- function( x <- gsub("\n", paste0("\n", exdent), x) lines <- strsplit(x, "\n") - ids <- rep(seq_along(x), length(lines)) + ids <- rep(seq_along(x), lengths(lines)) + first <- c(TRUE, ids[-length(ids)] != ids[-1]) lines <- unlist(lines) if (length(lines) > max_lines) { + if (first[max_lines + 1]) { + max_lines <- if (ids[max_lines] > 1) max_lines - 1 else max(max_lines, 1) + } + lines <- lines[seq_len(max_lines)] lines <- c(lines, paste0(exdent, "...")) - n_extra <- n - ids[max_lines - 1] + n_extra <- n - ids[max_lines] } cat(lines, sep = "\n") diff --git a/tests/testthat/_snaps/interpolate.md b/tests/testthat/_snaps/interpolate.md index f88d952f..f652b095 100644 --- a/tests/testthat/_snaps/interpolate.md +++ b/tests/testthat/_snaps/interpolate.md @@ -40,6 +40,13 @@ | y [2] | a | ... + Code + print(ellmer_prompt("a\nb\nc\nd\ne"), max_lines = 3) + Output + [1] | a + | b + | c + | ... # errors if the path does not exist diff --git a/tests/testthat/test-interpolate.R b/tests/testthat/test-interpolate.R index 33f9c6f4..0e700f3d 100644 --- a/tests/testthat/test-interpolate.R +++ b/tests/testthat/test-interpolate.R @@ -22,6 +22,7 @@ test_that("print method truncates many elements", { print(prompt, max_items = 1) print(prompt, max_lines = 2) print(prompt, max_lines = 3) + print(ellmer_prompt("a\nb\nc\nd\ne"), max_lines = 3) }) })