-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.functions
126 lines (104 loc) · 3.62 KB
/
.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
#!/usr/bin/env bash
enable-brew () {
alias brew="$HOME/.local/homebrew/bin/brew"
}
# Escape UTF-8 characters into their 3-byte format
function escape() {
printf "\\\x%s" $(printf "$@" | xxd -p -c1 -u)
# print a newline unless we’re piping the output to another program
if [ -t 1 ]; then
echo # newline
fi
}
# Decode \x{ABCD}-style Unicode escape sequences
function unidecode() {
perl -e "binmode(STDOUT, ':utf8'); print \"$@\""
# print a newline unless we’re piping the output to another program
if [ -t 1 ]; then
echo # newline
fi
}
# Get a character’s Unicode code point
function codepoint() {
perl -e "use utf8; print sprintf('U+%04X', ord(\"$@\"))"
# print a newline unless we’re piping the output to another program
if [ -t 1 ]; then
echo # newline
fi
}
# Manually remove a downloaded app or file from the quarantine
function unquarantine() {
for attribute in com.apple.metadata:kMDItemDownloadedDate com.apple.metadata:kMDItemWhereFroms com.apple.quarantine; do
xattr -r -d "$attribute" "$@"
done
}
# `tre` is a shorthand for `tree` with hidden files and color enabled, ignoring
# the `.git` directory, listing directories first. The output gets piped into
# `less` with options to preserve color and line numbers, unless the output is
# small enough for one screen.
tre() {
tree -aC -I '.git' --dirsfirst "$@" | less -FRNX
}
# Get colors in manual pages
man() {
env \
LESS_TERMCAP_mb=$(printf "\e[1;31m") \
LESS_TERMCAP_md=$(printf "\e[1;31m") \
LESS_TERMCAP_me=$(printf "\e[0m") \
LESS_TERMCAP_se=$(printf "\e[0m") \
LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
LESS_TERMCAP_ue=$(printf "\e[0m") \
LESS_TERMCAP_us=$(printf "\e[1;32m") \
man "$@"
}
# check if uri is up
isup() {
local uri=$1
if curl -s --head --request GET "$uri" | grep "200 OK" > /dev/null ; then
notify-send --urgency=critical "$uri is down"
else
notify-send --urgency=low "$uri is up"
fi
}
# Usage: pls [<var>]
# List path entries of PATH or environment variable <var>.
pls () { eval echo \$${1:-PATH} |tr : '\n'; }
# Usage: pshift [-n <num>] [<var>]
# Shift <num> entries off the front of PATH or environment var <var>.
# with the <var> option. Useful: pshift $(pwd)
pshift () {
local n=1
[ "$1" = "-n" ] && { n=$(( $2 + 1 )); shift 2; }
eval "${1:-PATH}='$(pls |tail -n +$n |tr '\n' :)'"
}
# Usage: ppop [-n <num>] [<var>]
# Pop <num> entries off the end of PATH or environment variable <var>.
ppop () {
local n=1 i=0
[ "$1" = "-n" ] && { n=$2; shift 2; }
while [ $i -lt $n ]
do eval "${1:-PATH}='\${${1:-PATH}%:*}'"
i=$(( i + 1 ))
done
}
# Usage: prm <path> [<var>]
# Remove <path> from PATH or environment variable <var>.
prm () { eval "${2:-PATH}='$(pls $2 |grep -v "^$1\$" |tr '\n' :)'"; }
# Usage: punshift <path> [<var>]
# Shift <path> onto the beginning of PATH or environment variable <var>.
punshift () { eval "${2:-PATH}='$1:$(eval echo \$${2:-PATH})'"; }
# Usage: ppush <path> [<var>]
ppush () { eval "${2:-PATH}='$(eval echo \$${2:-PATH})':$1"; }
# Usage: puniq [<path>]
# Remove duplicate entries from a PATH style value while retaining
# the original order. Use PATH if no <path> is given.
#
# Example:
# $ puniq /usr/bin:/usr/local/bin:/usr/bin
# /usr/bin:/usr/local/bin
puniq () {
# use $@ instead of $1 to cater for pathnames containing spaces
# also separate the various paths with '|' and reflect it in nl, sort and cut
echo "$@" | sed -e 's/^[ ][ ]*//' -e 's/:$//' -e 's/^://' |tr : '\n' | nl -s '|' | sort -u -t '|' -k 2,2 |sort -n | \
cut -f 2- -d '|' | tr '\n' : | sed -e 's/^[ ][ ]*//' -e 's/:$//' -e 's/^://'
}