diff --git a/shellfolio.sh b/shellfolio.sh index d10ce62..9922620 100644 --- a/shellfolio.sh +++ b/shellfolio.sh @@ -5,9 +5,11 @@ minus=$(tput setaf 1) plus=$(tput setaf 2) neutral=$(tput sgr0) -colwidths="$neutral%6s %15s %8s %8s %8s %12s %12s\n" -total=0 +colwidths="$neutral%5s %14s %7s %7s %7s %12s %12s %12s\n" +totalvalue=0 +totalgain=0 +# checks if value is positive or negative, returns color code colorize() { if [[ $1 == *"-"* ]]; then color=$minus @@ -17,12 +19,14 @@ colorize() { echo $color } +# print table header +printf "${colwidths}" "Coin" "USD" "+/- 1h" "+/- 24h" "+/- 7d" "Amount" "Value" "Gain" +printf "${colwidths}" "-----" "--------------" "-------" "-------" "-------" "------------" "------------" "------------" -printf "${colwidths}" "Symbol" "USD" "+/- 1h" "+/- 24h" "+/- 7d" "Amount" "Value" -printf "${colwidths}" "------" "---------------" "--------" "--------" "--------" "------------" "------------" - +# print one line per coin for coin in $COINS do + # fetch data from coinmarketcap.com 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") @@ -30,27 +34,45 @@ do 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) usd=$(echo $result | jq ".[0].price_usd" | xargs printf "%.*f\n" 8) - + + # set color to green or red col1h=$(colorize $change1h) col24h=$(colorize $change24h) col7d=$(colorize $change7d) - + + # check if $symbol is set, split into $amount and $paid if [ -z ${!symbol} ]; then amount=0 + paid=0 else - amount=${!symbol} + IFS=: read -r amount paid <<< "${!symbol}" fi - + + # calculates value of coins value=$(echo "scale=2; $amount*$usd" | bc) usd=$(echo "scale=2; $usd" | bc) - total=$(echo "scale=2; $value+$total" | bc) + totalvalue=$(echo "scale=2; $value+$totalvalue" | bc) - printf "$neutral%6s %15.6f $col1h%8s $col24h%8s $col7d%8s $neutral%12s %12.2f\n" "$symbol" "$usd" "$change1h%" "$change24h%" "$change7d%" "$amount" "$value" + # 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" fi done -printf "${colwidths}" "------" "---------------" "--------" "--------" "--------" "------------" "------------" -printf "%75.2f\n" "$total" +# 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" exit 0