#!/usr/bin/env sh
# wallstreet widget: scrolling line of gainers (+), losers (-) or headlines
#
# Part of wallstreet.  Copyright 2014-2026 Dustin Kirkland
# <dustin.kirkland@gmail.com>, macOS port and data sources 2026.
# Licensed under the Apache License, Version 2.0; see LICENSE.

PKG=wallstreet
WSDATA="$(dirname "$0")/../../share/$PKG/wsdata"
# Ctrl-C in any pane ends the show: close the window all widgets live in
quit() {
	[ -n "$TMUX_PANE" ] && tmux kill-window -t "$TMUX_PANE" >/dev/null 2>&1
	exit 0
}
trap quit INT QUIT TERM

while true; do
	case "$1" in
		+) color=green;  delay=0.12; text=$("$WSDATA" movers up) ;;
		-) color=red;    delay=0.14; text=$("$WSDATA" movers down) ;;
		*) color=yellow; delay=0.08
		   text=$("$WSDATA" headlines | sort -R | head -n 40 | sed -e 's/$/ ... /' | tr -d '\n') ;;
	esac
	if [ -z "$text" ]; then
		printf '\rwaiting for data ...'
		sleep 10
		continue
	fi
	"$WSDATA" marquee "$color" "$delay" "$text" || sleep 10
done
