Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions R/interpolate.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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")
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/_snaps/interpolate.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-interpolate.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})

Expand Down