diff --git a/xdg-desktop-search b/xdg-desktop-search index e53eae8..f193c5f 100755 --- a/xdg-desktop-search +++ b/xdg-desktop-search @@ -6,12 +6,13 @@ ## # Configuration +name=$(basename "$(readlink -f "$0")") IFS=":" xdg_data_dirs=${XDG_DATA_DIRS:-/usr/share/gnome:$HOME/.local/share/flatpak/exports/share:/var/lib/flatpak/exports/share:/usr/local/share:/usr/share} -search_regex=".*" +search_regex=.* help=false verbose=false -show_directives_regex=".*" +show_directives_regex=.* search_directives_regex="(Name|Exec)" ## @@ -161,40 +162,26 @@ parse_options() { esac done - [[ "${#search_directives[@]}" -gt 0 ]] && search_directives_regex="($(join "|" "${search_directives[@]}"))" - [[ "${#show_directives[@]}" -gt 0 ]] && show_directives_regex="^($(join "|" "${show_directives[@]}"))=" - [[ "${#search[@]}" -gt 0 ]] && search_regex="^$search_directives_regex=.*($(join "|" "${search[@]}"))" -} - -valid_data_dirs() { - local dirs=$1 - local IFS=: - local result=() - - for dir in $dirs ; do - if [[ -d $dir/applications ]] && [[ -r $dir/applications ]] ; then - result+=( "$dir" ) - fi - done - - echo "${result[*]}" + [[ "${#search_directives[@]}" -gt 0 ]] && search_directives_regex="($(join "|" ${search_directives[@]}))" + [[ "${#show_directives[@]}" -gt 0 ]] && show_directives_regex="^($(join "|" ${show_directives[@]}))=" + [[ "${#search[@]}" -gt 0 ]] && search_regex="^$search_directives_regex=.*($(join "|" ${search[@]}))" } ## # Main Program -parse_options "$@" +parse_options $@ "$help" && { usage ; exit 0 ; } found=0 -for _data_dir in $(valid_data_dirs "$xdg_data_dirs") ; do +for _data_dir in $xdg_data_dirs ; do data_dir=$(realpath --canonicalize-missing --no-symlinks "$_data_dir") applications="$data_dir/applications" [[ -d $applications ]] && for desktop in "$applications"/*.desktop ; do - grep -Eiqs "$search_regex" "$desktop" && { + grep -Eiq "$search_regex" "$desktop" && { (( found++ )) if "$verbose" ; then echo "# $desktop"