-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path.functions.zsh
More file actions
227 lines (190 loc) · 5.35 KB
/
.functions.zsh
File metadata and controls
227 lines (190 loc) · 5.35 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/bin/zsh
# Pastebin
PB_HOST="https://fars.ee"
pb() {
emulate -L zsh
case $1 in
--rec)
asciinema rec /tmp/$$.json
local url=$(pb_core < /tmp/$$.json)
print "${${url%/json}/.ee/.ee/t}"
;;
--tmux)
tmux capture-pane -J -p | pb_core
;;
--delete)
if [[ ! -f ~/.cache/pb/$2 ]]; then
print "$2 doesn't exists!"
return 1
fi
local -A info=(${(@s/: /)${${(f)"$(<~/.cache/pb/$2)"}//$'\n'/: }})
local uuid=$info[uuid]
curl -X DELETE "$PB_HOST/$uuid"
;;
*)
pb_core ${*:1}
;;
esac
}
pb_core() {
local file==(<<<'')
if (( $# == 0 )); then
cat > $file
else
file=$1
fi
local res=${(f)"$(curl -F "c=@-" "$PB_HOST" < $file)"}
local -A info=(${(s/: /)${res//$'\n'/: }})
local url=$info[url]
[[ -d ~/.cache/pb ]] || mkdir ~/.cache/pb
print $res > ~/.cache/pb/$info[short]
local mime=$(file --mime-type -b $file)
case $mime in
image/*)
local type=${${file:e}:-${mime#*/}}
print "$url.$type" ;;
text/x-*)
local lang=${${file:e}:-${mime#*-}}
print "$url/$lang" ;;
application/json)
print "$url/json" ;;
*) print "$url${(M)${file:t}%.*}" ;;
esac
(( $# == 0 )) && rm $file
}
# ghq-fzf zsh
function ghq-fzf() {
local selected_dir=$(ghq list | fzf --query="$LBUFFER")
if [ -n "$selected_dir" ]; then
BUFFER="cd $(ghq root)/${selected_dir}"
zle accept-line
fi
zle reset-prompt
}
zle -N ghq-fzf
bindkey "^]" ghq-fzf
# Shell
b64() { echo -n $1 | base64 }
b64d() { echo -n $1 | base64 -d}
set_proxy() {
CUSTOM_HTTP_PROXY=${CUSTOM_HTTP_PROXY:-"127.0.0.1:1080"}
export http_proxy=$CUSTOM_HTTP_PROXY
export https_proxy=$CUSTOM_HTTP_PROXY
if [[ ! -z $CUSTOM_NO_PROXY ]]; then
export no_proxy=$CUSTOM_NO_PROXY
export NO_PROXY=$CUSTOM_NO_PROXY
fi
}
unset_proxy(){
unset http_proxy
unset https_proxy
unset no_proxy
unset NO_PROXY
}
nasm-bin() { nasm $1.asm -f bin -o $1.bin }
function take() {
mkdir -p $@ && cd ${@:$#}
}
function loop() {
set -o pipefail
count=0
cmds=($@)
echo "Loop $count start"
# If environment variable LOOP_OUTPUT is set, output the result of the command.
if [ -n "$LOOP_OUTPUT" ]; then
echo "Loop $count output to $LOOP_OUTPUT"
cmds+=("| tee $LOOP_OUTPUT")
fi
echo "Loop commands: ${cmds[@]}"
while eval ${cmds[@]}; do
echo "Loop $count end"
count=$((count+1))
echo "Loop $count start"
done
echo "Loop $count end (exit)\n"
set +o pipefail
}
# Move the repo to the ghq directory according to git remote url.
hatch() {
repo=$1
hash ghq
cd $repo
git_url=`git remote -v | grep fetch | awk '{print $2}'`
cd ..
platform=`echo $git_url | awk -F'[:/@]' '{print $2}'`
username=`echo $git_url | awk -F'[:/@]' '{print $3}'`
repo_name=`echo $git_url | awk -F'[:/@]' '{print $4}' | sed 's/\.git$//'`
dest=`ghq root`/$platform/$username
echo "Moving $repo to $dest/$repo_name"
read REPLY\?"Do you want to continue (y/n)?"
# If $dest does not exist, create it.
if [ ! -d $dest ]; then
mkdir -p $dest
fi
# If $dest/$repo_name exists, prompt then abort.
if [ -d $dest/$repo_name ]; then
echo "$dest/$repo_name already exists."
return 1
fi
if [ "$REPLY" = "y" ]; then
mv $repo $dest/$repo_name
cd $dest/$repo_name
else
echo "Aborted, please enter y to confirm."
fi
}
# Select wanted file and copy them to current dir.
update_from_template() {
local templates_dir="$HOME/templates"
local selected_file source_file target_file
# Check if templates directory exists
if [[ ! -d "$templates_dir" ]]; then
echo "Error: Templates directory $templates_dir does not exist" >&2
return 1
fi
# Check if fzf is installed
if ! command -v fzf &>/dev/null; then
echo "Error: fzf is required but not installed" >&2
return 1
fi
# Select template using fzf
selected_file=$(
find "$templates_dir" -maxdepth 1 -type f -printf "%f\n" \
| fzf --height=40% --reverse --border --prompt="Select template: "
)
# Exit if no selection
[[ -z "$selected_file" ]] && return 0
source_file="$templates_dir/$selected_file"
target_file="./$selected_file"
# Handle existing target file
if [[ -e "$target_file" ]]; then
# Load zsh/stat module for mtime comparison
zmodload zsh/stat || {
echo "Error: zsh/stat module required for modification time check" >&2
return 1
}
# Get modification times (epoch seconds)
local tpl_mtime target_mtime
echo "$source_file, $target_file"
zstat -A tpl_stat +mtime "$source_file"
tpl_mtime=${tpl_stat}
zstat -A target_stat +mtime "$target_file"
target_mtime=${target_stat}
echo "template mtime: '$tpl_mtime', target mtime: '$target_mtime'"
# Compare modification times
if (( tpl_mtime > target_mtime )); then
read -q "REPLY?Target file '$target_file' exists. Template is newer. Overwrite? [y/N] "
echo # Move to new line after read -q
if [[ $REPLY =~ ^[Yy]$ ]]; then
cp -v "$source_file" "$target_file"
else
echo "WARNING! Skipping overwrite, Template is older."
fi
else
echo "Target file '$target_file' is up to date or newer. Not overwriting"
fi
else
# Copy new file
cp -v "$source_file" "$target_file"
fi
}