Skip to content

Instantly share code, notes, and snippets.

@kulvind3r
Created June 13, 2021 18:15
Show Gist options
  • Save kulvind3r/79eaeb810761fb7eca45d72edfc7eb5e to your computer and use it in GitHub Desktop.
Save kulvind3r/79eaeb810761fb7eca45d72edfc7eb5e to your computer and use it in GitHub Desktop.
Script that randomly suggest something to do from your favourites. Useful for those slow days when you are bored to death and have no idea what to do with your life.
#!/bin/bash
set -e
SCRIPT=$0
ARG=$1
ACTIVITIES_FILE=~/what_next_activities.json
# Colors
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
NOCOLOR=$(tput sgr0)
which jq > /dev/null || { echo "dependency 'jq' not found. Exiting."; exit 1;}
usage() {
echo -n " ${SCRIPT} [OPTIONS]
Smartly suggests a random activity to do next
Options:
-s|--suggest Suggest an activity
-u|--update Update acitivities list.
"
exit 1
}
create_sample_actitivies_file() {
cat << EOF > $ACTIVITIES_FILE
[
{
"name": "<category_1>",
"activities": [
"<category_example_1>",
"<category_example_2>",
"<category_example_3>"
]
},
{
"name": "<category_2>",
"activities": [
"<category_example_1>",
"<category_example_2>",
"<category_example_3>"
]
}
]
EOF
}
suggest() {
LENGTH=$(jq length $ACTIVITIES_FILE)
[[ $LENGTH == 0 ]] && { printf "\nNo More Suggestions left!! Exiting\n"; return 0; }
SELECTED_ACTIVITY_INDEX=$(( RANDOM % LENGTH ))
CATEGORY=$(jq -r .[$SELECTED_ACTIVITY_INDEX].name $ACTIVITIES_FILE)
printf "\nCategory: %s%s%s" "${RED}" "${CATEGORY}" "${NOCOLOR}"
printf "\nSuggestions:\n"
jq -r ".[$SELECTED_ACTIVITY_INDEX].activities[]" $ACTIVITIES_FILE | while read -r ACTIVITY; do
echo "* ${GREEN}$ACTIVITY${NOCOLOR}"
done
jq "del(.[$SELECTED_ACTIVITY_INDEX])" $ACTIVITIES_FILE > $ACTIVITIES_FILE.tmp; mv $ACTIVITIES_FILE.tmp $ACTIVITIES_FILE;
read -rp "Suggest another? (Y/N) " ANOTHER_SUGGESTION
if [[ $ANOTHER_SUGGESTION == [yY] ]]
then
suggest
fi
}
update() {
echo "Opening activities json save file after making changes. Ensure valid json."
sleep 3
[[ -f $ACTIVITIES_FILE ]] || create_sample_actitivies_file
EDITOR=$(which vi || which nano)
cp $ACTIVITIES_FILE $ACTIVITIES_FILE.bak
$EDITOR $ACTIVITIES_FILE
if ! $(cat $ACTIVITIES_FILE | jq empty);
then
echo "Invalid JSON. reverting file changes. Please start over.";
mv $ACTIVITIES_FILE.bak $ACTIVITIES_FILE
exit 1;
fi
}
case $ARG in
-s | --suggest)
cp $ACTIVITIES_FILE $ACTIVITIES_FILE.preserved
suggest
mv $ACTIVITIES_FILE.preserved $ACTIVITIES_FILE
;;
-u | --update)
update
;;
*)
usage
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment