about summary refs log blame commit diff stats
path: root/bin/fl
blob: 8c058d79955b2c0cd315f4d3d7a64950648d44dc (plain) (tree)































                                                                                
#!/bin/sh
# fl: Find Links
# uses regular expressions to find links in the stream it's given,
# or its args if it is given args

# from pure-sh-bible, strips from the start of a string
lstrip() {
	printf '%s\n' "${1##$2}"
}

# put url-finding regex in a variable so no lines go over 80 chars
re_urls='((https?://|www\.)[[:alnum:].]*:?[[:alnum:]./@$&%?$#=_-]*)'

# finds links, puts them in a list
# sort -u prevents duplicates, but also sorts the links
urlparse() {
	lstrip "$(cat)" "*\│" |\
	grep -Eo "$re_urls" |\
	sort -u | sed 's|^www.|http://www\.|g'
}
if [ -n "$1" ]; then
	urls=$(echo "$@"|urlparse)
else
	urls=$(urlparse)
fi

# wipe IFS so dmenu handles being sent the links properly
IFS=

# send any found links to dmenu so one can be chosen to be sent to the clipboard
[ -n "$urls" ] &&\
	echo $urls | dmenu -i -p 'copy which url?' -l 10 | xclip -r -sel c