From 61c7c1a62bda03388ffd235905097c75cb8257c6 Mon Sep 17 00:00:00 2001 From: KeeganForelight Date: Sun, 15 Jan 2023 18:16:28 -0500 Subject: [PATCH] cleaned up env usage and added pretty option for symbols --- bb | 164 ++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 92 insertions(+), 72 deletions(-) diff --git a/bb b/bb index 95aa2da..bc5c249 100755 --- a/bb +++ b/bb @@ -4,12 +4,13 @@ usage() { cat < "$ALIAS_FILE" +cd "$WORKING_DIR" + +# creating alias file on fresh install +if [[ ! -e "$ALIASES_FILE" ]] ; then + echo "{}" > "$ALIASES_FILE" fi -# getting station -if [[ -e "$STATION_FILE" ]] ; then - # if file exists +load_station_info() { + # loads the saved info about station + # getting station + if [[ ! -e "$STATION_FILE" ]] ; then + # doesn't exist, create it + save_station_info + fi + # loading info source "$STATION_FILE" -fi +} + +save_station_info() { + # saves relevant info to file + + printf '%s="%s"\n' \ + "STATION_ID" "$STATION_ID" \ + "BIKES" "$BIKES" \ + "DOCKS" "$DOCKS" \ + "STATION_NAME" "$STATION_NAME" \ + "EXPIRATION" "$EXPIRATION" > "$STATION_FILE" +} get_station_info() { # sets $BIKES and $DOCKS based on $STATIONID # checks that $STATIONID is set - if [[ -z "$STATIONID" ]] ; then + if [[ -z "$STATION_ID" ]] ; then printf 'Please set a station with bb -s' >&2 exit 1 fi - # uses $UPDATE set based on update check - if [ "$UPDATE" = true ] ; then - # getting new station info + # adding "" + id=$(printf '"%d"' $STATION_ID) + STATION_INFO=$(curl --silent -fL $BB_API | gojq ".data.stations[] | select( .station_id == $id)" 2>/dev/null) - STATION_INFO=$(curl --silent -fL $BB_API | gojq --arg id $STATIONID '.data.stations[] | select( .station_id == $id)' 2>/dev/null) - - if [[ -z "$STATION_INFO" ]] ; then - printf 'Error retrieving station info! Check your connection' >&2 - exit 1 - fi - - # set vars - BIKES=$(echo "$STATION_INFO" | gojq '.num_bikes_available') - DOCKS=$(echo "$STATION_INFO" | gojq '.num_docks_available') - - - EXPIRATION=$(($(date +%s) + $TIMEOUT)) - - # creating info file - printf 'BIKES=%d\nDOCKS=%d\n' $BIKES $DOCKS > "$DATA_DIR/$EXPIRATION.bb" - else - # grab existing data - source $DATA_DIR/*.bb + if [[ -z "$STATION_INFO" ]] ; then + printf 'Error retrieving station info! Check your connection' >&2 + exit 1 fi + + # set vars + BIKES=$(echo "$STATION_INFO" | gojq '.num_bikes_available') + DOCKS=$(echo "$STATION_INFO" | gojq '.num_docks_available') + + # updating expiration + EXPIRATION=$(($(date +%s) + $TIMEOUT)) + + # save the new info + save_station_info } check_update() { - # sets $UPDATE if needed - file_expr=$(ls "$DATA_DIR" | grep .bb | awk -F . '{print $1}') - - if [[ $(date +%s) -gt $file_expr ]] ; then - # out of date - UPDATE=true - - # cleaning old station info - rm $DATA_DIR/*.bb 2> /dev/null + # checks if an update is needed + if [[ $(date +%s) -gt $EXPIRATION || "$UPDATE" = true ]] ; then + # update info + get_station_info fi } set_station_alias() { # sets $ALIAS for $STATION_ID - if [[ -z "$STATIONID" ]] ; then + if [[ -z "$STATION_ID" ]] ; then # no station id - printf 'No Station set to change name for!\nPlease run bb -s to set a station' >&2 + printf 'No Station set to change name for! Please run bb -s to set a station\n' >&2 exit 1 fi # gojq hates qoutes FMT_ALIAS=$(printf '"%s"' $(echo $ALIAS | tr -d '"')) - FMT_STATIONID=$(printf '"%d"' $STATIONID) + FMT_STATIONID=$(printf '"%d"' $STATION_ID) - OUT=$(cat "$ALIAS_FILE" | gojq ".$FMT_STATIONID |= $FMT_ALIAS") - echo "$OUT" > "$ALIAS_FILE" + echo "$(cat "$ALIASES_FILE" | gojq ".$FMT_STATIONID |= $FMT_ALIAS")" > "$ALIASES_FILE" # printing change if [[ -z "$ALIAS" ]] ; then - printf 'Removing alias\n' + printf 'Removed alias\n' else printf 'Updated alias to %s\n' "$ALIAS" fi @@ -125,7 +132,7 @@ set_station_alias() { update_station() { # provides a gui to update the station to watch - STATIONS=$(curl --silent -fL https://gbfs.bluebikes.com/gbfs/en/station_information.json | gojq '.data.stations[]') + STATIONS=$(curl --silent -fL "$BB_STATION_API" | gojq '.data.stations[]') if [[ -z "$STATIONS" ]] ; then printf 'Error retrieving station info! Check your connection' >&2 @@ -133,16 +140,19 @@ update_station() { fi # prompt user to search - NEW_STATION=$(echo "$STATIONS" | gojq '.name' | tr -d '"' | eval "$SEARCH") - if [[ -z "$NEW_STATION" ]] ; then + STATION_NAME=$(echo "$STATIONS" | gojq '.name' | tr -d '"' | eval "$SEARCH") + if [[ -z "$STATION_NAME" ]] ; then exit 0 fi # number correlating to selected name - STATIONID=$(echo "$STATIONS"| gojq --arg name "$NEW_STATION" '. | select( .name == $name) | .station_id' | tr -d '"') # trim quotes - # setting data/.station file - output=$(printf 'STATIONID=%s\nSTATION_NAME="%s"\n' "$STATIONID" "$NEW_STATION") - echo "$output" > "$STATION_FILE" + STATION_ID=$(echo "$STATIONS"| gojq --arg name "$STATION_NAME" '. | select( .name == $name) | .station_id' | tr -d '"') + + # force an update + UPDATE=true + + # save new station info + save_station_info } colorize() { @@ -164,18 +174,18 @@ colorize() { print_status() { # prints - get_station_info # sets $DOCKS $BIKES and $STATIONID + + check_update # check for update - if [[ -e "$ALIAS_FILE" ]] ; then - FMT_STATIONID=$(printf '"%d"' $STATIONID) - ALIAS=$(cat "$ALIAS_FILE" | gojq ".$FMT_STATIONID" | tr -d '"') - if [[ ! -z "$ALIAS" ]] ; then - STATION_NAME="$ALIAS" - fi + # check for alias + id=$(printf '"%d"' $STATION_ID) + ALIAS=$(cat "$ALIASES_FILE" | gojq ".$id" | tr -d '"') + + if [[ ! -z "$ALIAS" ]] ; then + # if there is an alias + STATION_NAME="$ALIAS" fi - DEFAULT="" - if [[ "$COLORIZE" = true ]] ; then # adding tmux colors DEFAULT="#[default]" @@ -183,12 +193,19 @@ print_status() { BIKES=$(colorize $BIKES) DOCKS=$(colorize $DOCKS) fi - printf '%s  %s  %s%s\n' $BIKES $DOCKS "$DEFAULT" "$STATION_NAME" + + if [[ "$PRETTY" = true ]] ; then + # print with symbols + printf '%s  %s  at %s%s\n' $BIKES $DOCKS "$DEFAULT" "$STATION_NAME" + else + # print raw + printf '%d Bikes %d Docks at %s%s\n' $BIKES $DOCKS "$DEFAULT" "$STATION_NAME" + fi } -# always check/clean status -check_update +# loading info +load_station_info # change long form for arg in "$@"; do @@ -204,7 +221,7 @@ for arg in "$@"; do done # parsing args -while getopts "hsfcr" opt ; do +while getopts "hsfcrp" opt ; do case "$opt" in 'h' ) usage @@ -219,6 +236,9 @@ while getopts "hsfcr" opt ; do 'c' ) COLORIZE=true ;; + 'p' ) + PRETTY=true + ;; 'r' ) nextopt=${!OPTIND} if [[ -n "$nextopt" && "$nextopt" != -* ]] ; then