-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathformarks.plugin.zsh
117 lines (102 loc) · 3.15 KB
/
formarks.plugin.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
#!/usr/bin/env zsh
#===============================================================================
# Author: Wenxuan
# Email: wenxuangm@gmail.com
# Created: 2018-04-15 10:24
#===============================================================================
[ -z "${PATHMARKS_FILE}" ] && export PATHMARKS_FILE=$HOME/.pathmarks
[ ! -f $PATHMARKS_FILE ] && mkdir -p "$(dirname "$PATHMARKS_FILE")" && touch "$PATHMARKS_FILE"
wfxr::pathmarks-fzf() {
local list
(( $+commands[eza] )) && list='eza -lbhg --git' || list='ls -l'
fzf --ansi \
--height '40%' \
--preview="echo {}|sed 's#.*-> ##'| xargs $list --color=always" \
--preview-window="right:50%" \
"$@"
}
function mark() {
[[ "$#" -eq 0 ]] && wfxr::mark_usage && return 1
local mark_to_add
mark_to_add=$(echo "$*: $(pwd)")
echo "${mark_to_add}" >> "${PATHMARKS_FILE}"
echo "** The following mark has been added **"
echo "${mark_to_add}"
}
function dmarks() {
local lines
lines=$(lmarks| wfxr::pathmarks-fzf --query="$*" -m)
wfxr::pathmarks-delete "$lines"
}
# List all marks
function wfxr::lmarks() {
sed 's#: # -> #' "$PATHMARKS_FILE" | sort | nl | column -t
}
function lmarks() {
wfxr::lmarks | wfxr::pathmarks-colorize
}
function wfxr::pathmarks-colorize() {
local field='\(\S\+\s*\)'
local esc=$(printf '\033')
local N="${esc}[0m"
local R="${esc}[31m"
local G="${esc}[32m"
local Y="${esc}[33m"
local B="${esc}[34m"
local pattern="s#^${field}${field}${field}${field}#$Y\1$R\2$N\3$B\4$N#"
# Use GNU sed if possible
# BSD sed(default sed on mac) can not display color by this pattern
(( $+commands[gsed] )) && gsed "$pattern" || sed "$pattern"
}
# Prompt user to delete invalid marks
function cmarks() {
local invalid_marks
invalid_marks=$(wfxr::lmarks |
wfxr::pathmarks-invalid |
wfxr::pathmarks-colorize |
wfxr::pathmarks-fzf -0 -m --header='** The following marks are not invalid anymore **')
wfxr::pathmarks-delete "$invalid_marks"
}
# Delete selected pathmarks
function wfxr::pathmarks-delete() {
local lines
lines="$*"
if [[ -n $lines ]]; then
echo "$lines" |awk '{print $1}'| sed 's/$/d/'| paste -sd';'| xargs -I{} sed -i "{}" "$PATHMARKS_FILE"
echo "** The following marks have been deleted **"
echo "$lines"
fi
}
# Show usage for function mark
function wfxr::mark_usage() {
echo "Usage: mark <bookmark>" >&2
echo " eg: mark downloads"
}
# List invalid marks
function wfxr::pathmarks-invalid() {
local line
local directory
while read line; do
directory=$(echo "$line" |sed 's#.*-> ##')
test -d "$directory" || echo "$line"
done
}
function jump() {
local target
target=$(lmarks |
wfxr::pathmarks-colorize |
wfxr::pathmarks-fzf --query="$*" -1|
sed 's#.*-> ##')
if [[ -d "$target" ]]; then
cd "$target"
local precmd
for precmd in $precmd_functions; do
$precmd
done
zle reset-prompt
else
zle redisplay # Just redisplay if no jump to do
fi
}
zle -N jump
bindkey ${FZF_MARKS_JUMP:-'^g'} jump