-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.zsh
173 lines (137 loc) · 3.24 KB
/
functions.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# +-------------------------------------------+
# | Almost always require an external command |
# +-------------------------------------------+
IGNORES=(
"*.egg-info"
".DS_Store"
".Rproj.user"
".coverage"
".git/"
".idea/"
".ipynb_checkpoints/"
".jj/"
".mypy_cache/"
".pytest_cache/"
".ruff_cache/"
".terraform"
".venv/"
".vscode/"
"__pycache__/"
"assets/"
"coverage.xml"
"debug/"
"htmlcov/"
"node_modules/"
"renv/"
"target/"
"venv/"
)
# prepend "-E " to each element of ignores
IGNORES_FD=("${IGNORES[@]/#/-E=}")
# chain every ignore in `ignore1|ignore2` format
IGNORES_EZA="${(j:|:)IGNORES}"
CMD_PREVIEW_BAT="bat --color=always --style='plain,changes' --line-range=:500 -- {}"
CMD_PREVIEW_EZA="eza --all --group-directories-first --icons --level=2 --tree --ignore-glob=\"$IGNORES_EZA\" -- {}"
# find files and pipe in sk/fzf preview with bat
_fuzzy-file() {
requires fd sk bat
local pattern="$1"
fd --type=file --unrestricted "${IGNORES_FD[@]}" |
sk \
--preview="$CMD_PREVIEW_BAT" \
--query="$pattern"
}
# find directories and pipe in sk/fzf preview with eza
_fuzzy-dir() {
requires fd sk bat
local pattern="$1"
# `sk --cmd` does not work with zle
fd --type=directory --unrestricted "${IGNORES_FD[@]}" |
sk \
--preview="$CMD_PREVIEW_EZA" \
--query="$pattern"
}
# live grep and pipe in sk/fzf preview with bat
_live-grep() {
requires rg sk bat
local pattern="$1"
rg \
--hidden \
--color=always \
--line-number \
--no-heading \
--smart-case "${*:-}" |
sk \
--ansi \
--color "hl:-1:underline,hl+:-1:underline:reverse" \
--delimiter : \
--preview 'bat --color=always {1} --highlight-line {2} --plain' \
--query "$pattern"
}
fuzzy-file() {
requires nvim
local pattern="$1"
local file
file="$(_fuzzy-file "$pattern")"
if [[ -z "$file" ]]; then
return 1
else
nvim -- "$file"
fi
}
fuzzy-dir() {
requires nvim
local filemanager_cmd="${NVIM_FILEMANAGER_CMD:-"Neotree"}"
local pattern="$1"
local dir
dir="$(_fuzzy-dir "$pattern")"
if [[ -z "$dir" ]]; then
return 1
else
nvim -c "${filemanager_cmd} ${dir}"
fi
}
live-grep() {
requires nvim
local pattern="$1"
local file
file="$(_live-grep "$pattern" | cut -d : -f 1,2)"
if [[ -z "$file" ]]; then
return 1
else
nvim +"${file#*:}" -c "normal zz" -- "${file%:*}"
fi
}
zoxide-interactive() {
requires zoxide sk
local query="$1"
local chosen_directory
chosen_directory=$(
zoxide query \
--exclude "$PWD" \
--list |
sk \
--preview="$CMD_PREVIEW_EZA" \
--height 40% \
--preview-window=down,40% \
--query="$query"
)
cd -- "$chosen_directory" || return 1
}
# +-------+
# | Utils |
# +-------+
path() { print "${PATH//:/\\n}" }
fpath() { print "${fpath}" | tr " " "\n" }
take () { command mkdir -p "$1" && cd "$1"; }
tmp() { cd "$(mktemp -t scratch --directory)" }
cleanup() { rm -rf * }
leave() {
local here="${PWD}"
cd .. || return 1
rm -rf "$here"
}
zim-update () {
print -- "\n🐚 $fg_bold[white]Let's fish some new shells!$reset_color 🐚"
zimfw upgrade && zimfw uninstall && zimfw update
}