forked from homeofficehost/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.shellfn
151 lines (132 loc) · 5.11 KB
/
.shellfn
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
#!/bin/sh
[ -r $HOME/.projectfn ] && source $HOME/.projectfn
# Utility Functions
# Create a new git repo with one README commit and CD into it
function gitnr() { mkdir $1; cd $1; git init; touch README; git add README; git commit -mFirst-commit;}
# Do a Matrix movie effect of falling characters
function matrix1() {
echo -e "\e[1;40m" ; clear ; while :; do echo $LINES $COLUMNS $(( $RANDOM % $COLUMNS)) $(( $RANDOM % 72 )) ;sleep 0.05; done|gawk '{ letters="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@#$%^&*()"; c=$4; letter=substr(letters,c,1);a[$3]=0;for (x in a) {o=a[x];a[x]=a[x]+1; printf "\033[%s;%sH\033[2;32m%s",o,x,letter; printf "\033[%s;%sH\033[1;37m%s\033[0;0H",a[x],x,letter;if (a[x] >= $1) { a[x]=0; } }}'
}
function matrix2() {
echo -e "\e[1;40m" ; clear ; characters=$( jot -c 94 33 | tr -d '\n' ) ; while :; do echo $LINES $COLUMNS $(( $RANDOM % $COLUMNS)) $(( $RANDOM % 72 )) $characters ;sleep 0.05; done|gawk '{ letters=$5; c=$4; letter=substr(letters,c,1);a[$3]=0;for (x in a) {o=a[x];a[x]=a[x]+1; printf "\033[%s;%sH\033[2;32m%s",o,x,letter; printf "\033[%s;%sH\033[1;37m%s\033[0;0H",a[x],x,letter;if (a[x] >= $1) { a[x]=0; } }}'
}
# Use Mac OS Preview to open a man page in a more handsome format
function manp() {
man -t $1 | open -f -a /Applications/Preview.app
}
# Show normally hidden system and dotfile types of files
# in Mac OS Finder
function showhiddenfiles() {
defaults write com.apple.Finder AppleShowAllFiles YES
osascript -e 'tell application "Finder" to quit'
sleep 0.25
osascript -e 'tell application "Finder" to activate'
}
# Hide (back to defaults) normally hidden system and dotfile types of files
# in Mac OS Finder
function hidehiddenfiles() {
defaults write com.apple.Finder AppleShowAllFiles NO
osascript -e 'tell application "Finder" to quit'
sleep 0.25
osascript -e 'tell application "Finder" to activate'
}
## hammer a service with curl for a given number of times
## usage: curlhammer $url
function curlhammer () {
bot "about to hammer $1 with $2 curls ⇒";
echo "curl -k -s -D - $1 -o /dev/null | grep 'HTTP/1.1' | sed 's/HTTP\/1.1 //'"
for i in {1..$2}
do
curl -k -s -D - $1 -o /dev/null | grep 'HTTP/1.1' | sed 's/HTTP\/1.1 //'
done
bot "done"
}
## curlheader will return only a specific response header or all response headers for a given URL
## usage: curlheader $header $url
## usage: curlheader $url
function curlheader() {
if [[ -z "$2" ]]; then
echo "curl -k -s -D - $1 -o /dev/null"
curl -k -s -D - $1 -o /dev/null:
else
echo "curl -k -s -D - $2 -o /dev/null | grep $1:"
curl -k -s -D - $2 -o /dev/null | grep $1:
fi
}
## get the timings for a curl to a URL
## usage: curltime $url
function curltime(){
curl -w " time_namelookup: %{time_namelookup}\n\
time_connect: %{time_connect}\n\
time_appconnect: %{time_appconnect}\n\
time_pretransfer: %{time_pretransfer}\n\
time_redirect: %{time_redirect}\n\
time_starttransfer: %{time_starttransfer}\n\
--------------------------\n\
time_total: %{time_total}\n" -o /dev/null -s "$1"
}
function fixperms(){
find . \( -name "*.sh" -or -type d \) -exec chmod 755 {} \; && find . -type f ! -name "*.sh" -exec chmod 644 {} \;
}
# Create a new directory and enter it
function mkd() {
mkdir -p "$@" && cd "$_";
}
# Generate Subresource Integrity hashes.
# 1st argument is the filename.
# 2nd argument, optional, is the hash algorithm
# (currently the allowed prefixes are sha256, sha384, and sha512)
# See http://www.w3.org/TR/SRI/ and
# https://developer.mozilla.org/docs/Web/Security/Subresource_Integrity
function sri() {
if [ -z "${1}" ]; then
echo "ERROR: No file specified.";
return 1;
fi;
local algorithm="${2:-sha512}"
if ! echo "${algorithm}" | egrep -q "^sha(256|384|512)$"; then
echo "ERROR: hash algorithm must be sha256, sha384 or sha512.";
return 1;
fi;
local filehash=$(openssl dgst "-${algorithm}" -binary "$1" | openssl base64 -A)
if [ -z "${filehash}" ]; then
return 1;
fi;
echo "${algorithm}-${filehash}";
}
## output directory/file tree, excluding ignorables
function tre(){
tree -aC -I '.git|node_modules|bower_components|.DS_Store' --dirsfirst "$@"
}
function weather() {
curl wttr.in/$1
}
function ipinfo(){
curl ipinfo.io/$1
}
# fo [FUZZY PATTERN] - Open the selected file with the default editor
# - Bypass fuzzy finder if there's only one match (--select-1)
# - Exit if there's no match (--exit-0)
function fo() {
local files
IFS=$'\n' files=($(fzf-tmux --query="$1" --multi --select-1 --exit-0))
[[ -n "$files" ]] && ${EDITOR:-vim} "${files[@]}"
}
# easier: echo http://url/q?=$( rawurlencode "$args" )
# faster: rawurlencode "$args"; echo http://url/q?${REPLY}
rawurlencode() {
local string="${1}"
local strlen=${#string}
local encoded=""
local pos c o
for (( pos=0 ; pos<strlen ; pos++ )); do
c=${string:$pos:1}
case "$c" in
[-_.~a-zA-Z0-9] ) o="${c}" ;;
* ) printf -v o '%%%02x' "'$c"
esac
encoded+="${o}"
done
echo "${encoded}" # You can either set a return variable (FASTER)
REPLY="${encoded}" #+or echo the result (EASIER)... or both... :p
}