Skip to content

Commit

Permalink
Avoid grep --color option for improved OpenBSD compatibility.
Browse files Browse the repository at this point in the history
refs gh-313
  • Loading branch information
xwmx committed Apr 3, 2024
1 parent 3027fa6 commit 466d18e
Showing 1 changed file with 37 additions and 25 deletions.
62 changes: 37 additions & 25 deletions nb
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -2595,22 +2607,22 @@ _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[@]}"
do
_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)
Expand All @@ -2629,66 +2641,66 @@ _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[@]}"
do
_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[@]}"
do
_grep_patterns+=("-e" "\.${__extension}$" "-e" "\.${__extension}\.enc$")
done

grep --color=never "${_grep_patterns[@]}"
grep "${_grep_patterns[@]}"
;;
encrypted)
for __extension in "${_ENCRYPTED_FILE_EXTENSIONS[@]}"
do
_grep_patterns+=("-e" "\.${__extension}$")
done

grep --color=never "${_grep_patterns[@]}"
grep "${_grep_patterns[@]}"
;;
image|images|picture|pictures)
for __extension in "${_IMAGE_FILE_EXTENSIONS[@]}"
do
_grep_patterns+=("-e" "\.${__extension}$" "-e" "\.${__extension}\.enc$")
done

grep --color=never "${_grep_patterns[@]}"
grep "${_grep_patterns[@]}"
;;
text|txt)
for __extension in "${_TEXT_FILE_EXTENSIONS[@]}"
do
_grep_patterns+=("-e" "\.${__extension}$")
done

grep --color=never "${_grep_patterns[@]}"
grep "${_grep_patterns[@]}"
;;
todo|todos)
for __extension in "${_TODO_FILE_EXTENSIONS[@]}"
do
_grep_patterns+=("-e" "\.${__extension}$")
done

grep --color=never "${_grep_patterns[@]}"
grep "${_grep_patterns[@]}"
;;
video|videos)
for __extension in "${_VIDEO_FILE_EXTENSIONS[@]}"
do
_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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -11946,7 +11958,6 @@ _resolve_links() {
2>/dev/null || :
else
grep \
--color=never \
--extended-regexp \
-h \
--only-matching \
Expand Down Expand Up @@ -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 ""
)"
Expand Down Expand Up @@ -16963,7 +16974,6 @@ _search() {
fi

local _grep_arguments=(
"--color=never"
"--extended-regexp"
"-I"
"--ignore-case"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 \
Expand Down Expand Up @@ -24944,7 +24957,6 @@ HEREDOC
elif _command_name="$(
printf "%s\\n" "${_normalized_input}" \
| grep \
--color=never \
--only-matching \
-e '^code' \
-e '^emacs' \
Expand Down Expand Up @@ -26015,7 +26027,7 @@ _todo() {
--visible-length "${_extra_length:-0}"

{
grep --color=never \
grep \
-e "- \[[[:space:]]*\] " \
-e "- \[x\] " \
"${__item_path:-}" || :
Expand Down

0 comments on commit 466d18e

Please sign in to comment.