Skip to content

Commit

Permalink
feat(MultipleDevices): show a menu if multiple simulators are booted
Browse files Browse the repository at this point in the history
fix #2
  • Loading branch information
fjcaetano committed Sep 27, 2019
1 parent e670550 commit be6b7a7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
1 change: 1 addition & 0 deletions HomebrewFormula/ios-simulator-gif.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class IosSimulatorGif < Formula
sha256 "3a34bc612b7a7832bb46a30774b063845635fd56f1bd3744be65cbc0f4ceaa03"

depends_on "ffmpeg"
depends_on "gnu-sed"

def install
bin.install "bin/ios-simulator-gif"
Expand Down
50 changes: 43 additions & 7 deletions bin/ios-simulator-gif
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#!/bin/sh
#!/bin/bash
#
# Records iOS simulator screen and saves as a gif to simulator.gif
# A new file name may be passed as argument

RED="\033[1;31m"
GREEN="\033[1;32m"
CLEAR="\033[0m"

##############
### MAIN ###
##############
Expand All @@ -17,21 +21,53 @@ main() {
### Check Booted ###
#####################
check_booted() {
xcrun simctl list | grep Booted &> /dev/null
VERSION=""
COUNT=0
MENU=()
UUIDS=()
UUID="booted"

# Parse simulators
while read -r SIM; do
NAME=$(echo "$SIM" | gsed '/Booted/!{q1}; {s/^\(.*\) (.*(Booted)/\1/}')

if [ $? -eq 1 ]; then
VERSION=$(echo "$SIM" | gsed '/-- /!{q1}; {s/-- \(.*\) --/\1/}')
continue
fi

UUID=$(echo "$SIM" | gsed '/Booted/!{q1}; {s/^.* (\(.*\)) .*$/\1/}')
UUIDS+=($UUID)

if [[ $? -ne 0 ]]; then
MENU+=("[$COUNT]: $NAME ($VERSION)")
((COUNT++))
done < <(xcrun simctl list | grep 'Booted\|-- ')

if [[ $COUNT -eq 0 ]]; then
echo "No devices are booted"
exit 404
elif [[ $COUNT -gt 1 ]]; then
echo -e "There are more than one simulator booted:\n"
printf '%s\n' "${MENU[@]}"

res=-1
while (( res < 0 || res >= $COUNT )); do
printf "\\nPlease, select an option: [0-%d]: " $(expr $COUNT - 1)
read -n1 res
done

UUID=${UUIDS[$res]}
echo -e "\n"
fi
}

##################
### Record ###
##################
record() {
echo 'Recording...\n\n[Press any key to stop recording]'
echo -e "${RED}Recording...${CLEAR}\n\n[Press any key to stop recording]"

xcrun simctl io booted recordVideo /tmp/simulator.mov &
xcrun simctl io ${UUID} recordVideo /tmp/simulator.mov &
PID=$!

read -n1
Expand All @@ -42,11 +78,11 @@ record() {
### Convert GIF ###
#######################
convert_gif() {
echo "Converting and saving to ${FILE_NAME}\n"
echo -e "Converting and saving to ${FILE_NAME}\n"
ffmpeg -i /tmp/simulator.mov -vf $VIDEOFILTER -r $RATE -f $FORMAT -y $FILE_NAME $EXTRA &> /tmp/ffmpeg.log

if [[ $? -eq 0 ]]; then
echo "Gif saved to ${FILE_NAME}"
echo -e "${GREEN}Gif saved to ${FILE_NAME}${CLEAR}"
else
cat /tmp/ffmpeg.log
fi
Expand Down

0 comments on commit be6b7a7

Please sign in to comment.