This commit is contained in:
Andrzej Zabost 2025-10-21 21:05:08 -03:00 committed by GitHub
commit 94ac1a7da0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 155 additions and 0 deletions

47
plugins/avd/README.md Normal file
View file

@ -0,0 +1,47 @@
# avd plugin
The avd plugin provides a few functions for Android Virtual Device /
Android Emulator commands to list all the available AVDs and launch them
more easily.
To use it, add avd to the plugins array of your zshrc file:
```zsh
plugins=(... avd)
```
## Requirements
In order to make this work, you will need to have the Android `emulator`
tool set up in your path. This plugin will try to find that tool in
`$ANDROID_HOME` as a last resort, however it will make it slower and may
not work as expected.
## Functions
- `avds` - Lists all the AVDs
- `-s` will skip the AVD numbers at the beginning of each output line
- `avd [-v] <n>` - Launches n-th AVD from the AVDs list printed by
`avds`
- `-v` will let stdout and stderr print to the console which is
disabled by default to avoid the clutter
- `find_emulator` - Tries to find the `emulator` tool either in your
path or `$ANDROID_HOME` directory
## Aliases
- `emus [-s]` - Same as `avds [-s]`
- `emu [-v] <n>` - Same as `avd [-v] <n>`
## Exemplary usage:
```
~/
> avds
1:Pixel_2_API_30
2:Samsung_Tab_A_2019_API_25
3:Samsung_Tab_A_2019_API_29
~/
> avd 2
Starting emulator: Samsung_Tab_A_2019_API_25
```

108
plugins/avd/avd.plugin.zsh Normal file
View file

@ -0,0 +1,108 @@
function find_emulator() {
local emulator_found
if [[ -n $(command -v emulator) ]]; then
emulator_found="emulator"
elif [[ -d "$ANDROID_HOME" ]]; then
# If there is no emulator in the path, try to find it in $ANDROID_HOME if it
# is defined.
emulator_found=$(find "$ANDROID_HOME" -name emulator -type f | head -1)
fi
if [[ -n $emulator_found ]]; then
echo "$emulator_found"
else
cat <<< "Emulator not found" 1>&2
# Use "emulator" even if it's not found just to let the other functions fail
# later.
echo "emulator"
fi
}
function avds() {
_avds_usage() {
echo "Usage: avds [-s]"
}
local OPTIND o emulator_path cmd_to_execute skip_numbers
while getopts ":s" o; do
case "$o" in
s)
skip_numbers=1;;
*)
_avds_usage; return;;
esac
done
shift $((OPTIND-1))
emulator_path=$(find_emulator)
cmd_to_execute="$emulator_path -list-avds"
if [[ $skip_numbers -eq 1 ]]; then
# Just print all AVDs.
eval "$cmd_to_execute"
else
# Print all AVDs and prepend each output line with a number, starting at 1.
eval "$cmd_to_execute" | grep -n '^'
fi
}
function avd() {
_avd_usage() {
echo "Usage: avd [-v] [AVD position on the list]"
avds
}
local OPTIND o verbose avd_number avd_name avds_count emulator_path
local cmd_to_execute
while getopts ":v" o; do
case "$o" in
v)
verbose=1;;
*)
_avd_usage; return;;
esac
done
shift $((OPTIND-1))
avd_number="$1"
if [[ -z $avd_number ]]; then
_avd_usage
return
fi
avds_count=$(avds | wc -l | grep -Eo '[0-9]+') || return
if [[ $avds_count -le 0 ]]; then
echo "No AVDs found"
return
fi
if [[ ($avd_number -lt 1 || $avd_number -gt $avds_count) ]]; then
echo "The number must be in 1..${avds_count}"
return
fi
# Print only the n-th AVD name.
avd_name=$(avds -s | head -"$avd_number" | tail -1)
echo "Starting emulator: $avd_name"
emulator_path=$(find_emulator)
cmd_to_execute="$emulator_path -avd $avd_name"
if [[ $verbose -eq 1 ]]; then
eval "$cmd_to_execute"
else
eval "$cmd_to_execute" > /dev/null 2>&1
fi
}
alias emus="avds"
alias emu="avd"