-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.zsh_functions
171 lines (151 loc) · 3.46 KB
/
.zsh_functions
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
# shellcheck disable=SC2148
# vim: foldmethod=marker
stderr() {
echo "${@}" 1>&2
}
fail() {
stderr "${1}"
stderr ""
stderr "Exiting …"
exit "${2:-1}"
}
# Search on both snap and apt
apps() {
snap search $1
apt search $1
}
# base64 -d
bd() {
echo "$1" | base64 -d ; echo
}
checksum() {
echo "$1 $2" > /tmp/hash.txt &&\
printf 'md5:\n'; md5sum -c /tmp/hash.txt;\
printf '\n';\
printf 'sha256:\n'; sha256sum -c /tmp/hash.txt
}
# Copy path to current working directory
cwd() {
# If WSL
if [[ "$(uname -r)" =~ .*microsoft.* ]]; then
pwd | /mnt/c/Windows/System32/clip.exe
else
pwd | xclip -selection clipboard
pwd | xclip -selection primary
fi
}
datestamp() {
date +'%F'
}
m2p() {
pandoc $1 -s -f gfm -o $1.pdf &&\
rename md.pdf pdf ./$1.pdf
}
unalias md # Was aliased by OMZ
md() {
mkdir -p ${1:?} && cd ${1:?}
}
# Timestamp in microseconds
now_micro() {
date +'%s%6N'
}
# Timestamp in milliseconds
now_milli() {
date +'%s%3N'
}
# Timestamp in nanoseconds
now_nano() {
date +'%s%N'
}
stderr() {
python -c "import sys; print('$1', file=sys.stderr)"
}
timestamp() {
date +'%F_%T'
}
title() {
echo -ne "\033]0;${1}\007"
}
mcd() {
mkdir -p ${1:?Missing argument to mcd.}
cd ${1}
}
password() {
if command -v openssl &> /dev/null; then
openssl rand -base64 "${1:-32}" | paste -sd '' -
else
tr -dc A-Za-z0-9 </dev/urandom | head -c "${1:-32}" ; echo ''
fi
}
prompt_forever() {
while true; do
prompt_once && break
done
}
prompt_once() {
if read -q "x?Are you sure? (y/N) "; then
echo -en "\n"
true
else
echo -en "\n"
false
fi
}
# Toggle a theme that is friendly for copying to ```console in Markdown
zle -N _toggle_md_mode toggle_md_mode
toggle_md_mode() {
declare -r PLAIN_PROMPT='$ '
if [[ "${PROMPT}" == "${PLAIN_PROMPT}" ]]; then
zle && zle accept-line
print '\n```'
# Source theme like ~/.oh-my-zsh/oh-my-zsh.sh sources theme {{{
if [[ -n "$ZSH_THEME" ]]; then
if is_theme "$ZSH_CUSTOM" "$ZSH_THEME"; then
source "$ZSH_CUSTOM/$ZSH_THEME.zsh-theme"
elif is_theme "$ZSH_CUSTOM/themes" "$ZSH_THEME"; then
source "$ZSH_CUSTOM/themes/$ZSH_THEME.zsh-theme"
elif is_theme "$ZSH/themes" "$ZSH_THEME"; then
source "$ZSH/themes/$ZSH_THEME.zsh-theme"
else
echo "[oh-my-zsh] theme '$ZSH_THEME' not found"
fi
fi
# }}}
else
print_md_console=true
precmd() {
print ''
if [[ "${print_md_console}" == "true" ]]; then
print '```console'
print_md_console=false
fi
}
PROMPT="${PLAIN_PROMPT}"
# Reset everything {{{
unset return_code
unset user_host
unset user_symbol
unset current_dir
unset vcs_branch
unset rvm_ruby
unset venv_prompt
unset ZSH_THEME_RVM_PROMPT_OPTIONS
unset RPROMPT
unset ZSH_THEME_GIT_PROMPT_PREFIX
unset ZSH_THEME_GIT_PROMPT_SUFFIX
unset ZSH_THEME_GIT_PROMPT_DIRTY
unset ZSH_THEME_GIT_PROMPT_CLEAN
unset ZSH_THEME_HG_PROMPT_PREFIX
unset ZSH_THEME_HG_PROMPT_SUFFIX
unset ZSH_THEME_HG_PROMPT_DIRTY
unset ZSH_THEME_HG_PROMPT_CLEAN
unset ZSH_THEME_RUBY_PROMPT_PREFIX
unset ZSH_THEME_RUBY_PROMPT_SUFFIX
unset ZSH_THEME_VIRTUAL_ENV_PROMPT_PREFIX
unset ZSH_THEME_VIRTUAL_ENV_PROMPT_SUFFIX
unset ZSH_THEME_VIRTUALENV_PREFIX
unset ZSH_THEME_VIRTUALENV_SUFFIX
# }}}
zle && zle accept-line
fi
}