shellfolio/shellfolio.sh

79 lines
2.9 KiB
Bash
Raw Normal View History

2018-01-23 14:21:06 +01:00
#!/usr/pkg/bin/bash
. ~/.shellfolio
minus=$(tput setaf 1)
plus=$(tput setaf 2)
neutral=$(tput sgr0)
colwidths="$neutral%5s %14s %7s %7s %7s %12s %12s %12s\n"
totalvalue=0
totalgain=0
2018-01-23 14:21:06 +01:00
# checks if value is positive or negative, returns color code
2018-01-23 14:21:06 +01:00
colorize() {
if [[ $1 == *"-"* ]]; then
color=$minus
else
color=$plus
fi
echo $color
}
# print table header
printf "${colwidths}" "Coin" "USD" "+/- 1h" "+/- 24h" "+/- 7d" "Amount" "Value" "Gain"
printf "${colwidths}" "-----" "--------------" "-------" "-------" "-------" "------------" "------------" "------------"
2018-01-23 14:21:06 +01:00
# print one line per coin
2018-01-23 14:21:06 +01:00
for coin in $COINS
do
# fetch data from coinmarketcap.com
2018-01-23 14:21:06 +01:00
result=$(curl -s -XGET "https://api.coinmarketcap.com/v1/ticker/$coin/?convert=usd")
if [[ $currency != *"error"* ]]; then
symbol=$(echo $result | jq ".[0].symbol" | xargs printf "%s\n")
change1h=$(echo $result | jq ".[0].percent_change_1h" | xargs printf "%.*f\n" 2)
change24h=$(echo $result | jq ".[0].percent_change_24h" | xargs printf "%.*f\n" 2)
change7d=$(echo $result | jq ".[0].percent_change_7d" | xargs printf "%.*f\n" 2)
2018-01-23 17:03:31 +01:00
usd=$(echo $result | jq ".[0].price_usd" | xargs printf "%.*f\n" 8)
# set color to green or red
2018-01-23 14:21:06 +01:00
col1h=$(colorize $change1h)
col24h=$(colorize $change24h)
col7d=$(colorize $change7d)
# check if $symbol is set, split into $amount and $paid
2018-01-23 14:21:06 +01:00
if [ -z ${!symbol} ]; then
amount=0
paid=0
2018-01-23 14:21:06 +01:00
else
IFS=: read -r amount paid <<< "${!symbol}"
2018-01-23 14:21:06 +01:00
fi
# calculates value of coins
2018-01-23 14:21:06 +01:00
value=$(echo "scale=2; $amount*$usd" | bc)
2018-01-23 17:03:31 +01:00
usd=$(echo "scale=2; $usd" | bc)
totalvalue=$(echo "scale=2; $value+$totalvalue" | bc)
2018-01-23 14:21:06 +01:00
# calculates gain
if [ "$paid" -eq "0" ]; then
gain=0
else
gain=$(echo "scale=2; $value-$paid" | bc)
fi
colgain=$(colorize $gain)
totalgain=$(echo "scale=2; $gain+$totalgain" | bc)
# prints actual table line
printf "$neutral%5s %14.6f $col1h%7s $col24h%7s $col7d%7s $neutral%12s %12.2f $colgain%12.2f$neutral\n" "$symbol" "$usd" "$change1h%" "$change24h%" "$change7d%" "$amount" "$value" "$gain"
2018-01-23 14:21:06 +01:00
fi
done
# prints table footer
printf "${colwidths}" "-----" "--------------" "-------" "-------" "-------" "------------" "------------" "------------"
#prints total coin value and total gain
colgain=$(colorize $totalgain)
printf "%70.2f $colgain%12.2f$neutral\n" "$totalvalue" "$totalgain"
2018-01-23 14:21:06 +01:00
exit 0