Skip to content

Commit

Permalink
Return uncolored for malformed LS_COLORS
Browse files Browse the repository at this point in the history
We could try to patch up the empty case as shown in the issue, but I
think it is better to just bail and give up coloring if something seems
wrong. I decided not to issue a warning for this case, if it comes up
again we can think about adding one.

Fixes #135
  • Loading branch information
jimhester committed Sep 17, 2018
1 parent 695ab8c commit edc88f2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Development

* `colourise_fs_path()` now returns paths uncolored if the colors argument /
`LS_COLORS` is malformed (#135).

# fs 1.2.6

* This is a small bugfix only release.
Expand Down
8 changes: 8 additions & 0 deletions R/fs_path.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ colourise_fs_path <- function(x, ..., colors = Sys.getenv("LS_COLORS", gnu_ls_de

vals <- strsplit(colors, ":")[[1]]
nms <- strsplit(vals, "=")

if (!(
length(vals) == length(nms) &&
all(lengths(nms) == 2)
)) {
return(x)
}

map <- setNames(
vapply(nms, `[[`, character(1), 2),
vapply(nms, `[[`, character(1), 1))
Expand Down
4 changes: 4 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,7 @@ assert <- function(msg, ..., class = "invalid_argument") {
fs_error <- function(msg, class = "invalid_argument") {
structure(class = c(class, "fs_error", "error", "condition"), list(message = msg))
}

lengths <- function(x) {
vapply(x, length, integer(1))
}
7 changes: 7 additions & 0 deletions tests/testthat/test-fs_path.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ describe("colourise_fs_path", {
expect_equal(colourise_fs_path("file.Rmd"), "\033[32mfile.Rmd\033[0m")
})
})

it ("returns uncolored result if LS_COLORS is malformed", {
# This has an empty leading :
withr::local_envvar(c("LS_COLORS" = ":di=1;38;5;39:ex=1;38;5;34:ln=1;38;5;45:*.py=1;38;5;220:*.pdf=1;38;5;202:*.tex=1;38;5;196"))

expect_equal(colourise_fs_path("foo"), "foo")
})
})

describe("multicol", {
Expand Down

0 comments on commit edc88f2

Please sign in to comment.