cleaned up env usage and added pretty option for symbols
This commit is contained in:
parent
d60276e403
commit
61c7c1a62b
138
bb
138
bb
@ -4,12 +4,13 @@
|
|||||||
|
|
||||||
usage() {
|
usage() {
|
||||||
cat <<EOF
|
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
|
Queries Bluebikes API to get recent bike status
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
-c, --colorize enables tmux color options
|
-c, --colorize enables tmux color options
|
||||||
|
-p, --pretty enables nerdfont symbols
|
||||||
-f, --force force refreshes bb data
|
-f, --force force refreshes bb data
|
||||||
-s, --search pulls up a searchable fzf menu to update
|
-s, --search pulls up a searchable fzf menu to update
|
||||||
displayed station
|
displayed station
|
||||||
@ -22,48 +23,64 @@ EOF
|
|||||||
|
|
||||||
# setting up vars
|
# setting up vars
|
||||||
TIMEOUT=60 # interval we request bike information at (s)
|
TIMEOUT=60 # interval we request bike information at (s)
|
||||||
|
# directorys
|
||||||
WORKING_DIR="$HOME/.local/share/bluebikes"
|
WORKING_DIR="$HOME/.local/share/bluebikes"
|
||||||
DATA_DIR="$WORKING_DIR/.data"
|
STATION_FILE=".env"
|
||||||
ALIAS_FILE="$WORKING_DIR/aliases.json"
|
ALIASES_FILE="aliases.json"
|
||||||
STATION_FILE="$DATA_DIR/.station"
|
|
||||||
|
|
||||||
# api
|
# api
|
||||||
BB_API="https://gbfs.bluebikes.com/gbfs/en/station_status.json"
|
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
|
# allows customizing search window or dropping in dmenu
|
||||||
SEARCH="fzf-tmux --layout=reverse -p 50%,50% --border"
|
SEARCH="fzf-tmux --layout=reverse -p 50%,50% --border"
|
||||||
|
|
||||||
# create directory on fresh installs
|
# create directory on fresh installs
|
||||||
if [[ ! -d "$DATA_DIR" ]] ; then
|
if [[ ! -d "$WORKING_DIR" ]] ; then
|
||||||
mkdir -p "$DATA_DIR"
|
mkdir -p "$WORKING_DIR"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# creating station alias list on fresh installs
|
cd "$WORKING_DIR"
|
||||||
if [[ ! -e "$ALIAS_FILE" ]] ; then
|
|
||||||
starter_json=$(printf '{}' | gojq '.')
|
# creating alias file on fresh install
|
||||||
echo "$starter_json" > "$ALIAS_FILE"
|
if [[ ! -e "$ALIASES_FILE" ]] ; then
|
||||||
|
echo "{}" > "$ALIASES_FILE"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
load_station_info() {
|
||||||
|
# loads the saved info about station
|
||||||
# getting station
|
# getting station
|
||||||
if [[ -e "$STATION_FILE" ]] ; then
|
if [[ ! -e "$STATION_FILE" ]] ; then
|
||||||
# if file exists
|
# doesn't exist, create it
|
||||||
source "$STATION_FILE"
|
save_station_info
|
||||||
fi
|
fi
|
||||||
|
# loading info
|
||||||
|
source "$STATION_FILE"
|
||||||
|
}
|
||||||
|
|
||||||
|
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() {
|
get_station_info() {
|
||||||
# sets $BIKES and $DOCKS based on $STATIONID
|
# sets $BIKES and $DOCKS based on $STATIONID
|
||||||
|
|
||||||
# checks that $STATIONID is set
|
# checks that $STATIONID is set
|
||||||
if [[ -z "$STATIONID" ]] ; then
|
if [[ -z "$STATION_ID" ]] ; then
|
||||||
printf 'Please set a station with bb -s' >&2
|
printf 'Please set a station with bb -s' >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# uses $UPDATE set based on update check
|
# adding ""
|
||||||
if [ "$UPDATE" = true ] ; then
|
id=$(printf '"%d"' $STATION_ID)
|
||||||
# getting new station info
|
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
|
if [[ -z "$STATION_INFO" ]] ; then
|
||||||
printf 'Error retrieving station info! Check your connection' >&2
|
printf 'Error retrieving station info! Check your connection' >&2
|
||||||
@ -74,49 +91,39 @@ get_station_info() {
|
|||||||
BIKES=$(echo "$STATION_INFO" | gojq '.num_bikes_available')
|
BIKES=$(echo "$STATION_INFO" | gojq '.num_bikes_available')
|
||||||
DOCKS=$(echo "$STATION_INFO" | gojq '.num_docks_available')
|
DOCKS=$(echo "$STATION_INFO" | gojq '.num_docks_available')
|
||||||
|
|
||||||
|
# updating expiration
|
||||||
EXPIRATION=$(($(date +%s) + $TIMEOUT))
|
EXPIRATION=$(($(date +%s) + $TIMEOUT))
|
||||||
|
|
||||||
# creating info file
|
# save the new info
|
||||||
printf 'BIKES=%d\nDOCKS=%d\n' $BIKES $DOCKS > "$DATA_DIR/$EXPIRATION.bb"
|
save_station_info
|
||||||
else
|
|
||||||
# grab existing data
|
|
||||||
source $DATA_DIR/*.bb
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
check_update() {
|
check_update() {
|
||||||
# sets $UPDATE if needed
|
# checks if an update is needed
|
||||||
file_expr=$(ls "$DATA_DIR" | grep .bb | awk -F . '{print $1}')
|
if [[ $(date +%s) -gt $EXPIRATION || "$UPDATE" = true ]] ; then
|
||||||
|
# update info
|
||||||
if [[ $(date +%s) -gt $file_expr ]] ; then
|
get_station_info
|
||||||
# out of date
|
|
||||||
UPDATE=true
|
|
||||||
|
|
||||||
# cleaning old station info
|
|
||||||
rm $DATA_DIR/*.bb 2> /dev/null
|
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
set_station_alias() {
|
set_station_alias() {
|
||||||
# sets $ALIAS for $STATION_ID
|
# sets $ALIAS for $STATION_ID
|
||||||
|
|
||||||
if [[ -z "$STATIONID" ]] ; then
|
if [[ -z "$STATION_ID" ]] ; then
|
||||||
# no station id
|
# 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
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# gojq hates qoutes
|
# gojq hates qoutes
|
||||||
FMT_ALIAS=$(printf '"%s"' $(echo $ALIAS | tr -d '"'))
|
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 "$(cat "$ALIASES_FILE" | gojq ".$FMT_STATIONID |= $FMT_ALIAS")" > "$ALIASES_FILE"
|
||||||
echo "$OUT" > "$ALIAS_FILE"
|
|
||||||
|
|
||||||
# printing change
|
# printing change
|
||||||
if [[ -z "$ALIAS" ]] ; then
|
if [[ -z "$ALIAS" ]] ; then
|
||||||
printf 'Removing alias\n'
|
printf 'Removed alias\n'
|
||||||
else
|
else
|
||||||
printf 'Updated alias to %s\n' "$ALIAS"
|
printf 'Updated alias to %s\n' "$ALIAS"
|
||||||
fi
|
fi
|
||||||
@ -125,7 +132,7 @@ set_station_alias() {
|
|||||||
|
|
||||||
update_station() {
|
update_station() {
|
||||||
# provides a gui to update the station to watch
|
# 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
|
if [[ -z "$STATIONS" ]] ; then
|
||||||
printf 'Error retrieving station info! Check your connection' >&2
|
printf 'Error retrieving station info! Check your connection' >&2
|
||||||
@ -133,16 +140,19 @@ update_station() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# prompt user to search
|
# prompt user to search
|
||||||
NEW_STATION=$(echo "$STATIONS" | gojq '.name' | tr -d '"' | eval "$SEARCH")
|
STATION_NAME=$(echo "$STATIONS" | gojq '.name' | tr -d '"' | eval "$SEARCH")
|
||||||
if [[ -z "$NEW_STATION" ]] ; then
|
if [[ -z "$STATION_NAME" ]] ; then
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# number correlating to selected name
|
# number correlating to selected name
|
||||||
STATIONID=$(echo "$STATIONS"| gojq --arg name "$NEW_STATION" '. | select( .name == $name) | .station_id' | tr -d '"') # trim quotes
|
STATION_ID=$(echo "$STATIONS"| gojq --arg name "$STATION_NAME" '. | select( .name == $name) | .station_id' | tr -d '"')
|
||||||
# setting data/.station file
|
|
||||||
output=$(printf 'STATIONID=%s\nSTATION_NAME="%s"\n' "$STATIONID" "$NEW_STATION")
|
# force an update
|
||||||
echo "$output" > "$STATION_FILE"
|
UPDATE=true
|
||||||
|
|
||||||
|
# save new station info
|
||||||
|
save_station_info
|
||||||
}
|
}
|
||||||
|
|
||||||
colorize() {
|
colorize() {
|
||||||
@ -164,17 +174,17 @@ colorize() {
|
|||||||
|
|
||||||
print_status() {
|
print_status() {
|
||||||
# prints
|
# prints
|
||||||
get_station_info # sets $DOCKS $BIKES and $STATIONID
|
|
||||||
|
|
||||||
if [[ -e "$ALIAS_FILE" ]] ; then
|
check_update # check for update
|
||||||
FMT_STATIONID=$(printf '"%d"' $STATIONID)
|
|
||||||
ALIAS=$(cat "$ALIAS_FILE" | gojq ".$FMT_STATIONID" | tr -d '"')
|
# check for alias
|
||||||
|
id=$(printf '"%d"' $STATION_ID)
|
||||||
|
ALIAS=$(cat "$ALIASES_FILE" | gojq ".$id" | tr -d '"')
|
||||||
|
|
||||||
if [[ ! -z "$ALIAS" ]] ; then
|
if [[ ! -z "$ALIAS" ]] ; then
|
||||||
|
# if there is an alias
|
||||||
STATION_NAME="$ALIAS"
|
STATION_NAME="$ALIAS"
|
||||||
fi
|
fi
|
||||||
fi
|
|
||||||
|
|
||||||
DEFAULT=""
|
|
||||||
|
|
||||||
if [[ "$COLORIZE" = true ]] ; then
|
if [[ "$COLORIZE" = true ]] ; then
|
||||||
# adding tmux colors
|
# adding tmux colors
|
||||||
@ -183,12 +193,19 @@ print_status() {
|
|||||||
BIKES=$(colorize $BIKES)
|
BIKES=$(colorize $BIKES)
|
||||||
DOCKS=$(colorize $DOCKS)
|
DOCKS=$(colorize $DOCKS)
|
||||||
fi
|
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
|
# loading info
|
||||||
check_update
|
load_station_info
|
||||||
|
|
||||||
# change long form
|
# change long form
|
||||||
for arg in "$@"; do
|
for arg in "$@"; do
|
||||||
@ -204,7 +221,7 @@ for arg in "$@"; do
|
|||||||
done
|
done
|
||||||
|
|
||||||
# parsing args
|
# parsing args
|
||||||
while getopts "hsfcr" opt ; do
|
while getopts "hsfcrp" opt ; do
|
||||||
case "$opt" in
|
case "$opt" in
|
||||||
'h' )
|
'h' )
|
||||||
usage
|
usage
|
||||||
@ -219,6 +236,9 @@ while getopts "hsfcr" opt ; do
|
|||||||
'c' )
|
'c' )
|
||||||
COLORIZE=true
|
COLORIZE=true
|
||||||
;;
|
;;
|
||||||
|
'p' )
|
||||||
|
PRETTY=true
|
||||||
|
;;
|
||||||
'r' )
|
'r' )
|
||||||
nextopt=${!OPTIND}
|
nextopt=${!OPTIND}
|
||||||
if [[ -n "$nextopt" && "$nextopt" != -* ]] ; then
|
if [[ -n "$nextopt" && "$nextopt" != -* ]] ; then
|
||||||
|
Loading…
x
Reference in New Issue
Block a user