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
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+
5967str_view_highlighter <- function (html = TRUE ) {
6068 if (html ) {
6169 function (x ) paste0(" <span class='match'>" , x , " </span>" )
0 commit comments