1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
R version 4.0.3 (2020-10-10) -- "Bunny-Wunnies Freak Out"
Copyright (C) 2020 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin17.0 (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> NAME <- "text"
> source(file.path('_helper', 'init.R'))
>
> # - simple wrap
>
> txt1 <- c(
+ "humpty dumpty sat on a wall and had a big fall",
+ "humpty sat on a wall and dumped a big fall"
+ )
> res1 <- diffobj:::wrap(txt1, 10, TRUE, sgr.supported=TRUE)
>
> identical(
+ gsub(" *$", "", vapply(res1, paste0, character(1L), collapse="")), txt1
+ )
[1] TRUE
> all.equal(lapply(res1, nchar), list(rep(10L, 5L), rep(10L, 5L)))
[1] TRUE
>
> txt2 <- "hello world!"
> identical(
+ unlist(diffobj:::wrap(txt2, nchar(txt2), TRUE, sgr.supported=TRUE)),
+ txt2
+ )
[1] TRUE
> identical(
+ paste0(
+ unlist(diffobj:::wrap(txt2, nchar(txt2) / 2, TRUE, sgr.supported=TRUE)),
+ collapse=""
+ ),
+ txt2
+ )
[1] TRUE
>
> # - wrap with escape sequences
>
> txt3 <- c(
+ paste0(
+ "humpty dumpty ", crayon::style("sat on a wall", "red"),
+ " and had a big fall",
+ crayon::style(
+ crayon::style(
+ "humpty sat on a wall and dumped a big fall",
+ "green"
+ ),
+ "bgRed"
+ ), "woohoo"
+ ),
+ paste0(
+ crayon::style("hello ", "inverse"), "beautiful ",
+ crayon::style("world", "blue")
+ )
+ )
> res3 <- diffobj:::wrap(txt3, 10, TRUE, sgr.supported=TRUE)
>
> identical(
+ crayon::strip_style(
+ gsub(" *$", "", vapply(res3, paste0, character(1L), collapse=""))
+ ),
+ crayon::strip_style(txt3)
+ )
[1] TRUE
> all.equal(
+ lapply(res3, crayon::col_nchar),
+ list(rep(10L, 10L), rep(10L, 3L))
+ )
[1] TRUE
>
> # - strip hz whitespace
>
> options(crayon.enabled=FALSE)
> all.equal(
+ diffobj:::strip_hz_control("a\tb", stops=4L, sgr.supported=TRUE), "a b")
[1] TRUE
> all.equal(
+ diffobj:::strip_hz_control("ab\t", stops=4L, sgr.supported=TRUE), "ab ")
[1] TRUE
> all.equal(
+ diffobj:::strip_hz_control("a\tb\t", stops=4L, sgr.supported=TRUE), "a b ")
[1] TRUE
> all.equal(
+ diffobj:::strip_hz_control("\ta\tb\t", stops=4L, sgr.supported=TRUE),
+ " a b "
+ )
[1] TRUE
> all.equal(
+ diffobj:::strip_hz_control("\ta\tb\t", stops=c(2L, 4L), sgr.supported=TRUE),
+ " a b "
+ )
[1] TRUE
> all.equal(
+ diffobj:::strip_hz_control(
+ c("ab\t", "\ta\tb\t"), sgr.supported=TRUE, stops=4L
+ ),
+ c("ab ", " a b ")
+ )
[1] TRUE
> # recall that nchar("\033") == 1
> all.equal(
+ diffobj:::strip_hz_control(
+ "\033[31ma\t\033[39mhello\tb", stops=10L, sgr.supported=FALSE
+ ),
+ "\033[31ma \033[39mhello b"
+ )
[1] TRUE
> all.equal(
+ diffobj:::strip_hz_control(
+ "\033[31ma\t\033[39mhello\tb", stops=10L, sgr.supported=TRUE
+ ),
+ "\033[31ma\033[39m \033[31m\033[39mhello \033[31m\033[39mb"
+ )
[1] TRUE
> # carriage returns
>
> all.equal(
+ diffobj:::strip_hz_control("hellothere\rHELLO", sgr.supported=TRUE),
+ "HELLOthere"
+ )
[1] TRUE
> all.equal(
+ diffobj:::strip_hz_control(
+ c("hellothere\rHELLO", "000\r12345678\rabcdef\rABC"), sgr.supported=TRUE
+ ),
+ c("HELLOthere", "ABCdef78")
+ )
[1] TRUE
> all.equal(
+ diffobj:::strip_hz_control("hellothere\r", sgr.supported=TRUE),
+ "hellothere"
+ )
[1] TRUE
> all.equal(
+ diffobj:::strip_hz_control(character(), sgr.supported=TRUE), character()
+ )
[1] TRUE
> # newlines
>
> all.equal(
+ diffobj:::strip_hz_control(c("a", "", "\n", "a\nb"), sgr.supported=TRUE),
+ c("a", "", "", "a", "b")
+ )
[1] TRUE
> # with colors
>
> options(crayon.enabled=TRUE)
>
> all.equal(
+ crayon::strip_style(
+ diffobj:::strip_hz_control(
+ "\033[31ma\t\033[39mhello\tb", stops=10L, sgr.supported=TRUE)
+ ),
+ "a hello b"
+ )
[1] TRUE
> test.chr <- paste0(
+ crayon::red(crayon::`%+%`("000", crayon::bgBlue("\r12345678"))),
+ "\rabcdef", crayon::green("\rABC")
+ )
> # visually inspect these
>
> # cat("\n")
> # cat(test.chr, sep="\n")
> res <- diffobj:::strip_hz_control(test.chr, sgr.supported=TRUE)
> # cat(res, sep="\n")
> all.equal(crayon::strip_style(res), "ABCdef78")
[1] TRUE
>
> # Mix tabs and carriage returns, visual inspection assumes terminal tab
> # stops at 8L; note output not exactly the same since it seems tabs don't
> # ovewrite prior screen state whereas spaces do
>
> test.chr.2 <- paste0(
+ crayon::red(crayon::`%+%`("000", crayon::bgBlue("\r123\t456\t78"))),
+ "\rab\tcd f", crayon::green("\rABC")
+ )
> # cat("\n")
> # cat(test.chr.2, sep="\n")
> res.2 <- diffobj:::strip_hz_control(test.chr.2, stops=8L, sgr.supported=TRUE)
> # cat(res.2, sep="\n")
>
> all.equal(crayon::strip_style(res.2), "ABC cd f 78")
[1] TRUE
>
> # multi line
>
> test.chr.3 <- c(test.chr, test.chr.2)
> # cat("\n")
> res.3 <- diffobj:::strip_hz_control(test.chr.3, sgr.supported=TRUE)
> # cat(res.3, sep="\n")
> # cat(test.chr.3, sep="\n")
>
> all.equal(crayon::strip_style(res.3), c("ABCdef78", "ABC cd f 78"))
[1] TRUE
>
>
> proc.time()
user system elapsed
1.047 0.106 1.146
|