#!/usr/local/bin/andy

set -e NOTIFY_LONG  unicode
set -e NOTIFY_SHORT `basename $args[0]

func setup {
	curl 'https://www.unicode.org/Public/UNIDATA/UnicodeData.txt'
	| sed -E '
		s/;[^;]*//2g
		s/\<(.)([A-Z]*)/\1\L\2/2g
		/^[^;]*;</d
		/Compatibility/d
		/Variation Selector/d
		s/[^;]*/\\u&/
		s/^\\u([^;]{5})/\\U000\1/
	' >$DATA
}

set DATA $(XDG_DATA_HOME:$HOME/.local/share)/unicode-data
test -f $DATA || setup

func read_runes {
	cut -d';' -f1 $DATA | read -gd\n runes
}

async read_runes
set i `cut -d';' -f2 $DATA | osel -i
wait
set rune `printf $runes[$i]

if test -t 2 {
	wl-copy -n $rune
	| notify 'Rune Copied' "The rune ‘$rune’ was copied to the clipboard"
} else {
	wtype -- $rune
}