Skip to content

Commit ac7bd49

Browse files
committed
Auto-detect html in str_view()
1 parent df00095 commit ac7bd49

File tree

4 files changed

+41
-20
lines changed

4 files changed

+41
-20
lines changed

R/view.R

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
#' If `FALSE`, shows only the strings that don't match the pattern.
1010
#' Otherwise (the default, `NA`) displays both matches and non-matches.
1111
#' @param html Use HTML output? If `TRUE` will create an HTML widget;
12-
#' if `FALSE` will style using ANSI escapes.
12+
#' if `FALSE` will style using ANSI escapes. The default will prefers
13+
#' ANSI escapes if available in the current terminal; you can override by
14+
#' setting `option(stringr.html = TRUE)`.
1315
#' @export
1416
#' @examples
1517
#' # Show special characters
@@ -22,7 +24,8 @@
2224
#'
2325
#' # Show all matches
2426
#' str_view_all(c("abc", "def", "fgh"), "d|e")
25-
str_view <- function(string, pattern = NULL, match = NA, html = getOption("stringr.html", TRUE)) {
27+
str_view <- function(string, pattern = NULL, match = NA, html = NULL) {
28+
html <- str_view_use_html(html)
2629
out <- str_view_filter(string, pattern, match)
2730
if (!is.null(pattern)) {
2831
out <- str_replace(out, pattern, str_view_highlighter(html))
@@ -32,7 +35,8 @@ str_view <- function(string, pattern = NULL, match = NA, html = getOption("strin
3235

3336
#' @rdname str_view
3437
#' @export
35-
str_view_all <- function(string, pattern = NULL, match = NA, html = getOption("stringr.html", TRUE)) {
38+
str_view_all <- function(string, pattern = NULL, match = NA, html = NULL) {
39+
html <- str_view_use_html(html)
3640
out <- str_view_filter(string, pattern, match)
3741
if (!is.null(pattern)) {
3842
out <- str_replace_all(out, pattern, str_view_highlighter(html))
@@ -56,6 +60,10 @@ str_view_filter <- function(x, pattern, match) {
5660

5761
# Helpers -----------------------------------------------------------------
5862

63+
str_view_use_html <- function(html) {
64+
html %||% getOption("stringr.html") %||% (cli::num_ansi_colors() < 8L)
65+
}
66+
5967
str_view_highlighter <- function(html = TRUE) {
6068
if (html) {
6169
function(x) paste0("<span class='match'>", x, "</span>")

man/str_view.Rd

Lines changed: 5 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/view.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
11
# view works
22

33
Code
4-
str_view(x, "[aeiou]")$x$html
4+
str_view(x, "[aeiou]", html = TRUE)$x$html
55
Output
66
<ul>
77
<li><span class='match'>a</span>bc</li>
88
<li>d<span class='match'>e</span>f</li>
99
<li>fgh</li>
1010
</ul>
1111
Code
12-
str_view_all(x, "d|e")$x$html
12+
str_view_all(x, "d|e", html = TRUE)$x$html
1313
Output
1414
<ul>
1515
<li>abc</li>
1616
<li><span class='match'>d</span><span class='match'>e</span>f</li>
1717
<li>fgh</li>
1818
</ul>
1919

20+
---
21+
22+
Code
23+
str_view(x, "[aeiou]", html = FALSE)
24+
Output
25+
[a]bc
26+
d[e]f
27+
fgh
28+
Code
29+
str_view_all(x, "d|e", html = FALSE)
30+
Output
31+
abc
32+
[d][e]f
33+
fgh
34+

tests/testthat/test-view.R

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22
test_that("view works", {
33
x <- c("abc", "def", "fgh")
44
expect_snapshot({
5-
str_view(x, "[aeiou]")$x$html
6-
str_view_all(x, "d|e")$x$html
5+
str_view(x, "[aeiou]", html = TRUE)$x$html
6+
str_view_all(x, "d|e", html = TRUE)$x$html
77
})
8+
9+
expect_snapshot({
10+
str_view(x, "[aeiou]", html = FALSE)
11+
str_view_all(x, "d|e", html = FALSE)
12+
})
13+
814
})
915

1016
test_that("match argument controls what is shown", {

0 commit comments

Comments
 (0)