2013-12-05 18:38:42 +08:00
# Completions for Android adb command
2017-10-12 01:17:35 +08:00
function __fish_adb_no_subcommand -d 'Test if adb has yet to be given the subcommand'
2024-01-22 14:42:45 +08:00
for i in ( commandline -xpc )
2023-03-15 11:39:55 +08:00
if contains -- $i connect disconnect devices push pull sync shell emu logcat install uninstall jdwp forward bugreport backup restore version help start-server kill -server remount reboot get-state get-serialno get-devpath status -window root usb tcpip ppp sideload reconnect unroot exec -out
2017-05-04 05:35:34 +08:00
return 1
end
end
return 0
2013-12-05 18:38:42 +08:00
end
2017-10-12 01:17:35 +08:00
function __fish_adb_get_devices -d 'Run adb devices and parse output'
2017-05-04 05:35:34 +08:00
# This seems reasonably portable for all the platforms adb runs on
set -l procs ( ps -Ao comm = | string match 'adb' )
# Don't run adb devices unless the server is already started - it takes a while to init
if set -q procs [ 1 ]
2023-03-11 06:42:54 +08:00
adb devices -l | string replace -rf '(\S+).*product:(\S+).*model:(\S+).*' '$1' \t '$2 $3'
2017-05-04 05:35:34 +08:00
end
2013-12-05 18:38:42 +08:00
end
2022-10-02 00:21:52 +08:00
function __fish_adb_get_tcpip_devices -d 'Get list of devices connected via TCP/IP'
__fish_adb_get_devices | string match -r '^\d+\.\d+\.\d+\.\d+:\d+\t.*'
end
2017-10-12 01:17:35 +08:00
function __fish_adb_run_command -d 'Runs adb with any -s parameters already given on the command line'
2017-05-04 05:35:34 +08:00
set -l sopt
set -l sopt_is_next
2024-01-22 14:42:45 +08:00
set -l cmd ( commandline -pxc )
2017-05-04 05:35:34 +08:00
set -e cmd [ 1 ]
for i in $cmd
2019-01-27 21:23:36 +08:00
if test -n " $sopt_is_next "
2017-05-04 05:35:34 +08:00
set sopt -s $i
break
else
switch $i
case -s
set sopt_is_next 1
end
end
end
2013-12-05 18:38:42 +08:00
2017-05-04 05:35:34 +08:00
# If no -s option, see if there's a -d or -e instead
if test -z " $sopt "
if contains -- -d $cmd
2020-03-10 02:36:12 +08:00
set sopt -d
2017-05-04 05:35:34 +08:00
else if contains -- -e $cmd
2020-03-10 02:36:12 +08:00
set sopt -e
2017-05-04 05:35:34 +08:00
end
end
2013-12-05 18:38:42 +08:00
2017-05-04 05:35:34 +08:00
# adb returns CRLF (seemingly) so strip CRs
adb $sopt shell $argv | string replace -a \r ''
2013-12-05 18:38:42 +08:00
end
function __fish_adb_list_packages
2020-06-23 15:02:26 +08:00
# That "2\>" is to pass the redirection *to adb*.
# It sends stderr from commands it executes to its stdout as well.
# Why it does that, I don't know - crossing the streams is a bad idea (c.f. Ghostbusters)
2020-06-20 04:32:10 +08:00
__fish_adb_run_command pm list packages 2 \> /dev/null | string replace 'package:' ''
2013-12-05 18:38:42 +08:00
end
function __fish_adb_list_uninstallable_packages
2017-05-04 05:35:34 +08:00
# -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 | string replace 'package:' ''
2013-12-05 18:38:42 +08:00
end
2024-09-19 02:12:45 +08:00
function __fish_adb_list_local_files
set -l token ( commandline -ct ) *
# Unquoted $token to expand the array
# Return list of directories suffixed with '/'
find $token -maxdepth 0 -type d 2 > /dev/null | string replace -r '$' /
# Return list of files
find $token -maxdepth 0 -type f -o -type l 2 > /dev/null
end
2018-05-03 13:06:32 +08:00
function __fish_adb_list_files
2019-05-05 18:53:09 +08:00
set -l token ( commandline -ct )
2018-05-03 13:06:32 +08:00
2019-05-05 18:53:09 +08:00
# Have tab complete show initial / if nothing on current token
if test -z " $token "
2020-03-10 02:36:12 +08:00
set token /
2019-05-05 18:53:09 +08:00
end
2018-05-03 13:06:32 +08:00
2019-05-05 18:53:09 +08:00
# Return list of directories suffixed with '/'
2024-03-09 11:56:37 +08:00
__fish_adb_run_command find -H " $token * " -maxdepth 0 -type d 2 \> /de v/null | string replace -r '$' /
2019-05-05 18:53:09 +08:00
# Return list of files
2020-06-20 04:32:10 +08:00
__fish_adb_run_command find -H " $token * " -maxdepth 0 -type f 2 \> /dev/null
2018-05-03 13:06:32 +08:00
end
2023-03-15 11:39:55 +08:00
function __fish_adb_list_bin
# list all binary without group
__fish_adb_run_command ls -1 /system/bin/ 2 \> /dev/null
__fish_adb_run_command ls -1 /system/xbin/ 2 \> /dev/null
end
function __fish_adb_list_properties
__fish_adb_run_command getprop | string match -rg '\[(.*)\]:'
end
2013-12-05 18:38:42 +08:00
# Generic options, must come before command
2022-10-02 00:21:52 +08:00
complete -n __fish_adb_no_subcommand -c adb -o a -d 'Listen on all network interfaces'
complete -n __fish_adb_no_subcommand -c adb -o d -d 'Use first USB device'
complete -n __fish_adb_no_subcommand -c adb -o e -d 'Use first TCP/IP device'
complete -n __fish_adb_no_subcommand -c adb -o s -x -a "(__fish_adb_get_devices)" -d 'Use device with given serial'
complete -n __fish_adb_no_subcommand -c adb -o t -d 'Use device with given transport id'
complete -n __fish_adb_no_subcommand -c adb -o H -d 'Name of adb server host'
complete -n __fish_adb_no_subcommand -c adb -o P -d 'Port of adb server'
complete -n __fish_adb_no_subcommand -c adb -o L -d 'Listen on given socket for adb server'
2013-12-05 18:38:42 +08:00
# Commands
2020-03-10 02:36:12 +08:00
complete -f -n __fish_adb_no_subcommand -c adb -a connect -d 'Connect to device'
complete -f -n __fish_adb_no_subcommand -c adb -a disconnect -d 'Disconnect from device'
complete -f -n __fish_adb_no_subcommand -c adb -a devices -d 'List all connected devices'
complete -f -n __fish_adb_no_subcommand -c adb -a push -d 'Copy file to device'
complete -f -n __fish_adb_no_subcommand -c adb -a pull -d 'Copy file from device'
complete -f -n __fish_adb_no_subcommand -c adb -a sync -d 'Copy host->device only if changed'
complete -f -n __fish_adb_no_subcommand -c adb -a shell -d 'Run remote shell [command]'
complete -f -n __fish_adb_no_subcommand -c adb -a emu -d 'Run emulator console command'
complete -f -n __fish_adb_no_subcommand -c adb -a logcat -d 'View device log'
complete -f -n __fish_adb_no_subcommand -c adb -a install -d 'Install package'
complete -f -n __fish_adb_no_subcommand -c adb -a uninstall -d 'Uninstall package'
complete -f -n __fish_adb_no_subcommand -c adb -a jdwp -d 'List PIDs of processes hosting a JDWP transport'
complete -f -n __fish_adb_no_subcommand -c adb -a forward -d 'Port forwarding'
complete -f -n __fish_adb_no_subcommand -c adb -a bugreport -d 'Return bugreport information'
complete -f -n __fish_adb_no_subcommand -c adb -a backup -d 'Perform device backup'
complete -f -n __fish_adb_no_subcommand -c adb -a restore -d 'Restore device from backup'
complete -f -n __fish_adb_no_subcommand -c adb -a version -d 'Show adb version'
complete -f -n __fish_adb_no_subcommand -c adb -a help -d 'Show adb help'
complete -f -n __fish_adb_no_subcommand -c adb -a wait -for-device -d 'Block until device is online'
complete -f -n __fish_adb_no_subcommand -c adb -a start-server -d 'Ensure that there is a server running'
complete -f -n __fish_adb_no_subcommand -c adb -a kill -server -d 'Kill the server if it is running'
complete -f -n __fish_adb_no_subcommand -c adb -a remount -d 'Remounts the /system partition on the device read-write'
complete -f -n __fish_adb_no_subcommand -c adb -a reboot -d 'Reboots the device, optionally into the bootloader or recovery program'
complete -f -n __fish_adb_no_subcommand -c adb -a get-state -d 'Prints state of the device'
complete -f -n __fish_adb_no_subcommand -c adb -a get-serialno -d 'Prints serial number of the device'
complete -f -n __fish_adb_no_subcommand -c adb -a get-devpath -d 'Prints device path'
complete -f -n __fish_adb_no_subcommand -c adb -a status -window -d 'Continuously print the device status'
complete -f -n __fish_adb_no_subcommand -c adb -a root -d 'Restart the adbd daemon with root permissions'
2023-03-11 06:42:54 +08:00
complete -f -n __fish_adb_no_subcommand -c adb -a unroot -d 'Restart the adbd daemon without root permissions'
2020-03-10 02:36:12 +08:00
complete -f -n __fish_adb_no_subcommand -c adb -a usb -d 'Restart the adbd daemon listening on USB'
complete -f -n __fish_adb_no_subcommand -c adb -a tcpip -d 'Restart the adbd daemon listening on TCP'
complete -f -n __fish_adb_no_subcommand -c adb -a ppp -d 'Run PPP over USB'
complete -f -n __fish_adb_no_subcommand -c adb -a sideload -d 'Sideloads the given package'
complete -f -n __fish_adb_no_subcommand -c adb -a reconnect -d 'Kick current connection from host side and make it reconnect.'
2023-03-15 11:39:55 +08:00
complete -f -n __fish_adb_no_subcommand -c adb -a exec -out -d 'Execute a command on the device and send its stdout back'
2013-12-05 18:38:42 +08:00
# install options
complete -n '__fish_seen_subcommand_from install' -c adb -s l -d 'Forward-lock the app'
complete -n '__fish_seen_subcommand_from install' -c adb -s r -d 'Reinstall the app keeping its data'
complete -n '__fish_seen_subcommand_from install' -c adb -s s -d 'Install on SD card instead of internal storage'
2020-03-10 02:36:12 +08:00
complete -n '__fish_seen_subcommand_from install' -c adb -l algo -d 'Algorithm name'
complete -n '__fish_seen_subcommand_from install' -c adb -l key -d 'Hex-encoded key'
complete -n '__fish_seen_subcommand_from install' -c adb -l iv -d 'Hex-encoded iv'
2022-06-25 04:20:35 +08:00
complete -n '__fish_seen_subcommand_from install' -c adb -ka '(__fish_complete_suffix .apk)'
2013-12-05 18:38:42 +08:00
# uninstall
complete -n '__fish_seen_subcommand_from uninstall' -c adb -s k -d 'Keep the data and cache directories'
2016-12-20 19:08:13 +08:00
complete -n '__fish_seen_subcommand_from uninstall' -c adb -f -a "(__fish_adb_list_uninstallable_packages)"
2013-12-05 18:38:42 +08:00
# devices
2022-09-19 23:52:09 +08:00
complete -n '__fish_seen_subcommand_from devices' -c adb -f
2013-12-05 18:38:42 +08:00
complete -n '__fish_seen_subcommand_from devices' -c adb -s l -d 'Also list device qualifiers'
# disconnect
2022-10-02 00:21:52 +08:00
complete -n '__fish_seen_subcommand_from disconnect' -c adb -x -a "(__fish_adb_get_tcpip_devices)" -d 'Device to disconnect'
2013-12-05 18:38:42 +08:00
# backup
complete -n '__fish_seen_subcommand_from backup' -c adb -s f -d 'File to write backup data to'
2020-03-10 02:36:12 +08:00
complete -n '__fish_seen_subcommand_from backup' -c adb -o apk -d 'Enable backup of the .apks themselves'
complete -n '__fish_seen_subcommand_from backup' -c adb -o noapk -d 'Disable backup of the .apks themselves (default)'
complete -n '__fish_seen_subcommand_from backup' -c adb -o obb -d 'Enable backup of any installed apk expansion'
complete -n '__fish_seen_subcommand_from backup' -c adb -o noobb -d 'Disable backup of any installed apk expansion (default)'
complete -n '__fish_seen_subcommand_from backup' -c adb -o shared -d 'Enable backup of the device\' s shared storage / SD card contents'
complete -n '__fish_seen_subcommand_from backup' -c adb -o noshared -d 'Disable backup of the device\' s shared storage / SD card contents ( default ) '
complete -n '__fish_seen_subcommand_from backup' -c adb -o all -d 'Back up all installed applications'
complete -n '__fish_seen_subcommand_from backup' -c adb -o system -d 'Include system applications in -all (default)'
complete -n '__fish_seen_subcommand_from backup' -c adb -o nosystem -d 'Exclude system applications in -all'
2013-12-05 18:38:42 +08:00
complete -n '__fish_seen_subcommand_from backup' -c adb -f -a "(__fish_adb_list_packages)" -d 'Package(s) to backup'
# reboot
2022-10-02 00:21:52 +08:00
complete -n '__fish_seen_subcommand_from reboot' -c adb -x -a 'bootloader recovery fastboot'
2013-12-05 18:38:42 +08:00
# forward
2020-03-10 02:36:12 +08:00
complete -n '__fish_seen_subcommand_from forward' -c adb -l list -d 'List all forward socket connections'
complete -n '__fish_seen_subcommand_from forward' -c adb -l no-rebind -d 'Fails the forward if local is already forwarded'
complete -n '__fish_seen_subcommand_from forward' -c adb -l remove -d 'Remove a specific forward socket connection'
complete -n '__fish_seen_subcommand_from forward' -c adb -l remove-all -d 'Remove all forward socket connections'
2017-02-15 19:30:44 +08:00
# sideload
2020-12-13 15:27:21 +08:00
complete -n '__fish_seen_subcommand_from sideload' -c adb -k -xa '(__fish_complete_suffix .zip)'
2017-02-15 19:30:44 +08:00
# reconnect
2020-03-10 02:36:12 +08:00
complete -n '__fish_seen_subcommand_from reconnect' -c adb -x -a device -d 'Kick current connection from device side and make it reconnect.'
2018-05-03 13:06:32 +08:00
# commands that accept listing device files
complete -n '__fish_seen_subcommand_from shell' -c adb -f -a "(__fish_adb_list_files)" -d 'File on device'
2022-10-02 00:21:52 +08:00
complete -n '__fish_seen_subcommand_from pull' -c adb -F -a "(__fish_adb_list_files)" -d 'File on device'
2024-09-19 02:12:45 +08:00
complete -n '__fish_seen_subcommand_from push' -c adb -ka "(__fish_adb_list_files)" -d 'File on device'
complete -n '__fish_seen_subcommand_from push' -c adb -ka "(__fish_adb_list_local_files)"
2022-09-19 23:52:09 +08:00
# logcat
complete -n '__fish_seen_subcommand_from logcat' -c adb -f
# general options
complete -n '__fish_seen_subcommand_from logcat' -c adb -s L -l last -d 'Dump logs from prior to last reboot from pstore'
2023-08-17 03:56:08 +08:00
complete -n '__fish_seen_subcommand_from logcat' -c adb -s b -l buffer -d ' Request alternate ring buffer(s)' -xa '(__fish_append , main system radio events crash default all)'
2022-09-19 23:52:09 +08:00
complete -n '__fish_seen_subcommand_from logcat' -c adb -s c -l clear -d 'Clear (flush) the entire log and exit'
complete -n '__fish_seen_subcommand_from logcat' -c adb -s d -d 'Dump the log and then exit (don\' t block) '
complete -n '__fish_seen_subcommand_from logcat' -c adb -l pid -d 'Only print the logs for the given PID'
complete -n '__fish_seen_subcommand_from logcat' -c adb -l wrap -d 'Sleep for 2 hours or when buffer about to wrap whichever comes first'
# formatting
complete -n '__fish_seen_subcommand_from logcat' -c adb -s v -l format -d 'Sets log print format verb and adverbs' -xa ' brief help long process raw tag thread threadtime time color descriptive epoch monotonic printable uid usec UTC year zone'
complete -n '__fish_seen_subcommand_from logcat' -c adb -s D -l dividers -d 'Print dividers between each log buffer'
complete -n '__fish_seen_subcommand_from logcat' -c adb -s B -l binary -d 'Output the log in binary'
# outfile files
complete -n '__fish_seen_subcommand_from logcat' -c adb -s f -l file -d 'Log to file instead of stdout'
complete -n '__fish_seen_subcommand_from logcat' -c adb -s r -l rotate-kbytes -d 'Rotate log every kbytes, requires -f'
complete -n '__fish_seen_subcommand_from logcat' -c adb -s n -l rotate-count -d 'Sets number of rotated logs to keep, default 4'
2024-05-17 00:49:52 +08:00
complete -n '__fish_seen_subcommand_from logcat' -c adb -l id -d 'If the given signature for logging changes, clear the associated files'
2022-09-19 23:52:09 +08:00
# logd control
complete -n '__fish_seen_subcommand_from logcat' -c adb -s g -l buffer-size -d 'Get the size of the ring buffers within logd'
complete -n '__fish_seen_subcommand_from logcat' -c adb -s G -l buffer-size -d 'Set size of a ring buffer in logd'
complete -n '__fish_seen_subcommand_from logcat' -c adb -s S -l statistics -d 'Output statistics'
complete -n '__fish_seen_subcommand_from logcat' -c adb -s p -l prune -d 'Print prune rules'
complete -n '__fish_seen_subcommand_from logcat' -c adb -s P -l prune -d 'Set prune rules'
# filtering
complete -n '__fish_seen_subcommand_from logcat' -c adb -s s -d 'Set default filter to silent'
complete -n '__fish_seen_subcommand_from logcat' -c adb -s e -l regex -d 'Only print lines where the log message matches'
complete -n '__fish_seen_subcommand_from logcat' -c adb -s m -l max-count -d 'Quit after print <count> lines'
complete -n '__fish_seen_subcommand_from logcat' -c adb -l print -d 'Print all message even if they do not matches, requires --regex and --max-count'
complete -n '__fish_seen_subcommand_from logcat' -c adb -l uid -d 'Only display log messages from UIDs present in the comma separate list <uids>'
2023-03-15 11:39:55 +08:00
# commands that accept listing device binaries
complete -n '__fish_seen_subcommand_from exec-out' -c adb -f -a "(__fish_adb_list_bin)" -d "Command on device"
complete -n '__fish_seen_subcommand_from shell' -c adb -f -a "(__fish_adb_list_bin)" -d "Command on device"
# setprop and getprop in shell
complete -n '__fish_seen_subcommand_from setprop' -c adb -f -a "(__fish_adb_list_properties)" -d 'Property to set'
complete -n '__fish_seen_subcommand_from getprop' -c adb -f -a "(__fish_adb_list_properties)" -d 'Property to get'