Stringify adb completions

Also reindent.
This commit is contained in:
Fabian Homborg 2017-05-03 23:35:34 +02:00
parent 4c798ce3b4
commit 044d45a0ff

View file

@ -1,64 +1,61 @@
# Completions for Android adb command # Completions for Android adb command
function __fish_adb_no_subcommand --description 'Test if adb has yet to be given the subcommand' function __fish_adb_no_subcommand --description 'Test if adb has yet to be given the subcommand'
for i in (commandline -opc) for i in (commandline -opc)
if contains -- $i connect disconnect devices push pull sync shell emu logcat install uninstall jdwp forward bugreport backup restore version help wait-for-device start-server kill-server remount reboot get-state get-serialno get-devpath status-window root usb tcpip ppp sideload reconnect if contains -- $i connect disconnect devices push pull sync shell emu logcat install uninstall jdwp forward bugreport backup restore version help wait-for-device start-server kill-server remount reboot get-state get-serialno get-devpath status-window root usb tcpip ppp sideload reconnect
return 1 return 1
end end
end end
return 0 return 0
end end
function __fish_adb_get_devices --description 'Run adb devices and parse output' function __fish_adb_get_devices --description 'Run adb devices and parse output'
# This seems reasonably portable for all the platforms adb runs on # This seems reasonably portable for all the platforms adb runs on
set -l count (ps x | grep -c adb) set -l procs (ps -Ao comm= | string match 'adb')
set -l TAB \t # Don't run adb devices unless the server is already started - it takes a while to init
# Don't run adb devices unless the server is already started - it takes a while to init if set -q procs[1]
if [ $count -gt 1 ] adb devices -l | string replace -rf '(\S+) +.' '$1'\t | string replace -r \t'.*model:(\S+).*' \t'$1'
# The tail is to strip the header line, the sed is to massage the -l format end
# into a simple "identifier <TAB> modelname" format which is what we want for complete
adb devices -l | tail -n +2 | sed -E -e "s/([^ ]+) +./\1$TAB/" -e "s/$TAB.*model:([^ ]+).*/$TAB\1/"
end
end end
function __fish_adb_run_command --description 'Runs adb with any -s parameters already given on the command line' function __fish_adb_run_command --description 'Runs adb with any -s parameters already given on the command line'
set -l sopt set -l sopt
set -l sopt_is_next set -l sopt_is_next
set -l cmd (commandline -poc) set -l cmd (commandline -poc)
set -e cmd[1] set -e cmd[1]
for i in $cmd for i in $cmd
if test $sopt_is_next if test $sopt_is_next
set sopt -s $i set sopt -s $i
break break
else else
switch $i switch $i
case -s case -s
set sopt_is_next 1 set sopt_is_next 1
end end
end end
end end
# If no -s option, see if there's a -d or -e instead # If no -s option, see if there's a -d or -e instead
if test -z "$sopt" if test -z "$sopt"
if contains -- -d $cmd if contains -- -d $cmd
set sopt '-d' set sopt '-d'
else if contains -- -e $cmd else if contains -- -e $cmd
set sopt '-e' set sopt '-e'
end end
end end
# adb returns CRLF (seemingly) so strip CRs # adb returns CRLF (seemingly) so strip CRs
adb $sopt shell $argv | sed s/\r// adb $sopt shell $argv | string replace -a \r ''
end end
function __fish_adb_list_packages function __fish_adb_list_packages
__fish_adb_run_command pm list packages ^/dev/null | sed s/package:// __fish_adb_run_command pm list packages ^/dev/null | string replace 'package:' ''
end end
function __fish_adb_list_uninstallable_packages function __fish_adb_list_uninstallable_packages
# -3 doesn't exactly mean show uninstallable, but it's the closest you can get to with pm list # -3 doesn't exactly mean show uninstallable, but it's the closest you can get to with pm list
__fish_adb_run_command pm list packages -3 | sed s/package:// __fish_adb_run_command pm list packages -3 | string replace 'package:' ''
end end
# Generic options, must come before command # Generic options, must come before command