Skip to content

Commit

Permalink
add preview window toggle from @u1and0
Browse files Browse the repository at this point in the history
ping @u1and0

thank you for the PR -- i will surely accept it after testing

please rebase
- |     # to cleanly redo changeset
  |     git clone git@github.com:u1and0/fzf-extras.git
  |     cd fzf-extras
  |     git remote add upstream https://github.com/atweiden/fzf-extras
  |     git fetch upstream
  |     git diff upstream/master > ~/Documents/preview-window-toggle.diff
  |
  |     # wipe repo
  |     cd ..
  |     rm -rf fzf-extras
  |
  |     # re-fork atweiden/fzf-extras to your github account
  |
  |     # assuming the fresh fork is at u1and0/fzf-extras-dev
  |     # clone fresh fork
  |     git clone git@github.com:u1and0/fzf-extras-dev.git
  |     cd fzf-extras-dev
  |
  |     # apply changes to local branch
  |     git checkout -b preview-window-toggle
  |     git apply ~/Documents/preview-window-toggle.diff
  |     git commit
  |
  |     # create new pull request at atweiden/fzf-extras from
  |     # u1and0/fzf-extras-dev

if you can't rebase or don't want to rebase
- i can use this branch which includes your changes
  - but github does not list you as "contributor" that way

i have made two small changes:
- chmod -x fzf-extras.sh
  - files should be distributed as 644
- test for `bat` before `pygmentize`
  - because
    - my preference is for *bat* > *pygmentize*
      - but either way would be fine

also i corrected spelling errors in your comment

```diff
diff --git c/fzf-extras.sh w/fzf-extras.sh
index 03455e3..cb1e986 100755
--- c/fzf-extras.sh
+++ w/fzf-extras.sh
@@ -5,10 +5,10 @@
 # -----------------------------------------------------------------------------

 # fzf --preview command for file and directory
-if type pygmentize >/dev/null 2>&1; then
-    FZF_PREVIEW_CMD='head -n $FZF_PREVIEW_LINES {} | pygmentize -g'
-elif type bat >/dev/null 2>&1; then
+if type bat >/dev/null 2>&1; then
     FZF_PREVIEW_CMD='bat --color=always --plain --line-range :$FZF_PREVIEW_LINES {}'
+elif type pygmentize >/dev/null 2>&1; then
+    FZF_PREVIEW_CMD='head -n $FZF_PREVIEW_LINES {} | pygmentize -g'
 else
     FZF_PREVIEW_CMD='head -n $FZF_PREVIEW_LINES {}'
 fi
@@ -85,7 +85,7 @@ zst() {
             --bind=ctrl-x:toggle-sort \
             --header='<C-V> toggle preview <C-X> toggle sort' \
   )"
-  # check $dir exist for Ctrl-C interrapt
+  # check $dir exists for Ctrl-C interrupt
   # or change directory to $HOME (= no value cd)
   if [[ -d "$dir" ]]; then
     cd "$dir" || return
```

/* vim: set filetype=finn */
  • Loading branch information
atweiden committed Jul 18, 2020
1 parent b288090 commit f94aa01
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ bash cmdline | description

bash cmdline | description
--- | ---
`e` | Open 'frecency' files with `$VISUAL` editor
`e [FUZZY PATTERN]` | Open 'frecency' files with `$EDITOR` editor
`fe [FUZZY PATTERN]` | Open selected file with `$EDITOR`
`fo` | Equivalent to `fe`, but opens file with `$OPENER` (default: `xdg-open`) if you press <kbd>Ctrl+O</kbd>
`v` | Open selected files from `~/.viminfo` with `$EDITOR`
Expand Down
78 changes: 65 additions & 13 deletions fzf-extras.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,26 @@
# directory
# -----------------------------------------------------------------------------

# fzf --preview command for file and directory
if type bat >/dev/null 2>&1; then
FZF_PREVIEW_CMD='bat --color=always --plain --line-range :$FZF_PREVIEW_LINES {}'
elif type pygmentize >/dev/null 2>&1; then
FZF_PREVIEW_CMD='head -n $FZF_PREVIEW_LINES {} | pygmentize -g'
else
FZF_PREVIEW_CMD='head -n $FZF_PREVIEW_LINES {}'
fi

# zdd - cd to selected directory
zdd() {
local dir
dir="$(
find "${1:-.}" -path '*/\.*' -prune -o -type d -print 2> /dev/null \
| fzf +m
| fzf +m \
--preview='tree -C {} | head -n $FZF_PREVIEW_LINES' \
--preview-window='right:hidden:wrap' \
--bind=ctrl-v:toggle-preview \
--bind=ctrl-x:toggle-sort \
--header='<C-V> toggle preview <C-X> toggle sort' \
)" || return
cd "$dir" || return
}
Expand All @@ -19,7 +33,12 @@ zda() {
local dir
dir="$(
find "${1:-.}" -type d 2> /dev/null \
| fzf +m
| fzf +m \
--preview='tree -C {} | head -n $FZF_PREVIEW_LINES' \
--preview-window='right:hidden:wrap' \
--bind=ctrl-v:toggle-preview \
--bind=ctrl-x:toggle-sort \
--header='<C-V> toggle preview <C-X> toggle sort' \
)" || return
cd "$dir" || return
}
Expand All @@ -40,7 +59,12 @@ zdr() {

parent_dir="$(
get_parent_dirs "$(realpath "${1:-$PWD}")" \
| fzf +m
| fzf +m \
--preview 'tree -C {} | head -n $FZF_PREVIEW_LINES' \
--preview-window='right:hidden:wrap' \
--bind=ctrl-v:toggle-preview \
--bind=ctrl-x:toggle-sort \
--header='<C-V> toggle preview <C-X> toggle sort' \
)" || return

cd "$parent_dir" || return
Expand All @@ -54,9 +78,15 @@ zst() {
| sed 's#\s#\n#g' \
| uniq \
| sed "s#^~#$HOME#" \
| fzf +s +m -1 -q "$*"
| fzf +s +m -1 -q "$*" \
--preview='tree -C {} | head -n $FZF_PREVIEW_LINES' \
--preview-window='right:hidden:wrap' \
--bind=ctrl-v:toggle-preview \
--bind=ctrl-x:toggle-sort \
--header='<C-V> toggle preview <C-X> toggle sort' \
)"
# $dirの存在を確かめないとCtrl-Cしたとき$HOMEにcdしてしまう
# check $dir exists for Ctrl-C interrupt
# or change directory to $HOME (= no value cd)
if [[ -d "$dir" ]]; then
cd "$dir" || return
fi
Expand All @@ -65,7 +95,13 @@ zst() {
# zdf - cd into the directory of the selected file
zdf() {
local file
file="$(fzf +m -q "$*")"
file="$(fzf +m -q "$*" \
--preview="${FZF_PREVIEW_CMD}" \
--preview-window='right:hidden:wrap' \
--bind=ctrl-v:toggle-preview \
--bind=ctrl-x:toggle-sort \
--header='<C-V> toggle preview <C-X> toggle sort' \
)"
cd "$(dirname "$file")" || return
}

Expand All @@ -84,7 +120,11 @@ zz() {
--tiebreak=index \
--bind=ctrl-x:toggle-sort \
--query "$*" \
| grep -o '/.*'
--preview='tree -C {} | head -n $FZF_PREVIEW_LINES' \
--preview-window='right:hidden:wrap' \
--bind=ctrl-v:toggle-preview \
--bind=ctrl-x:toggle-sort \
--header='<C-V> toggle preview <C-X> toggle sort' \
)" || return

cd "$dir" || return
Expand Down Expand Up @@ -148,9 +188,11 @@ EOF

# e - open 'frecency' files in $VISUAL editor
e() {
local files
local IFS=$'\n'
local files=()

files="$(
files=(
"$(
fasd -fl \
| fzf \
--tac \
Expand All @@ -160,10 +202,15 @@ e() {
--tiebreak=index \
--bind=ctrl-x:toggle-sort \
--query "$*" \
| grep -o "/.*"
)" || echo 'No file selected'; return
--preview="${FZF_PREVIEW_CMD}" \
--preview-window='right:hidden:wrap' \
--bind=ctrl-v:toggle-preview \
--bind=ctrl-x:toggle-sort \
--header='<C-V> toggle preview <C-X> toggle sort' \
)"
) || return

"${VISUAL:-vim}" "$files"
"${EDITOR:-vim}" "${files[@]}"
}

# fe [FUZZY PATTERN] - Open the selected file with the default editor
Expand All @@ -177,7 +224,12 @@ fe() {
--query="$1" \
--multi \
--select-1 \
--exit-0
--exit-0 \
--preview="${FZF_PREVIEW_CMD}" \
--preview-window='right:hidden:wrap' \
--bind=ctrl-v:toggle-preview \
--bind=ctrl-x:toggle-sort \
--header='<C-V> toggle preview <C-X> toggle sort'
)"
) || return
"${EDITOR:-vim}" "${files[@]}"
Expand Down

0 comments on commit f94aa01

Please sign in to comment.