From c9f405b24423d17e714f2c97fc46904a7455fe2c Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Mon, 26 Feb 2018 11:47:45 +0100 Subject: [PATCH] fix bash completion of package names for the filter arg Not all devices can do `ls /data/data`, so instead use a couple other more reliable sources: * pm package list (older Android versions) * cmd package list (newer Android versions) * ps (all Android versions) Also, --device and --emulator are not adb flags, only pidcat, so they need to be corrected before being passed to adb. --- bash_completion.d/pidcat | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/bash_completion.d/pidcat b/bash_completion.d/pidcat index 3b54d8a..16ba879 100644 --- a/bash_completion.d/pidcat +++ b/bash_completion.d/pidcat @@ -15,9 +15,15 @@ function _pidcat() # so that the list of installed packaged can be fetched from the device case "${prev}" in - -d|-e|--device|--emulator) + -d|-e) device_selected="${prev}" ;; + --device) + device_selected="-d" + ;; + --emulator) + device_selected="-e" + ;; esac # use device set by serial number @@ -27,11 +33,11 @@ function _pidcat() ;; esac - if [ -z "$device_selected" ]; then + if [ -z "$device_selected" ] && [ "$prev" != "-s" ]; then local num_devices=$(( $(adb devices 2>/dev/null|wc -l) - 2 )) if [ "$num_devices" -gt "1" ]; then # With multiple devices, you must choose a device first. - COMPREPLY=( $(compgen -W "-d -e -s --device --emulator --serial" -- ${cur}) ) + COMPREPLY=( $(compgen -W "-s" -- ${cur}) ) return 0 fi fi @@ -57,8 +63,13 @@ function _pidcat() ;; esac - # if we can get the package names, then use those - local apks=$(adb $device_selected shell ls /data/data 2>/dev/null | tr '\n' ' ' | tr -d '\r') + # Try three methods for getting appIDs, at least one should work on all devices. + # Filter stderr on device/emulator to reduce data coming back from adb. + # Filter stderr on this machine to prevent adb errors from showing from tab completion + local apks=$(adb $device_selected shell '(pm package list packages -3; cmd package list packages -3; ps) 2> /dev/null' 2> /dev/null \ + | sed -n -e 's,^u[0-9].* \([^/][^/]*$\),\1,p' -e 's,^package:,,p' \ + | tr '\n' ' ' \ + | tr -d '\r' ) COMPREPLY=( $(compgen -W "$apks" -- ${cur}) ) return 0 }