From 48290dbfd2a33909856149d5031c8c94130ebeda Mon Sep 17 00:00:00 2001 From: vg Date: Wed, 24 Sep 2025 16:14:20 +0200 Subject: simplify url management and allow [digit] context keeping --- fzf-url.sh | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/fzf-url.sh b/fzf-url.sh index b15841f..4fe501c 100755 --- a/fzf-url.sh +++ b/fzf-url.sh @@ -44,23 +44,26 @@ else content="$(tmux capture-pane -J -p -S -"$limit")" fi -mapfile -t urls < <(echo "$content" |grep -oE '(https?|ftp|file):/?//[-A-Za-z0-9+&@#/%?=~_|!:,.;]*[-A-Za-z0-9+&@#/%=~_|]') -mapfile -t wwws < <(echo "$content" |grep -oE '(http?s://)?www\.[a-zA-Z](-?[a-zA-Z0-9])+\.[a-zA-Z]{2,}(/\S+)*' | grep -vE '^https?://' |sed 's/^\(.*\)$/http:\/\/\1/') -mapfile -t ips < <(echo "$content" |grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(:[0-9]{1,5})?(/\S+)*' |sed 's/^\(.*\)$/http:\/\/\1/') -mapfile -t gits < <(echo "$content" |grep -oE '(ssh://)?git@\S*' | sed 's/:/\//g' | sed 's/^\(ssh\/\/\/\)\{0,1\}git@\(.*\)$/https:\/\/\2/') +# debug +#exec >/tmp/url.log 2>&1 +#set -x -if [[ $# -ge 1 && "$1" != '' ]]; then - mapfile -t extras < <(echo "$content" |eval "$1") -fi - -items=$(printf '%s\n' "${urls[@]}" "${wwws[@]}" "${ips[@]}" "${gits[@]}" "${extras[@]}" | - grep -v '^$' | - nl -w3 -s ' ' +regexes=( + '(\[\d+\]\s*)?(https?|ftp|file):/?//[-A-Za-z0-9+&@#/%?=~_|!:,.;]*[-A-Za-z0-9+&@#/%=~_|]' + '(\[\d+\]\s*)?\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(:\d{1,5})?(/\S+)*' + '(\[\d+\)\s*)?(ssh://)?git@\S*' ) -[ -z "$items" ] && tmux display 'tmux-fzf-url: no URLs found' && exit -mapfile -t chosen < <(fzf_filter <<< "$items" | awk '{print $2}') +urls=() +for regex in "${regexes[@]}"; do + mapfile -t -O "${#urls[@]}" urls < \ + <(printf "%s\n" "$content" | grep -Po -- "$regex") +done + +[ "${#urls[@]}" -eq 0 ] && tmux display 'tmux-fzf-url: no URLs found' && exit + +mapfile -t chosen < <(fzf_filter <<< "$(printf "%s\n" "${urls[@]}")") for item in "${chosen[@]}"; do - open_url "$item" &>"/tmp/tmux-$(id -u)-fzf-url.log" + open_url "$(printf "%s" "$item" | grep -Po '^\s*(\[\d+\])?\s*\K.*$')" &>"/tmp/tmux-$(id -u)-fzf-url.log" done -- cgit v1.2.3