aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWenxuan Zhang <wenxuangm@gmail.com>2020-09-07 17:24:18 +0800
committerWenxuan Zhang <wenxuangm@gmail.com>2020-09-07 17:38:51 +0800
commit5b202610ae9dd788a4c07e18c07a2634854401cd (patch)
treec41c15e669a411b5a7c2d620c140191654502e42
parent64da84f1664494681348785f125ce400af4f593a (diff)
downloadfork-tmux-fzf-url-5b202610ae9dd788a4c07e18c07a2634854401cd.tar.gz
fork-tmux-fzf-url-5b202610ae9dd788a4c07e18c07a2634854401cd.tar.bz2
fork-tmux-fzf-url-5b202610ae9dd788a4c07e18c07a2634854401cd.zip
refactor: lint
-rwxr-xr-xfzf-url.sh54
1 files changed, 28 insertions, 26 deletions
diff --git a/fzf-url.sh b/fzf-url.sh
index 70b2d39..d3fbf77 100755
--- a/fzf-url.sh
+++ b/fzf-url.sh
@@ -5,38 +5,40 @@
# Created: 2018-04-06 12:12
#===============================================================================
-fzf_cmd() {
- fzf-tmux -d 35% --multi --exit-0 --cycle --reverse --bind='ctrl-r:toggle-all' --bind='ctrl-s:toggle-sort' --no-preview
+fzf_filter() {
+ fzf-tmux -d 35% -m -0 --no-preview --no-border
}
-if hash xdg-open &>/dev/null; then
- open_cmd='nohup xdg-open'
-elif hash open &>/dev/null; then
- open_cmd='open'
-elif [[ -n $BROWSER ]]; then
- open_cmd="$BROWSER"
-fi
+open_url() {
+ if hash xdg-open &>/dev/null; then
+ nohup xdg-open "$@"
+ elif hash open &>/dev/null; then
+ nohup open "$@"
+ elif [[ -n $BROWSER ]]; then
+ nohup "$BROWSER" "$@"
+ fi
+}
content="$(tmux capture-pane -J -p)"
-urls=($(echo "$content" |grep -oE '(https?|ftp|file):/?//[-A-Za-z0-9+&@#/%?=~_|!:,.;]*[-A-Za-z0-9+&@#/%=~_|]'))
-wwws=($(echo "$content" |grep -oE 'www\.[a-zA-Z](-?[a-zA-Z0-9])+\.[a-zA-Z]{2,}(/\S+)*' |sed 's/^\(.*\)$/http:\/\/\1/'))
-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/'))
-gits=($(echo "$content" |grep -oE '(ssh://)?git@\S*' | sed 's/:/\//g' | sed 's/^\(ssh\/\/\/\)\{0,1\}git@\(.*\)$/https:\/\/\2/'))
+
+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 'www\.[a-zA-Z](-?[a-zA-Z0-9])+\.[a-zA-Z]{2,}(/\S+)*' |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/')
if [[ $# -ge 1 && "$1" != '' ]]; then
- extras=($(echo "$content" |eval "$1"))
+ mapfile -t extras < <(echo "$content" |eval "$1")
fi
-merge() {
- for item in "$@" ; do
- echo "$item"
- done
-}
-
-merge "${urls[@]}" "${wwws[@]}" "${ips[@]}" "${gits[@]}" "${extras[@]}"|
+items=$(printf '%s\n' "${urls[@]}" "${wwws[@]}" "${ips[@]}" "${gits[@]}" "${extras[@]}" |
+ grep -v '^$' |
sort -u |
- nl -w3 -s ' ' |
- fzf_cmd |
- awk '{print $2}'|
- xargs -n1 -I {} "$open_cmd" {} &>/dev/null ||
- true
+ nl -w3 -s ' '
+)
+[ -z "$items" ] && exit
+
+mapfile -t chosen < <(fzf_filter <<< "$items" | awk '{print $2}')
+
+for item in "${chosen[@]}"; do
+ open_url "$item" &>"/tmp/tmux-$(id -u)-fzf-url.log"
+done