forked from nhs-england-tools/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdot_functions
More file actions
177 lines (146 loc) · 3.74 KB
/
dot_functions
File metadata and controls
177 lines (146 loc) · 3.74 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
# ==============================================================================
# git
function git-pull() {
git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote" 2> /dev/null; done
git pull --all
}
function git-prune() {
git fetch --prune
for branch in $(git for-each-ref --format '%(refname) %(upstream:track)' refs/heads | awk '$2 == "[gone]" {sub("refs/heads/", "", $1); print $1}'); do
git branch -D $branch
done
for branch in $(git branch -vv | cut -c 3- | awk '$3 !~/\[origin/ { print $1 }'); do
git branch -D $branch
done
}
function git-track() {
branch=$(git rev-parse --abbrev-ref HEAD)
git branch --set-upstream-to=origin/$branch $branch
}
# ==============================================================================
# Miscellaneous functions
# Generate a random string ('A-Za-z0-9') with the given length
#
# Usage:
# $ ./random 32
#
function random() {
chars='A-Za-z0-9'
length=${1:-32}
cat /dev/urandom | env LC_ALL=C tr -dc $chars | fold -w $length | head -n 1
}
# Convert a decimal number to a byte unit
#
# Usage:
# echo 1234567890 | byte-me
#
function byte-me() {
local list="bytes,KB,MB,GB,TB,PB,EB,ZB,YB"
local p=1
local data=$(cat)
local v=$(echo "scale=2; $data / 1" | bc)
local i=$(echo $v / 1024 | bc)
while [ ! $i = "0" ]; do
let p=p+1
v=$(echo "scale=2; $v / 1024" | bc)
i=$(echo $v / 1024 | bc)
done
echo $v$(echo $list | cut -f$p -d,)
}
# ==============================================================================
# containers (docker / podman)
# Provide a transparent `docker` command that delegates to podman when Docker
# CLI is not installed. This means any script, guide, or muscle-memory command
# using `docker ...` will just work with Podman.
if ! command -v docker > /dev/null 2>&1 && command -v podman > /dev/null 2>&1; then
function docker() {
podman "$@"
}
fi
# ==============================================================================
# macOS utility functions
# Lock the screen
#
# Usage:
# $ afk
#
function afk() {
osascript -e 'tell application "System Events" to key code 12 using {control down, command down}'
}
# Flush the DNS cache
#
# Usage:
# $ flush
#
function flush() {
sudo dscacheutil -flushcache
sudo killall -HUP mDNSResponder
}
# Hide desktop icons (useful during screen sharing)
#
# Usage:
# $ hidedesktop
#
function hidedesktop() {
defaults write com.apple.finder CreateDesktop -bool false
killall Finder
}
# Show desktop icons
#
# Usage:
# $ showdesktop
#
function showdesktop() {
defaults write com.apple.finder CreateDesktop -bool true
killall Finder
}
# Spin up a quick HTTP server in the current directory
#
# Usage:
# $ server [port]
#
function server() {
local port="${1:-8000}"
python3 -m http.server "$port" &
sleep 1
open "http://localhost:${port}"
}
# Create a directory and cd into it
#
# Usage:
# $ mkd path/to/dir
#
function mkd() {
mkdir -p "$@" && cd "$_" || return
}
# Extract any archive into a subdirectory named after the archive
#
# Usage:
# $ extract archive.tar.gz
#
function extract() {
if [ -f "$1" ]; then
local dir="${1%.*}"
mkdir -p "$dir"
case "$1" in
*.tar.bz2) tar xjf "$1" -C "$dir" ;;
*.tar.gz) tar xzf "$1" -C "$dir" ;;
*.tar.xz) tar xf "$1" -C "$dir" ;;
*.zip) unzip "$1" -d "$dir" ;;
*.gz) gunzip "$1" ;;
*.bz2) bunzip2 "$1" ;;
*) echo "Cannot extract '$1'" ;;
esac
else
echo "File not found: $1"
fi
}
# List processes using the camera
#
# Usage:
# $ camerausedby
#
function camerausedby() {
echo "Checking camera usage..."
lsof | grep -w "AppleCamera\|USBVDC\|iSight" | awk '{print $2, $1}' | sort -u
}