From e02520a526ff54f7fec2bd2920181d2f87bbe341 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Fri, 30 Sep 2022 20:26:00 +0300 Subject: [PATCH 1/5] fix(xdg-mime): complete only one query type argument --- completions/xdg-mime | 3 ++- test/t/test_xdg_mime.py | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/completions/xdg-mime b/completions/xdg-mime index 44b50968698..f5afd99f404 100644 --- a/completions/xdg-mime +++ b/completions/xdg-mime @@ -31,7 +31,8 @@ _xdg_mime() COMPREPLY=($(compgen -W 'filetype default' -- "$cur")) return fi - case ${words[2]} in # TODO and args == 3 (takes only one arg!) + ((args == 3)) || return + case ${words[2]} in filetype) _filedir ;; default) _xdg_mime_mimetype ;; esac diff --git a/test/t/test_xdg_mime.py b/test/t/test_xdg_mime.py index 432be0676bd..5bcd8587830 100644 --- a/test/t/test_xdg_mime.py +++ b/test/t/test_xdg_mime.py @@ -26,3 +26,7 @@ def test_5(self, completion): @pytest.mark.complete("xdg-mime install --mode ") def test_6(self, completion): assert completion + + @pytest.mark.complete("xdg-mime query filetype foo ") + def test_filetype_one_arg(self, completion): + assert not completion From 097a402bf7a730d3a049e81275e4fa7ba3d714d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Fri, 30 Sep 2022 20:38:20 +0300 Subject: [PATCH 2/5] feat(xdg-mime): complete MIME types from system MIME dir contents --- completions/xdg-mime | 15 ++++++++++++--- test/t/test_xdg_mime.py | 4 ++-- test/test-cmd-list.txt | 1 + 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/completions/xdg-mime b/completions/xdg-mime index f5afd99f404..3b1f1ce1291 100644 --- a/completions/xdg-mime +++ b/completions/xdg-mime @@ -2,9 +2,18 @@ _xdg_mime_mimetype() { - COMPREPLY+=($(compgen -S / -W 'application audio font image message model - multipart text video' -- "$cur")) - [[ ${COMPREPLY-} == */ ]] && compopt -o nospace + local d i + for d in /usr/share/mime /usr/local/share/mime; do + command cd /usr/share/mime 2>/dev/null || continue + trap "command cd - &>/dev/null" RETURN + _filedir xml + for i in ${!COMPREPLY[*]}; do + # packages/ is not a MIME type dir + [[ ${COMPREPLY[i]} != packages* ]] || unset -v "COMPREPLY[i]" + done + COMPREPLY=("${COMPREPLY[@]%.xml}") + return + done } _xdg_mime() diff --git a/test/t/test_xdg_mime.py b/test/t/test_xdg_mime.py index 5bcd8587830..2453dc298ad 100644 --- a/test/t/test_xdg_mime.py +++ b/test/t/test_xdg_mime.py @@ -1,7 +1,7 @@ import pytest -@pytest.mark.bashcomp(cmd="xdg-mime") +@pytest.mark.bashcomp(cmd="xdg-mime", ignore_env=r"^[+-](OLD)?PWD=") class TestXdgMime: @pytest.mark.complete("xdg-mime ") def test_1(self, completion): @@ -19,7 +19,7 @@ def test_3(self, completion): def test_4(self, completion): assert completion - @pytest.mark.complete("xdg-mime default foo.desktop ") + @pytest.mark.complete("xdg-mime default foo.desktop ", require_cmd=True) def test_5(self, completion): assert completion diff --git a/test/test-cmd-list.txt b/test/test-cmd-list.txt index ae2f174f049..c8e236dbfca 100644 --- a/test/test-cmd-list.txt +++ b/test/test-cmd-list.txt @@ -429,6 +429,7 @@ wol wsimport wtf wvdial +xdg-mime xdg-settings xev xfreerdp From f722a8b5289721c92e80bf054cf15132e6ace31b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Tue, 22 Nov 2022 00:05:18 +0200 Subject: [PATCH 3/5] fix(xdg-mime): actually `cd` to all candidate mime dirs --- completions/xdg-mime | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/completions/xdg-mime b/completions/xdg-mime index 3b1f1ce1291..076c1ab10d3 100644 --- a/completions/xdg-mime +++ b/completions/xdg-mime @@ -4,7 +4,7 @@ _xdg_mime_mimetype() { local d i for d in /usr/share/mime /usr/local/share/mime; do - command cd /usr/share/mime 2>/dev/null || continue + command cd $d 2>/dev/null || continue trap "command cd - &>/dev/null" RETURN _filedir xml for i in ${!COMPREPLY[*]}; do From 4fc5c89a462d22fdc685af79c8eade8ae476dba4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Wed, 23 Nov 2022 23:37:39 +0200 Subject: [PATCH 4/5] fix(xdg-mime): trap and cd issues --- completions/xdg-mime | 22 ++++++++++++++-------- test/t/test_xdg_mime.py | 2 +- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/completions/xdg-mime b/completions/xdg-mime index 076c1ab10d3..b4c36144433 100644 --- a/completions/xdg-mime +++ b/completions/xdg-mime @@ -3,17 +3,23 @@ _xdg_mime_mimetype() { local d i + local -a arr for d in /usr/share/mime /usr/local/share/mime; do - command cd $d 2>/dev/null || continue - trap "command cd - &>/dev/null" RETURN - _filedir xml - for i in ${!COMPREPLY[*]}; do - # packages/ is not a MIME type dir - [[ ${COMPREPLY[i]} != packages* ]] || unset -v "COMPREPLY[i]" + arr=($( + command cd "$d" 2>/dev/null || exit 1 + compgen -f -o plusdirs -X "!*.xml" -- "$cur" + )) || continue + for i in ${!arr[*]}; do + case ${arr[i]} in + packages*) unset -v "arr[i]" ;; # not a MIME type dir + *.xml) arr[i]=${arr[i]%.xml} ;; + */*) ;; + *) arr[i]+=/ ;; + esac done - COMPREPLY=("${COMPREPLY[@]%.xml}") - return + COMPREPLY+=("${arr[@]}") done + [[ $COMPREPLY != */ ]] || compopt -o nospace } _xdg_mime() diff --git a/test/t/test_xdg_mime.py b/test/t/test_xdg_mime.py index 2453dc298ad..b91e75a0b41 100644 --- a/test/t/test_xdg_mime.py +++ b/test/t/test_xdg_mime.py @@ -1,7 +1,7 @@ import pytest -@pytest.mark.bashcomp(cmd="xdg-mime", ignore_env=r"^[+-](OLD)?PWD=") +@pytest.mark.bashcomp(cmd="xdg-mime") class TestXdgMime: @pytest.mark.complete("xdg-mime ") def test_1(self, completion): From 79ca043dcd2e240787d6264867fde7b3566acd98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Sat, 26 Nov 2022 00:07:08 +0200 Subject: [PATCH 5/5] fix(xdg-mime): nounset and IFS fixes Co-authored-by: Koichi Murase --- completions/xdg-mime | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/completions/xdg-mime b/completions/xdg-mime index b4c36144433..39d784194cf 100644 --- a/completions/xdg-mime +++ b/completions/xdg-mime @@ -9,7 +9,7 @@ _xdg_mime_mimetype() command cd "$d" 2>/dev/null || exit 1 compgen -f -o plusdirs -X "!*.xml" -- "$cur" )) || continue - for i in ${!arr[*]}; do + for i in "${!arr[*]}"; do case ${arr[i]} in packages*) unset -v "arr[i]" ;; # not a MIME type dir *.xml) arr[i]=${arr[i]%.xml} ;; @@ -17,9 +17,10 @@ _xdg_mime_mimetype() *) arr[i]+=/ ;; esac done - COMPREPLY+=("${arr[@]}") + ((${#arr[@]})) && + COMPREPLY+=("${arr[@]}") done - [[ $COMPREPLY != */ ]] || compopt -o nospace + [[ ${COMPREPLY-} != */ ]] || compopt -o nospace } _xdg_mime()