From 0b03acebd8b8c36ecdfbf9256bd944d08ea00e4d Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Thu, 2 Jan 2025 22:22:59 +0200 Subject: [PATCH] feat(config): tmux plugin tmux-fzf-url --- config/tmux/plugins/tmux-fzf-url/LICENSE.txt | 1 + config/tmux/plugins/tmux-fzf-url/fzf-url.sh | 64 +++++++++++++++++++ config/tmux/plugins/tmux-fzf-url/fzf-url.tmux | 23 +++++++ config/tmux/tmux.conf | 6 +- 4 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 config/tmux/plugins/tmux-fzf-url/LICENSE.txt create mode 100755 config/tmux/plugins/tmux-fzf-url/fzf-url.sh create mode 100755 config/tmux/plugins/tmux-fzf-url/fzf-url.tmux diff --git a/config/tmux/plugins/tmux-fzf-url/LICENSE.txt b/config/tmux/plugins/tmux-fzf-url/LICENSE.txt new file mode 100644 index 0000000..19d6c5c --- /dev/null +++ b/config/tmux/plugins/tmux-fzf-url/LICENSE.txt @@ -0,0 +1 @@ +https://wfxr.mit-license.org/2018 diff --git a/config/tmux/plugins/tmux-fzf-url/fzf-url.sh b/config/tmux/plugins/tmux-fzf-url/fzf-url.sh new file mode 100755 index 0000000..cc60e16 --- /dev/null +++ b/config/tmux/plugins/tmux-fzf-url/fzf-url.sh @@ -0,0 +1,64 @@ +#!/usr/bin/env bash +#=============================================================================== +# Author: Wenxuan +# Email: wenxuangm@gmail.com +# Created: 2018-04-06 12:12 +#=============================================================================== +get_fzf_options() +{ + local fzf_options + local fzf_default_options='-w 100% -h 50% --multi -0 --no-preview' + fzf_options="$(tmux show -gqv '@fzf-url-fzf-options')" + [ -n "$fzf_options" ] && echo "$fzf_options" || echo "$fzf_default_options" +} + +fzf_filter() +{ + eval "fzf-tmux $(get_fzf_options)" +} + +custom_open=$3 +open_url() +{ + if [[ -n $custom_open ]]; then + $custom_open "$@" + elif hash xdg-open &> /dev/null; then + nohup xdg-open "$@" + elif hash open &> /dev/null; then + nohup open "$@" + elif [[ -n $BROWSER ]]; then + nohup "$BROWSER" "$@" + fi +} + +limit='screen' +[[ $# -ge 2 ]] && limit=$2 + +if [[ $limit == 'screen' ]]; then + content="$(tmux capture-pane -J -p -e | sed -r 's/\x1B\[[0-9;]*[mK]//g'))" +else + content="$(tmux capture-pane -J -p -e -S -"$limit" | sed -r 's/\x1B\[[0-9;]*[mK]//g'))" +fi + +urls=$(echo "$content" | grep -oE '(https?|ftp|file):/?//[-A-Za-z0-9+&@#/%?=~_|!:,.;]*[-A-Za-z0-9+&@#/%=~_|]') +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/') +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/') +gh=$(echo "$content" | grep -oE "['\"]([_A-Za-z0-9-]*/[_.A-Za-z0-9-]*)['\"]" | sed "s/['\"]//g" | sed 's#.#https://github.com/&#') + +if [[ $# -ge 1 && $1 != '' ]]; then + extras=$(echo "$content" | eval "$1") +fi + +items=$( + printf '%s\n' "${urls[@]}" "${wwws[@]}" "${gh[@]}" "${ips[@]}" "${gits[@]}" "${extras[@]}" \ + | grep -v '^$' \ + | sort -u \ + | nl -w3 -s ' ' +) +[ -z "$items" ] && tmux display 'tmux-fzf-url: no URLs found' && exit + +fzf_filter <<< "$items" | awk '{print $2}' \ + | while read -r chosen; do + open_url "$chosen" &> "/tmp/tmux-$(id -u)-fzf-url.log" + done diff --git a/config/tmux/plugins/tmux-fzf-url/fzf-url.tmux b/config/tmux/plugins/tmux-fzf-url/fzf-url.tmux new file mode 100755 index 0000000..06063e2 --- /dev/null +++ b/config/tmux/plugins/tmux-fzf-url/fzf-url.tmux @@ -0,0 +1,23 @@ +#!/usr/bin/env bash +#=============================================================================== +# Author: Wenxuan +# Email: wenxuangm@gmail.com +# Created: 2018-04-06 09:30 +#=============================================================================== +SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) + +# $1: option +# $2: default value +tmux_get() { + local value + value="$(tmux show -gqv "$1")" + [ -n "$value" ] && echo "$value" || echo "$2" +} + +key="$(tmux_get '@fzf-url-bind' 'u')" +history_limit="$(tmux_get '@fzf-url-history-limit' 'screen')" +extra_filter="$(tmux_get '@fzf-url-extra-filter' '')" +custom_open="$(tmux_get '@fzf-url-open' '')" +echo "$extra_filter" >/tmp/filter + +tmux bind-key "$key" run -b "$SCRIPT_DIR/fzf-url.sh '$extra_filter' $history_limit '$custom_open'" diff --git a/config/tmux/tmux.conf b/config/tmux/tmux.conf index 0608c18..7cd6454 100644 --- a/config/tmux/tmux.conf +++ b/config/tmux/tmux.conf @@ -159,6 +159,10 @@ set -g @mode_indicator_copy_mode_style 'bg=default,fg=yellow' set -g @mode_indicator_empty_mode_style 'bg=default,fg=#7aa2f7' set -g @mode_indicator_sync_mode_style 'bg=default,fg=red' +# https://github.com/wfxr/tmux-fzf-url +set -g @fzf-url-bind 'u' +set -g @fzf-url-history-limit '2000' + # ╭──────────────────────────────────────────────────────────╮ # │ Plugins │ # ╰──────────────────────────────────────────────────────────╯ @@ -172,7 +176,7 @@ run-shell ~/.dotfiles/config/tmux/plugins/tmux-suspend/suspend.tmux run-shell ~/.dotfiles/config/tmux/plugins/tmux-yank/yank.tmux run-shell ~/.dotfiles/config/tmux/plugins/tmux-current-pane-hostname/current_pane_hostname.tmux run-shell ~/.dotfiles/config/tmux/plugins/tmux-dark-notify/main.tmux +run-shell ~/.dotfiles/config/tmux/plugins/tmux-fzf-url/fzf-url.tmux if-shell "test -e $HOME/.local/state/tmux/tmux-dark-notify-theme.conf" \ "source-file $HOME/.local/state/tmux/tmux-dark-notify-theme.conf" -