@cryptotaxi247 / kubo / commits / a6e84f904

Updates bash completions to be compatible with zsh

The `compopt` bash builtin is not supported by zsh's bash compatibility features. By checking for zsh presence and using `complete` instead, this script becomes suitable for use with both bash and zsh.

Casey Chance committed Apr 30, 2020 at 10:11 UTC a6e84f90478930840e5bf74a83920e5aff91ce12
1 file changed +28 -19
misc/completion/ipfs-completion.bash
+28 -19
@@ -1,9 +1,18 @@
1 +_do_comp()
2 +{
3 + if [ -n "$ZSH_VERSION" ]; then
4 + complete $@
5 + else
6 + compopt $@
7 + fi
8 +}
9 +
10 _ipfs_comp()
11 {
12 COMPREPLY=( $(compgen -W "$1" -- ${word}) )
13 if [[ ${#COMPREPLY[@]} == 1 && ${COMPREPLY[0]} == "--"*"=" ]] ; then
14 # If there's only one option, with =, then discard space
6 - compopt -o nospace
15 + _do_comp -o nospace
16 fi
17 }
18
@@ -271,7 +280,7 @@ _ipfs_files_mv()
280 _ipfs_files_complete
281 else
282 COMPREPLY=( / )
274 - [[ $COMPREPLY = */ ]] && compopt -o nospace
283 + [[ $COMPREPLY = */ ]] && _do_comp -o nospace
284 fi
285 }
286
@@ -283,7 +292,7 @@ _ipfs_files_rm()
292 _ipfs_files_complete
293 else
294 COMPREPLY=( / )
286 - [[ $COMPREPLY = */ ]] && compopt -o nospace
295 + [[ $COMPREPLY = */ ]] && _do_comp -o nospace
296 fi
297 }
298 _ipfs_files_flush()
@@ -292,7 +301,7 @@ _ipfs_files_flush()
301 _ipfs_files_complete
302 else
303 COMPREPLY=( / )
295 - [[ $COMPREPLY = */ ]] && compopt -o nospace
304 + [[ $COMPREPLY = */ ]] && _do_comp -o nospace
305 fi
306 }
307
@@ -306,7 +315,7 @@ _ipfs_files_read()
315 _ipfs_files_complete
316 else
317 COMPREPLY=( / )
309 - [[ $COMPREPLY = */ ]] && compopt -o nospace
318 + [[ $COMPREPLY = */ ]] && _do_comp -o nospace
319 fi
320 }
321
@@ -322,7 +331,7 @@ _ipfs_files_write()
331 _ipfs_files_complete
332 else
333 COMPREPLY=( / )
325 - [[ $COMPREPLY = */ ]] && compopt -o nospace
334 + [[ $COMPREPLY = */ ]] && _do_comp -o nospace
335 fi
336 }
337
@@ -332,7 +341,7 @@ _ipfs_files_cp()
341 _ipfs_files_complete
342 else
343 COMPREPLY=( / )
335 - [[ $COMPREPLY = */ ]] && compopt -o nospace
344 + [[ $COMPREPLY = */ ]] && _do_comp -o nospace
345 fi
346 }
347
@@ -346,7 +355,7 @@ _ipfs_files_ls()
355 _ipfs_files_complete
356 else
357 COMPREPLY=( / )
349 - [[ $COMPREPLY = */ ]] && compopt -o nospace
358 + [[ $COMPREPLY = */ ]] && _do_comp -o nospace
359 fi
360 }
361
@@ -361,7 +370,7 @@ _ipfs_files_mkdir()
370 _ipfs_files_complete
371 else
372 COMPREPLY=( / )
364 - [[ $COMPREPLY = */ ]] && compopt -o nospace
373 + [[ $COMPREPLY = */ ]] && _do_comp -o nospace
374 fi
375 }
376
@@ -373,7 +382,7 @@ _ipfs_files_stat()
382 _ipfs_files_complete
383 else
384 COMPREPLY=( / )
376 - [[ $COMPREPLY = */ ]] && compopt -o nospace
385 + [[ $COMPREPLY = */ ]] && _do_comp -o nospace
386 fi
387 }
388
@@ -394,7 +403,7 @@ _ipfs_file_ls()
403 _ipfs_get()
404 {
405 if [ "${prev}" == "--output" ] ; then
397 - compopt -o default # Re-enable default file read
406 + _do_comp -o default # Re-enable default file read
407 COMPREPLY=()
408 elif [ "${prev}" == "--compression-level" ] ; then
409 _ipfs_comp "-1 1 2 3 4 5 6 7 8 9" # TODO: Solve autocomplete bug for "="
@@ -700,7 +709,7 @@ _ipfs_resolve()
709 else
710 opts="/ipns/ /ipfs/"
711 COMPREPLY=( $(compgen -W "${opts}" -- ${word}) )
703 - [[ $COMPREPLY = */ ]] && compopt -o nospace
712 + [[ $COMPREPLY = */ ]] && _do_comp -o nospace
713 fi
714 }
715
@@ -751,7 +760,7 @@ _ipfs_swarm_disconnect()
760 opts=$(for x in `ipfs swarm peers`; do echo ${x} ; done)
761 IFS="$OLDIFS" # Reset divider to space, ' '
762 COMPREPLY=( $(compgen -W "${opts}" -- ${word}) )
754 - [[ $COMPREPLY = */ ]] && compopt -o nospace -o filenames
763 + [[ $COMPREPLY = */ ]] && _do_comp -o nospace -o filenames
764 }
765
766 _ipfs_swarm_filters()
@@ -855,7 +864,7 @@ _ipfs_hash_complete()
864 echo "Current: ${word}" >> ~/Downloads/debug-ipfs.txt
865 COMPREPLY=( $(compgen -W "${opts}" -- ${word}) )
866 echo "Suggestion: ${COMPREPLY}" >> ~/Downloads/debug-ipfs.txt
858 - [[ $COMPREPLY = */ ]] && compopt -o nospace -o filenames # Removing whitespace after output & handle output as filenames. (Only printing the latest folder of files.)
867 + [[ $COMPREPLY = */ ]] && _do_comp -o nospace -o filenames # Removing whitespace after output & handle output as filenames. (Only printing the latest folder of files.)
868 return 0
869 }
870
@@ -866,7 +875,7 @@ _ipfs_files_complete()
875 opts=$(for x in `ipfs files ls ${lastDir}`; do echo ${lastDir}${x}/ ; done) # TODO: Implement "ipfs files ls -F" to get rid of frontslash after files. This does currently throw "Error: /cats/foo/ is not a directory"
876 IFS="$OLDIFS" # Reset divider to space, ' '
877 COMPREPLY=( $(compgen -W "${opts}" -- ${word}) )
869 - [[ $COMPREPLY = */ ]] && compopt -o nospace -o filenames
878 + [[ $COMPREPLY = */ ]] && _do_comp -o nospace -o filenames
879 return 0
880 }
881
@@ -901,7 +910,7 @@ _ipfs_multiaddr_complete()
910 opts="/ip4/ /ip6/"
911 COMPREPLY=( $(compgen -W "${opts}" -- ${word}) )
912 fi
904 - [[ $COMPREPLY = */ ]] && compopt -o nospace -o filenames
913 + [[ $COMPREPLY = */ ]] && _do_comp -o nospace -o filenames
914 return 0
915 }
916
@@ -913,19 +922,19 @@ _ipfs_pinned_complete()
922 IFS="$OLDIFS"
923 if [[ ${#COMPREPLY[*]} -eq 1 ]]; then # Only one completion, remove pretty output
924 COMPREPLY=( ${COMPREPLY[0]/ *//} ) #Remove ' ' and everything after
916 - [[ $COMPREPLY = */ ]] && compopt -o nospace # Removing whitespace after output
925 + [[ $COMPREPLY = */ ]] && _do_comp -o nospace # Removing whitespace after output
926 fi
927 }
928 _ipfs_filesystem_complete()
929 {
921 - compopt -o default # Re-enable default file read
930 + _do_comp -o default # Re-enable default file read
931 COMPREPLY=()
932 }
933
934 _ipfs()
935 {
936 COMPREPLY=()
928 - compopt +o default # Disable default to not deny completion, see: http://stackoverflow.com/a/19062943/1216348
937 + _do_comp +o default # Disable default to not deny completion, see: http://stackoverflow.com/a/19062943/1216348
938
939 local word="${COMP_WORDS[COMP_CWORD]}"
940 local prev="${COMP_WORDS[COMP_CWORD-1]}"