Skip to content

Commit 3176056

Browse files
authored
Merge pull request #156 from richlamdev/refactor-fzf
start refactor of fzf
2 parents 1fe5282 + e14142a commit 3176056

File tree

5 files changed

+179
-26
lines changed

5 files changed

+179
-26
lines changed

main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- dev-tools
1919
# - docker-cli-only
2020
- docker-desktop-dependency
21+
- fzf
2122
- gh_cli
2223
- git
2324
- hashicorp

roles/env/files/.bashrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ if ! shopt -oq posix; then
115115
fi
116116
fi
117117

118+
# BEGIN ANSIBLE MANAGED BLOCK: source .bashrc.d scripts
118119
# customize prompt
119120
for file in /home/$USER/.bashrc.d/*.sh; do
120121
[ -r "$file" ] && source "$file"
121122
done
123+
# END ANSIBLE MANAGED BLOCK: source .bashrc.d scripts

roles/env/tasks/main.yml

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,35 @@
11
---
2-
- name: install custom_bash.sh / git_bash_ps1.sh to home .bashrc.d directory
2+
- name: ensure .bashrc.d directory exists in user's home
3+
file:
4+
path: "{{ home_dir }}/.bashrc.d"
5+
state: directory
6+
owner: "{{ local_user }}"
7+
group: "{{ local_user_primary_group }}"
8+
mode: "0750"
9+
10+
- name: install custom_bash.sh / git_bash_ps1.sh to {{ home_dir}}/.bashrc.d/
311
copy:
412
src: "{{ item }}"
5-
dest: "/{{ home_dir }}/.bashrc.d/"
13+
dest: "{{ home_dir }}/.bashrc.d/"
614
owner: "{{ local_user }}"
715
group: "{{ local_user_primary_group }}"
816
mode: "0640"
917
loop:
1018
- custom_bash.sh
11-
- fzf_config.sh
1219
- git_bash_ps1.sh
1320
- pyenv.sh
1421

15-
- name: deploy .bashrc / .dircolors to home directory
22+
- name: ensure .bashrc sources from {{ home_dir }}/.bashrc.d / .dircolors to home directory
1623
copy:
1724
src: "{{ item }}"
18-
dest: "/{{ home_dir }}/"
25+
dest: "{{ home_dir }}/"
1926
owner: "{{ local_user }}"
2027
group: "{{ local_user_primary_group }}"
2128
mode: "0640"
2229
loop:
2330
- .bashrc
2431
- .dircolors
2532

26-
- name: install fzf
27-
git:
28-
repo: "https://github.com/junegunn/fzf.git"
29-
dest: "{{ home_dir }}/.fzf"
30-
depth: "1"
31-
version: "master"
32-
33-
- name: check if fzf install script has been executed
34-
shell: "/{{ home_dir }}/.fzf/bin/fzf --version"
35-
register: fzf_installed
36-
failed_when: false
37-
changed_when: false
38-
39-
#- name: fzf_installed
40-
#debug:
41-
#msg: "{{ fzf_installed }}"
42-
43-
- name: run fzf install
44-
shell: "/{{ home_dir }}/.fzf/install --key-bindings --completion --no-update-rc"
45-
when: fzf_installed.rc != 0
46-
4733
- name: copy .gitconfig
4834
copy:
4935
src: gitconfig

roles/fzf/files/fzf_config.sh

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# fzf configuration
2+
3+
#export FZF_DEFAULT_COMMAND='rg --files --ignore-vcs --smart-case --hidden'
4+
export FZF_DEFAULT_COMMAND='fdfind --type f --strip-cwd-prefix --hidden --follow --exclude .git'
5+
export FZF_DEFAULT_OPTS="
6+
--height 80%
7+
--layout reverse
8+
--multi
9+
--border
10+
--highlight-line --color gutter:-1,selected-bg:238,selected-fg:146,current-fg:189"
11+
12+
# Preview file content using bat (https://github.com/sharkdp/bat)
13+
export FZF_CTRL_T_OPTS="
14+
--walker-skip .git,node_modules,target
15+
--preview 'batcat -n --color=always {}'
16+
--bind 'ctrl-/:change-preview-window(down|hidden|)'"
17+
18+
# CTRL-P to toggle preview window to view full command
19+
# CTRL-Y to copy the command into clipboard using pbcopy
20+
export FZF_CTRL_R_OPTS="
21+
--preview 'echo {}' --preview-window up:3:hidden:wrap
22+
--bind 'ctrl-p:toggle-preview'
23+
--bind 'ctrl-y:execute-silent(echo -n {2..} | xclip -selection clipboard)+abort'
24+
--color header:italic
25+
--header '
26+
Press CTRL-P: toggle preview
27+
Press CTRL-Y to copy command into clipboard
28+
'"
29+
30+
# Print tree structure in the preview window
31+
export FZF_ALT_C_OPTS="
32+
--walker-skip .git,node_modules,target
33+
--preview 'tree -C {} | head -200'"
34+
35+
export EDITOR=vim
36+
37+
# original function obtained from:
38+
# https://thevaluable.dev/practical-guide-fzf-example/
39+
se() {
40+
local search_folder="${1:-$HOME}"
41+
42+
# Common folders to exclude from traversal
43+
local exclude_paths=(
44+
"$HOME/.cache"
45+
"$HOME/.config"
46+
"$HOME/.local"
47+
"$HOME/.codeium"
48+
"$HOME/.git"
49+
"$HOME/.vscode"
50+
"$HOME/.npm"
51+
"$HOME/.cargo"
52+
"$HOME/.rustup"
53+
"$HOME/.mozilla"
54+
"$HOME/.snap"
55+
)
56+
57+
# Build prune expression for find
58+
local prune_expr=""
59+
for path in "${exclude_paths[@]}"; do
60+
prune_expr+=" -path \"$path\" -o"
61+
done
62+
# Remove trailing -o
63+
prune_expr="${prune_expr::-3}"
64+
65+
selection=$(
66+
eval "find \"$search_folder\" \( $prune_expr \) -prune -o -type d -print" | fzf \
67+
--preview='command -v tree >/dev/null && tree -C {} || ls -la {}' \
68+
--preview-window='50%' \
69+
--prompt='Dirs > ' \
70+
--bind='del:execute(rm -ri {+})' \
71+
--bind='ctrl-p:toggle-preview' \
72+
--bind='ctrl-d:change-prompt(Dirs > )' \
73+
--bind="ctrl-d:+reload(eval \"find '$search_folder' \\( ${prune_expr//\"/\\\"} \\) -prune -o -type d -print\")" \
74+
--bind='ctrl-d:+change-preview(command -v tree >/dev/null && tree -C {} || ls -la {})' \
75+
--bind='ctrl-d:+refresh-preview' \
76+
--bind='ctrl-f:change-prompt(Files > )' \
77+
--bind="ctrl-f:+reload(eval \"find '$search_folder' \\( ${prune_expr//\"/\\\"} \\) -prune -o -type f -print\")" \
78+
--bind='ctrl-f:+change-preview(command -v batcat >/dev/null && batcat --color=always {} || cat {})' \
79+
--bind='ctrl-f:+refresh-preview' \
80+
--bind='ctrl-a:select-all' \
81+
--bind='ctrl-x:deselect-all' \
82+
--color header:italic \
83+
--header '
84+
CTRL-D to display directories
85+
CTRL-F to display files
86+
CTRL-A/CTRL-X to select/deselect all
87+
ENTER to navigate | DEL to delete
88+
CTRL-P to toggle preview
89+
'
90+
)
91+
92+
if [ -d "$selection" ]; then
93+
cd "$selection" || return
94+
elif [ -f "$selection" ]; then
95+
cd "$(dirname "$selection")" || return
96+
elif [ -n "$selection" ]; then
97+
${EDITOR:-vim} "$selection"
98+
fi
99+
}
100+
101+
[ -f ~/.fzf.bash ] && source ~/.fzf.bash

roles/fzf/tasks/main.yml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
- block:
3+
- name: ensure .bashrc.d directory exists in user's home
4+
file:
5+
path: "{{ home_dir }}/.bashrc.d"
6+
state: directory
7+
owner: "{{ local_user }}"
8+
group: "{{ local_user_primary_group }}"
9+
mode: "0750"
10+
11+
- name: ensure .bashrc sources scripts from .bashrc.d
12+
ansible.builtin.blockinfile:
13+
path: "/{{ home_dir }}/.bashrc"
14+
marker: "# {mark} ANSIBLE MANAGED BLOCK: source .bashrc.d scripts"
15+
block: |
16+
# customize prompt
17+
for file in /home/$USER/.bashrc.d/*.sh; do
18+
[ -r "$file" ] && source "$file"
19+
done
20+
owner: "{{ local_user }}"
21+
group: "{{ local_user_primary_group }}"
22+
mode: "0640"
23+
24+
- name: install fzf_config.sh to {{ home_dir}}/.bashrc.d/
25+
copy:
26+
src: "{{ item }}"
27+
dest: "{{ home_dir }}/.bashrc.d/"
28+
owner: "{{ local_user }}"
29+
group: "{{ local_user_primary_group }}"
30+
mode: "0640"
31+
loop:
32+
- fzf_config.sh
33+
34+
- name: install fzf
35+
git:
36+
repo: "https://github.com/junegunn/fzf.git"
37+
dest: "{{ home_dir }}/.fzf"
38+
depth: "1"
39+
version: "master"
40+
41+
- name: check if fzf install script has been executed
42+
shell: "{{ home_dir }}/.fzf/bin/fzf --version"
43+
register: fzf_installed
44+
failed_when: false
45+
changed_when: false
46+
47+
- name: display fzf version if installed
48+
debug:
49+
msg: "fzf version installed: {{ fzf_installed.stdout }}"
50+
when: fzf_installed.rc == 0
51+
52+
- name: run fzf install
53+
shell: "{{ home_dir }}/.fzf/install --key-bindings --completion --no-update-rc"
54+
when: fzf_installed.rc != 0
55+
56+
- name: display fzf uninstallation instructions
57+
debug:
58+
msg: |
59+
=== Uninstallation Instructions ===
60+
To uninstall fzf:
61+
rm -rf {{ home_dir }}/.fzf
62+
tags: ['fzf', 'upgrade']
63+
become: false

0 commit comments

Comments
 (0)