cleaned up env usage and added pretty option for symbols
This commit is contained in:
parent
d60276e403
commit
61c7c1a62b
164
bb
164
bb
@ -4,12 +4,13 @@
|
||||
|
||||
usage() {
|
||||
cat <<EOF
|
||||
usage: $0 [-c][-f][-s][-h][-r [option]]
|
||||
usage: $0 [-c][-p][-f][-s][-h][-r [option]]
|
||||
|
||||
Queries Bluebikes API to get recent bike status
|
||||
|
||||
Options:
|
||||
-c, --colorize enables tmux color options
|
||||
-p, --pretty enables nerdfont symbols
|
||||
-f, --force force refreshes bb data
|
||||
-s, --search pulls up a searchable fzf menu to update
|
||||
displayed station
|
||||
@ -22,101 +23,107 @@ EOF
|
||||
|
||||
# setting up vars
|
||||
TIMEOUT=60 # interval we request bike information at (s)
|
||||
# directorys
|
||||
WORKING_DIR="$HOME/.local/share/bluebikes"
|
||||
DATA_DIR="$WORKING_DIR/.data"
|
||||
ALIAS_FILE="$WORKING_DIR/aliases.json"
|
||||
STATION_FILE="$DATA_DIR/.station"
|
||||
STATION_FILE=".env"
|
||||
ALIASES_FILE="aliases.json"
|
||||
|
||||
# api
|
||||
BB_API="https://gbfs.bluebikes.com/gbfs/en/station_status.json"
|
||||
BB_STATION_API="https://gbfs.bluebikes.com/gbfs/en/station_information.json"
|
||||
|
||||
# allows customizing search window or dropping in dmenu
|
||||
SEARCH="fzf-tmux --layout=reverse -p 50%,50% --border"
|
||||
|
||||
# create directory on fresh installs
|
||||
if [[ ! -d "$DATA_DIR" ]] ; then
|
||||
mkdir -p "$DATA_DIR"
|
||||
if [[ ! -d "$WORKING_DIR" ]] ; then
|
||||
mkdir -p "$WORKING_DIR"
|
||||
fi
|
||||
|
||||
# creating station alias list on fresh installs
|
||||
if [[ ! -e "$ALIAS_FILE" ]] ; then
|
||||
starter_json=$(printf '{}' | gojq '.')
|
||||
echo "$starter_json" > "$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
|
||||
|
Loading…
x
Reference in New Issue
Block a user