Skip to content

Commit a09c124

Browse files
authored
Merge pull request #827 from scop/feat/xdg-mime-improvements
feat(xdg-mime): MIME type and arg completion improvements
2 parents d1ed7df + 79ca043 commit a09c124

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

completions/xdg-mime

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,25 @@
22

33
_xdg_mime_mimetype()
44
{
5-
COMPREPLY+=($(compgen -S / -W 'application audio font image message model
6-
multipart text video' -- "$cur"))
7-
[[ ${COMPREPLY-} == */ ]] && compopt -o nospace
5+
local d i
6+
local -a arr
7+
for d in /usr/share/mime /usr/local/share/mime; do
8+
arr=($(
9+
command cd "$d" 2>/dev/null || exit 1
10+
compgen -f -o plusdirs -X "!*.xml" -- "$cur"
11+
)) || continue
12+
for i in "${!arr[*]}"; do
13+
case ${arr[i]} in
14+
packages*) unset -v "arr[i]" ;; # not a MIME type dir
15+
*.xml) arr[i]=${arr[i]%.xml} ;;
16+
*/*) ;;
17+
*) arr[i]+=/ ;;
18+
esac
19+
done
20+
((${#arr[@]})) &&
21+
COMPREPLY+=("${arr[@]}")
22+
done
23+
[[ ${COMPREPLY-} != */ ]] || compopt -o nospace
824
}
925

1026
_xdg_mime()
@@ -31,7 +47,8 @@ _xdg_mime()
3147
COMPREPLY=($(compgen -W 'filetype default' -- "$cur"))
3248
return
3349
fi
34-
case ${words[2]} in # TODO and args == 3 (takes only one arg!)
50+
((args == 3)) || return
51+
case ${words[2]} in
3552
filetype) _filedir ;;
3653
default) _xdg_mime_mimetype ;;
3754
esac

test/t/test_xdg_mime.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@ def test_3(self, completion):
1919
def test_4(self, completion):
2020
assert completion
2121

22-
@pytest.mark.complete("xdg-mime default foo.desktop ")
22+
@pytest.mark.complete("xdg-mime default foo.desktop ", require_cmd=True)
2323
def test_5(self, completion):
2424
assert completion
2525

2626
@pytest.mark.complete("xdg-mime install --mode ")
2727
def test_6(self, completion):
2828
assert completion
29+
30+
@pytest.mark.complete("xdg-mime query filetype foo ")
31+
def test_filetype_one_arg(self, completion):
32+
assert not completion

test/test-cmd-list.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ wol
429429
wsimport
430430
wtf
431431
wvdial
432+
xdg-mime
432433
xdg-settings
433434
xev
434435
xfreerdp

0 commit comments

Comments
 (0)