sb-ticker (1622B)
1 #!/bin/bash 2 3 # Usage 4 # sb-ticker 5 # Sample output 6 # ^DJI: 0.09% 7 # CL=F: -1.88% 8 # Description 9 # displays/retrieves the latest percent-change in stock market quotes listed in $XDG_CONFIG_HOME/tickers. 10 # defaults to S&P 500, Dow Jones Industrial, and the Nasdaq 11 # 12 # intended to be used in the statusbar, which will display the first quote price in the output 13 14 url="terminal-stocks.dev" 15 pricefile="${XDG_CACHE_HOME:-$HOME/.cache}/stock-prices" 16 tickerfile="${XDG_CONFIG_HOME:-$HOME/.config}/tickers" 17 18 [ -f "$tickerfile" ] && tickers="$(cat "$tickerfile")" || tickers="^GSPC,^DJI,^IXIC"; 19 20 checkprice() { 21 [ -s "$pricefile" ] && [ "$(stat -c %y "$pricefile" 2>/dev/null | 22 cut -d':' -f1)" != "$(date '+%Y-%m-%d %H')" ] 23 } 24 25 getchange() { 26 mapfile -t changes < <(sed -e 's/ / /g' "$pricefile" | grep -oe '[m-]\+[0-9]\+\.[0-9]\+' | sed 's/[m ]/;/g') 27 IFS=',' read -ra TICKER <<< "$tickers" 28 for idx in "${!TICKER[@]}"; do 29 printf "%s: %s%%\n" "${TICKER[$idx]}" "${changes[$idx]//;/}" 30 done 31 } 32 33 updateprice() { curl -sfm 10 "$url/$tickers" --output "$pricefile" || rm -f "$pricefile" ; } 34 35 case $BLOCK_BUTTON in 36 1) setsid "$TERMINAL" -e less -Srf "$pricefile" >/dev/null 2>&1 ;; 37 2) notify-send -u low "Updating..." "Updating prices" ; updateme="1" ;; 38 3) notify-send "Current prices:" "Current stock prices:\n<b>$(getchange)</b> 39 - Left click to show price file 40 - Middle click to update stock prices 41 - Right click to get stock overview" ;; 42 8) setsid -f "$TERMINAL" -e "$EDITOR" "$0" >/dev/null 2>&1 ;; 43 esac 44 45 [ -n "$updateme" ] && updateprice 46 47 [ -f "$pricefile" ] && getchange 48 49 checkprice && updateprice