forked from alrra/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bash_functions
executable file
Β·197 lines (145 loc) Β· 4.57 KB
/
bash_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
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
#!/bin/bash
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Clone a repository and install its dependencies.
clone() {
git clone "$1" \
|| return
cd "$(basename "${1%.*}")" \
|| return
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Check if there are dependencies to be installed.
if [ ! -f "package.json" ]; then
return
fi
# Check if the project uses Yarn.
if [ -f "yarn.lock" ] && command -v "yarn" $> /dev/null; then
printf "\n"
yarn install
return
fi
# If not, assume it uses npm.
if command -v "npm" $> /dev/null; then
printf "\n"
npm install
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Create data URI from a file.
datauri() {
local mimeType=""
if [ ! -f "$1" ]; then
printf "%s is not a file.\n" "$1"
return
fi
mimeType=$(file --brief --mime-type "$1")
# ββ do not prepend the filename to the output
if [[ $mimeType == text/* ]]; then
mimeType="$mimeType;charset=utf-8"
fi
printf "data:%s;base64,%s" \
"$mimeType" \
"$(openssl base64 -in "$1" | tr -d "\n")"
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Delete files that match a certain pattern from the current directory.
delete-files() {
local q="${1:-*.DS_Store}"
find . -type f -name "$q" -ls -delete
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Execute Vim macro
evm() {
local numberOfTimes="${*: -1}"
local files
if [[ "$numberOfTimes" =~ ^[0-9]+$ ]]; then
files=("${@:1:$#-1}")
else
numberOfTimes="1"
files=("$@")
fi
for file in "${files[@]}"; do
printf "* %s\n" "$file"
vim \
-c "norm! $numberOfTimes@q" \
-c "wq" \
"$file"
done
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Process phone photo.
ppp() {
# Check if ImageMagick's convert command-line tool is installed.
if ! command -v "convert" $> /dev/null; then
printf "ImageMagick's 'convert' command-line tool is not installed!"
exit
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
declare option="$1"
declare photo="${2:-*.jpg}"
declare geometry="${3:-50%}"
if [ "$option" != "clean" ] &&
[ "$option" != "resize" ]; then
option="resize"
photo="${1:-*.jpg}"
geometry="${2:-50%}"
fi
if [[ "$(echo "${photo##*.}" | tr '[:upper:]' '[:lower:]')" != "png" ]]; then
newPhotoName="${photo%.*}.png"
else
newPhotoName="_${photo%.*}.png"
fi
if [ "$option" == "resize" ]; then
convert \
"$photo" \
-colorspace RGB \
+sigmoidal-contrast 11.6933 \
-define filter:filter=Sinc \
-define filter:window=Jinc \
-define filter:lobes=3 \
-sigmoidal-contrast 11.6933 \
-colorspace sRGB \
-background transparent \
-gravity center \
-resize "$geometry" \
+append \
"$newPhotoName" \
&& printf "* %s (%s)\n" \
"$newPhotoName" \
"$geometry"
return
fi
convert \
"$photo" \
-morphology Convolve DoG:10,10,0 \
-negate \
-normalize \
-blur 0x1 \
-channel RBG \
-level 10%,91%,0.1 \
"$newPhotoName" \
&& printf "* %s\n" "$newPhotoName"
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Search history.
h() {
# ββ Enable colors for pipe.
# β ("--color=auto" enables colors only
# β if the output is in the terminal.)
grep --color=always "$*" "$HISTFILE" \
| less --no-init --raw-control-chars
# β ββ Display ANSI color escape sequences in raw form.
# ββ Don't clear the screen after quitting less.
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Search for text within the current directory.
s() {
grep --color=always "$*" \
--exclude-dir=".git" \
--exclude-dir="node_modules" \
--ignore-case \
--recursive \
. \
| less --no-init --raw-control-chars
# β ββ Display ANSI color escape sequences in raw form.
# ββ Don't clear the screen after quitting less.
}