Skip to content

feat(xdg-mime): MIME type and arg completion improvements #827

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions completions/xdg-mime
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,25 @@

_xdg_mime_mimetype()
{
COMPREPLY+=($(compgen -S / -W 'application audio font image message model
multipart text video' -- "$cur"))
[[ ${COMPREPLY-} == */ ]] && compopt -o nospace
local d i
local -a arr
for d in /usr/share/mime /usr/local/share/mime; do
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
((${#arr[@]})) &&
COMPREPLY+=("${arr[@]}")
done
[[ ${COMPREPLY-} != */ ]] || compopt -o nospace
}

_xdg_mime()
Expand All @@ -31,7 +47,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
Expand Down
6 changes: 5 additions & 1 deletion test/t/test_xdg_mime.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ 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

@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
1 change: 1 addition & 0 deletions test/test-cmd-list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ wol
wsimport
wtf
wvdial
xdg-mime
xdg-settings
xev
xfreerdp
Expand Down