From 466d18e97d8a34ede3b5ec4dc48aaaa6c13f8046 Mon Sep 17 00:00:00 2001 From: William Melody Date: Tue, 2 Apr 2024 23:05:43 -0700 Subject: [PATCH] Avoid `grep --color` option for improved OpenBSD compatibility. refs gh-313 --- nb | 62 +++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/nb b/nb index 59740363..eecfb201 100755 --- a/nb +++ b/nb @@ -2358,6 +2358,18 @@ _get_visible_length() { printf "%s\\n" "${_calculated_length:-0}" } +# _grep_supports_color() +# +# Usage: +# _grep_supports_color +# +# Exit / Error / Return Status: +# 0 (success, true) If grep supports the --color option. +# 1 (error, false) if not. +_grep_supports_color() { + printf "\\n" | grep --color=never "" >/dev/null 2>&1 +} + # _highlight_syntax_if_available() # # Usage: @@ -2595,12 +2607,12 @@ _list_files() { case "${_type}" in bookmark|bookmarks) - grep --color=never \ + grep \ -e '\.bookmark\.' \ -e '\-bookmark\.' ;; book|books) - grep --color=never -e 'epub$' + grep -e 'epub$' ;; note|notes) for __extension in "${_TEXT_FILE_EXTENSIONS[@]}" @@ -2608,9 +2620,9 @@ _list_files() { _grep_patterns+=("-e" "${__extension}$") done - grep --color=never "${_grep_patterns[@]}" \ - | grep --color=never -v \ - -e '\.bookmark\.' \ + grep "${_grep_patterns[@]}" \ + | grep -v \ + -e '\.bookmark\.' \ -e '\-bookmark\.' ;; folder|folders|directory|directories) @@ -2629,7 +2641,7 @@ _list_files() { _grep_patterns+=("-e" "${__extension}$" "-e" "${__extension}.enc$") done - grep --color=never "${_grep_patterns[@]}" + grep "${_grep_patterns[@]}" ;; audio|music) for __extension in "${_AUDIO_FILE_EXTENSIONS[@]}" @@ -2637,7 +2649,7 @@ _list_files() { _grep_patterns+=("-e" "${__extension}$" "-e" "${__extension}.enc$") done - grep --color=never "${_grep_patterns[@]}" + grep "${_grep_patterns[@]}" ;; document|documents|doc|docs) for __extension in "${_DOCUMENT_FILE_EXTENSIONS[@]}" @@ -2645,7 +2657,7 @@ _list_files() { _grep_patterns+=("-e" "\.${__extension}$" "-e" "\.${__extension}\.enc$") done - grep --color=never "${_grep_patterns[@]}" + grep "${_grep_patterns[@]}" ;; encrypted) for __extension in "${_ENCRYPTED_FILE_EXTENSIONS[@]}" @@ -2653,7 +2665,7 @@ _list_files() { _grep_patterns+=("-e" "\.${__extension}$") done - grep --color=never "${_grep_patterns[@]}" + grep "${_grep_patterns[@]}" ;; image|images|picture|pictures) for __extension in "${_IMAGE_FILE_EXTENSIONS[@]}" @@ -2661,7 +2673,7 @@ _list_files() { _grep_patterns+=("-e" "\.${__extension}$" "-e" "\.${__extension}\.enc$") done - grep --color=never "${_grep_patterns[@]}" + grep "${_grep_patterns[@]}" ;; text|txt) for __extension in "${_TEXT_FILE_EXTENSIONS[@]}" @@ -2669,7 +2681,7 @@ _list_files() { _grep_patterns+=("-e" "\.${__extension}$") done - grep --color=never "${_grep_patterns[@]}" + grep "${_grep_patterns[@]}" ;; todo|todos) for __extension in "${_TODO_FILE_EXTENSIONS[@]}" @@ -2677,7 +2689,7 @@ _list_files() { _grep_patterns+=("-e" "\.${__extension}$") done - grep --color=never "${_grep_patterns[@]}" + grep "${_grep_patterns[@]}" ;; video|videos) for __extension in "${_VIDEO_FILE_EXTENSIONS[@]}" @@ -2685,10 +2697,10 @@ _list_files() { _grep_patterns+=("-e" "\.${__extension}$" "-e" "\.${__extension}\.enc$") done - grep --color=never "${_grep_patterns[@]}" + grep "${_grep_patterns[@]}" ;; *) - grep --color=never -e "\.${_type}$" + grep -e "\.${_type}$" ;; esac else @@ -3384,7 +3396,7 @@ _string_is_url() { _url_match_patterns+=(-e "^${__url_match_pattern}$") done - printf "%s\\n" "${1:-}" | grep -E -q --color=never "${_url_match_patterns[@]:-}" + printf "%s\\n" "${1:-}" | grep -E -q "${_url_match_patterns[@]:-}" &>/dev/null } # _wrap() @@ -11946,7 +11958,6 @@ _resolve_links() { 2>/dev/null || : else grep \ - --color=never \ --extended-regexp \ -h \ --only-matching \ @@ -13590,7 +13601,7 @@ _bookmark() { _wayback_response="$( _download_from \ "https://archive.org/wayback/available?url=${_target_url}" \ - | grep --color=never 'closest' \ + | grep 'closest' \ | sed -E "s/.*\"closest\": {([^}]+)}.*/\1/g" \ | sed -E "s/.*\"url\": \"([^\"]+)\".*/\1/g" || printf "" )" @@ -16963,7 +16974,6 @@ _search() { fi local _grep_arguments=( - "--color=never" "--extended-regexp" "-I" "--ignore-case" @@ -17283,11 +17293,14 @@ _search() { || return 0 # Don't fail out within a single scope. elif _search_with "grep" "${_search_utility:-}" then - if ((_COLOR_ENABLED)) + if _grep_supports_color then - _search_args+=("--color=always") - else - _search_args+=("--color=never") + if ((_COLOR_ENABLED)) + then + _search_args+=("--color=always") + else + _search_args+=("--color=never") + fi fi set +f @@ -18599,7 +18612,7 @@ ${_title/\[$' '\]/${_TPUT_SETAF_8}[ ]${_TPUT_SGR0}}" local _target_url= _target_url="$( - grep -E --color=never \ + grep -E \ "${_url_match_patterns[@]:-}" \ "${_target_path}" \ | head -n 1 \ @@ -24944,7 +24957,6 @@ HEREDOC elif _command_name="$( printf "%s\\n" "${_normalized_input}" \ | grep \ - --color=never \ --only-matching \ -e '^code' \ -e '^emacs' \ @@ -26015,7 +26027,7 @@ _todo() { --visible-length "${_extra_length:-0}" { - grep --color=never \ + grep \ -e "- \[[[:space:]]*\] " \ -e "- \[x\] " \ "${__item_path:-}" || :