Skip to content

Commit

Permalink
Prepare for R marking useBytes = TRUE results as 'bytes'
Browse files Browse the repository at this point in the history
More info: r-lib/cli#495
  • Loading branch information
gaborcsardi committed Aug 24, 2022
1 parent 0b5a8a8 commit 2caa09f
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 4 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Collate:
'utils.r'
'crayon-package.r'
'disposable.r'
'enc-utils.R'
'has_ansi.r'
'has_color.r'
'link.R'
Expand Down
2 changes: 1 addition & 1 deletion R/aab-num-ansi-colors.R
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ emacs_version <- function() {
ver <- Sys.getenv("INSIDE_EMACS")
if (ver == "") return(NA_integer_)

ver <- gsub("'", "", ver, useBytes = TRUE)
ver <- gsub("'", "", ver)
ver <- strsplit(ver, ",", fixed = TRUE)[[1]]
ver <- strsplit(ver, ".", fixed = TRUE)[[1]]
as.numeric(ver)
Expand Down
25 changes: 25 additions & 0 deletions R/enc-utils.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

# keep encoding, even if useBytes = TRUE

sub_ <- function(pattern, replacement, x, ...) {
enc <- Encoding(x)
ret <- sub(pattern, replacement, x, ...)
if (length(ret)) Encoding(ret) <- enc
ret
}

gsub_ <- function(pattern, replacement, x, ...) {
enc <- Encoding(x)
ret <- gsub(pattern, replacement, x, ...)
if (length(ret)) Encoding(ret) <- enc
ret
}

strsplit_ <- function(x, ...) {
enc <- Encoding(x)
ret <- strsplit(x, ...)
for (i in seq_along(ret)) {
Encoding(ret[[i]]) <- enc[i]
}
ret
}
2 changes: 1 addition & 1 deletion R/has_ansi.r
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ has_style <- function(string) {
#' strip_style(red("foobar")) == "foobar"

strip_style <- function(string) {
gsub(ansi_regex, "", string, perl = TRUE, useBytes = TRUE)
gsub_(ansi_regex, "", string, perl = TRUE, useBytes = TRUE)
}
2 changes: 1 addition & 1 deletion R/machinery.r
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ crayon_template <- function(...) {
for (st in rev(my_styles)) {
if (!is.null(st$palette)) st <- get_palette_color(st, nc)
text <- st$open %+%
gsub(st$close, st$open, text, fixed = TRUE, useBytes = TRUE) %+%
gsub_(st$close, st$open, text, fixed = TRUE, useBytes = TRUE) %+%
st$close
}
}
Expand Down
2 changes: 1 addition & 1 deletion R/utils.r
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ myseq <- function(from, to, by = 1) {

emacs_version <- function() {
ver <- Sys.getenv("INSIDE_EMACS")
ver <- gsub("[^0-9\\.]+", "", ver, useBytes = TRUE)
ver <- gsub("[^0-9\\.]+", "", ver)
if (ver == "") return(NA_integer_)
ver <- strsplit(ver, ".", fixed = TRUE)[[1]]
as.numeric(ver)
Expand Down

0 comments on commit 2caa09f

Please sign in to comment.