From 947b45d4141f02e02155b73725b93acfb493c7c0 Mon Sep 17 00:00:00 2001
From: Sergey Matveev <stargrave@stargrave.org>
Date: Mon, 21 Feb 2022 13:07:05 +0300
Subject: [PATCH] Refactored tmux fzf menus

---
 tmux/.tmux.conf                               | 16 +++++-----
 .../tmux-common.sh => .tmux/bin/common.sh}    |  0
 tmux/.tmux/bin/fzf.zsh                        | 13 +++++++++
 .../bin/menu-calc.sh}                         |  0
 .../bin/menu-dict.sh}                         |  0
 .../bin/menu-music.sh}                        |  0
 .../bin/menu-pass-session.sh}                 |  0
 .../bin/menu-pass.sh}                         |  0
 tmux/.tmux/functions.zsh/tmux-fzf-find        |  6 ++++
 .../.tmux/functions.zsh/tmux-fzf-git-branches |  2 ++
 tmux/.tmux/functions.zsh/tmux-fzf-git-commits |  2 ++
 tmux/.tmux/functions.zsh/tmux-fzf-git-files   |  3 ++
 tmux/bin/tmux-fzf.zsh                         | 29 -------------------
 x/bin/start-im                                |  2 +-
 14 files changed, 35 insertions(+), 38 deletions(-)
 rename tmux/{bin/tmux-common.sh => .tmux/bin/common.sh} (100%)
 create mode 100755 tmux/.tmux/bin/fzf.zsh
 rename tmux/{bin/tmux-menu-calc.sh => .tmux/bin/menu-calc.sh} (100%)
 rename tmux/{bin/tmux-menu-dict.sh => .tmux/bin/menu-dict.sh} (100%)
 rename tmux/{bin/tmux-menu-music.sh => .tmux/bin/menu-music.sh} (100%)
 rename tmux/{bin/tmux-menu-pass-session.sh => .tmux/bin/menu-pass-session.sh} (100%)
 rename tmux/{bin/tmux-menu-pass.sh => .tmux/bin/menu-pass.sh} (100%)
 create mode 100644 tmux/.tmux/functions.zsh/tmux-fzf-find
 create mode 100644 tmux/.tmux/functions.zsh/tmux-fzf-git-branches
 create mode 100644 tmux/.tmux/functions.zsh/tmux-fzf-git-commits
 create mode 100644 tmux/.tmux/functions.zsh/tmux-fzf-git-files
 delete mode 100755 tmux/bin/tmux-fzf.zsh

diff --git a/tmux/.tmux.conf b/tmux/.tmux.conf
index 3623ea3..1907cc7 100644
--- a/tmux/.tmux.conf
+++ b/tmux/.tmux.conf
@@ -56,15 +56,15 @@ bind-key Y {
 }
 
 bind-key t display-menu \
-    music t "display-popup -E -w 100% '~/bin/tmux-menu-music.sh'" \
-    pass p "display-popup -E '~/bin/tmux-menu-pass-session.sh'" \
-    dict d "display-popup -E -h 100% '~/bin/tmux-menu-dict.sh'" \
-    calc c "display-popup -E '~/bin/tmux-menu-calc.sh'" \
+    music t "display-popup -E -w 100% '~/.tmux/bin/menu-music.sh'" \
+    pass p "display-popup -E '~/.tmux/bin/menu-pass-session.sh'" \
+    dict d "display-popup -E -h 100% '~/.tmux/bin/menu-dict.sh'" \
+    calc c "display-popup -E '~/.tmux/bin/menu-calc.sh'" \
     cal l "display-popup 'cal -3N'" \
     top o "display-popup -E -h 100% 'top -s 1'"
 
 bind-key o display-menu \
-    find o "display-popup -E -w 100% \"~/bin/tmux-fzf.zsh find '#{pane_current_path}'\"" \
-    git-files g "display-popup -E -w 100% \"~/bin/tmux-fzf.zsh git-files '#{pane_current_path}'\"" \
-    git-branches b "display-popup -E -w 100% \"~/bin/tmux-fzf.zsh git-branches '#{pane_current_path}'\"" \
-    git-commits c "display-popup -E -w 100% \"~/bin/tmux-fzf.zsh git-commits '#{pane_current_path}'\""
+    find o "display-popup -E -w 100% \"~/.tmux/bin/fzf.zsh find '#{pane_current_path}'\"" \
+    git-files g "display-popup -E -w 100% \"~/.tmux/bin/fzf.zsh git-files '#{pane_current_path}'\"" \
+    git-branches b "display-popup -E -w 100% \"~/.tmux/bin/fzf.zsh git-branches '#{pane_current_path}'\"" \
+    git-commits c "display-popup -E -w 100% \"~/.tmux/bin/fzf.zsh git-commits '#{pane_current_path}'\""
diff --git a/tmux/bin/tmux-common.sh b/tmux/.tmux/bin/common.sh
similarity index 100%
rename from tmux/bin/tmux-common.sh
rename to tmux/.tmux/bin/common.sh
diff --git a/tmux/.tmux/bin/fzf.zsh b/tmux/.tmux/bin/fzf.zsh
new file mode 100755
index 0000000..8872926
--- /dev/null
+++ b/tmux/.tmux/bin/fzf.zsh
@@ -0,0 +1,13 @@
+#!/usr/bin/env zsh
+
+cd $2
+set -e
+fpath=(~/.tmux/functions.zsh $fpath)
+autoload tmux-fzf-$1
+tmp=`mktemp`
+trap "rm -f $tmp" HUP PIPE INT QUIT TERM EXIT
+tmux-fzf-$1 > $tmp || { echo unknown command ; sleep 1 ; exit }
+[[ -s $tmp ]] || exit
+tmux set-buffer "`perl -npe 's/\n/ /g' $tmp`"
+tmux paste-buffer
+tmux delete-buffer
diff --git a/tmux/bin/tmux-menu-calc.sh b/tmux/.tmux/bin/menu-calc.sh
similarity index 100%
rename from tmux/bin/tmux-menu-calc.sh
rename to tmux/.tmux/bin/menu-calc.sh
diff --git a/tmux/bin/tmux-menu-dict.sh b/tmux/.tmux/bin/menu-dict.sh
similarity index 100%
rename from tmux/bin/tmux-menu-dict.sh
rename to tmux/.tmux/bin/menu-dict.sh
diff --git a/tmux/bin/tmux-menu-music.sh b/tmux/.tmux/bin/menu-music.sh
similarity index 100%
rename from tmux/bin/tmux-menu-music.sh
rename to tmux/.tmux/bin/menu-music.sh
diff --git a/tmux/bin/tmux-menu-pass-session.sh b/tmux/.tmux/bin/menu-pass-session.sh
similarity index 100%
rename from tmux/bin/tmux-menu-pass-session.sh
rename to tmux/.tmux/bin/menu-pass-session.sh
diff --git a/tmux/bin/tmux-menu-pass.sh b/tmux/.tmux/bin/menu-pass.sh
similarity index 100%
rename from tmux/bin/tmux-menu-pass.sh
rename to tmux/.tmux/bin/menu-pass.sh
diff --git a/tmux/.tmux/functions.zsh/tmux-fzf-find b/tmux/.tmux/functions.zsh/tmux-fzf-find
new file mode 100644
index 0000000..c114afd
--- /dev/null
+++ b/tmux/.tmux/functions.zsh/tmux-fzf-find
@@ -0,0 +1,6 @@
+bfs -L . -mindepth 1 \
+    -path "*/.git" -prune -o \
+    -path "*/.redo" -prune -o \
+    \( -type f -o -type d -o -type l \) -print |
+cut -c3- | fzf -m --preview="less -N -S {}" |
+while read fn ; do print ${(q)fn} ; done
diff --git a/tmux/.tmux/functions.zsh/tmux-fzf-git-branches b/tmux/.tmux/functions.zsh/tmux-fzf-git-branches
new file mode 100644
index 0000000..91aa633
--- /dev/null
+++ b/tmux/.tmux/functions.zsh/tmux-fzf-git-branches
@@ -0,0 +1,2 @@
+{ git branch ; git branch --remote } | sed "s/ //g" |
+fzf --preview "git log --oneline --graph --decorate=short --color=always {}"
diff --git a/tmux/.tmux/functions.zsh/tmux-fzf-git-commits b/tmux/.tmux/functions.zsh/tmux-fzf-git-commits
new file mode 100644
index 0000000..47198cf
--- /dev/null
+++ b/tmux/.tmux/functions.zsh/tmux-fzf-git-commits
@@ -0,0 +1,2 @@
+git --no-pager log --oneline -n 100 | perl -ne 'print "@~$n $_"; $n++' |
+fzf --reverse | cut -w -f1
diff --git a/tmux/.tmux/functions.zsh/tmux-fzf-git-files b/tmux/.tmux/functions.zsh/tmux-fzf-git-files
new file mode 100644
index 0000000..35180f8
--- /dev/null
+++ b/tmux/.tmux/functions.zsh/tmux-fzf-git-files
@@ -0,0 +1,3 @@
+git status --short |
+fzf -m --delimiter " " --preview "git diff --color=always {-1}" |
+perl -npe 's/^\s*\S+\s+//'
diff --git a/tmux/bin/tmux-fzf.zsh b/tmux/bin/tmux-fzf.zsh
deleted file mode 100755
index 2eff748..0000000
--- a/tmux/bin/tmux-fzf.zsh
+++ /dev/null
@@ -1,29 +0,0 @@
-#!/usr/bin/env zsh
-
-cd $2
-set -e
-tmp=`mktemp`
-trap "rm -f $tmp" HUP PIPE INT QUIT TERM EXIT
-
-case $1 in
-(find)
-    bfs -L . -mindepth 1 \
-        -path "*/.git" -prune -o \
-        -path "*/.redo" -prune -o \
-        \( -type f -o -type d -o -type l \) -print |
-    cut -c3- | fzf -m --preview="less -N -S {}" |
-    while read fn ; do print ${(q)fn} ; done > $tmp
-    ;;
-(git-files) git status --short | fzf -m | perl -npe 's/^\s*\S+\s+//' > $tmp ;;
-(git-branches) { git branch ; git branch --remote } | fzf > $tmp ;;
-(git-commits)
-    git --no-pager log --oneline -n 20 | perl -ne "print \"@~\$n \$_\"; \$n++" |
-    fzf --reverse | cut -w -f1 > $tmp
-    ;;
-(*) echo unknown command ; sleep 1 ; exit ;;
-esac
-
-[ -s $tmp ] || exit
-tmux set-buffer "`perl -npe 's/\n/ /g' $tmp`"
-tmux paste-buffer
-tmux delete-buffer
diff --git a/x/bin/start-im b/x/bin/start-im
index 2e3373f..9fe966f 100755
--- a/x/bin/start-im
+++ b/x/bin/start-im
@@ -1,6 +1,6 @@
 #!/bin/zsh
 
-. ~/bin/tmux-common.sh
+. ~/.tmux/bin/common.sh
 
 tmux has-session -t im && exit
 tmux new-session -d -s im
-- 
2.50.0