Skip to content

Commit

Permalink
[todo msg] wrapi_text: count fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleam committed Jul 10, 2024
1 parent da3cb85 commit 2036d57
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
9 changes: 5 additions & 4 deletions R/util.R
Original file line number Diff line number Diff line change
Expand Up @@ -181,22 +181,23 @@ wrapi_text <- function(
if(is.null(start)) add else paste(start, add, sep = sep)
}

start <- NULL; shift <- 0
start <- NULL
shift <- 1
npieces <- length(pieces)
for (i in 2:npieces) {
pieces_sep <- pieces[(1 + shift):i]
pieces_sep <- pieces[shift:i]
add <- paste0(pieces_sep, collapse = wrap_chr)
s <- cat_start(start = start, add = add, sep = wrap_chr)
if(max_line_char(s) <= width){
next
}else{
pieces_start <- pieces[(1 + shift):(i-1)]
pieces_start <- pieces[shift:(i - 1)]
add <- paste(paste0(pieces_start, collapse = wrap_chr), pieces[i], sep = newline_sep)
if (i == npieces) {
s <- cat_start(start = start, add = add, sep = "")
}else{
start <- cat_start(start = start, add = add, sep = "")
shift <- shift + i
shift <- min(i + 1, npieces)
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions tests/testthat/test-util.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,25 @@ test_that("wrap_text(): past offenders", {
wrap_text("abcde", width = 2, wrap_sym = NULL, strict = TRUE),
"ab\ncd\ne"
)

expect_identical(
wrap_text("abcdefghij", width = 4, wrap_sym = NULL, strict = TRUE),
"abcd\nefgh\nij"
)

expect_identical(
wrap_text(
"test-foo.R\ntest-foo_bar.R",
width = 8, wrap_sym = NULL, strict = TRUE
),
"test-foo\n.R\ntest-foo\n_bar.R"
)

expect_identical(
wrap_text(
"test-foo.R\ntest-foo_bar.R",
width = 4, wrap_sym = NULL, strict = TRUE
),
"test\n-foo\n.R\ntest\n-foo\n_bar\n.R"
)
})

0 comments on commit 2036d57

Please sign in to comment.