forked from alrra/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbash_functions
More file actions
executable file
·228 lines (172 loc) · 5.84 KB
/
bash_functions
File metadata and controls
executable file
·228 lines (172 loc) · 5.84 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/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
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# 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.
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# From the specified files, rename the files containing a date
# in the filename to only the date in the following format:
#
# <year>-<month>-<day> <hour>.<minute>.<second>
#
# Usage examples:
#
# * rename-files-with-date-in-name path/to/some/directory path/to/some/file ...
rename-files-with-date-in-name() (
rename_file() (
filePath=$(dirname "${1%/}")
fileName=$(basename "$1")
# The following will do transformations such as:
#
# * 20200505_050505.dng => 2020-05-05 05.05.05.dng
# * Screenshot 2020-01-02 at 03.04.05.png => 2020-01-02 03-04-05.jpg
# * Screenshot_20201010-101010_Something.jpg => 2020-10-10 10-10-10.jpg
# * signal-2020-05-06-07-08-09-123.mp4 => 2020-05-06 07-08-09.mp4
newFilePath="${filePath}/$(printf "%s" "$fileName" | sed 's/[^0-9]*\([0-9]\{4\}\)[_-]\{0,1\}\([0-9]\{2\}\)[_-]\{0,1\}\([0-9]\{2\}\)[_-]\{0,1\}\( at \)\{0,1\}\([0-9]\{2\}\)[_.-]\{0,1\}\([0-9]\{2\}\)[_.-]\{0,1\}\([0-9]\{2\}\).*\(\..*\)$/\1-\2-\3 \5.\6.\7\8/')"
if [ "$newFilePath" != "$1" ]; then
mv -f "$1" "$newFilePath"
fi
)
# ┌─ Default to the current directory.
for filePath in "${@:-.}"; do
if [ -d "$filePath" ]; then
find "${filePath%/}" \
-type f \
-depth 1 \
-print \
| while read -r f; do
rename_file "$f"
done
elif [ -f "$filePath" ]; then
rename_file "$filePath"
fi
done
)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Resize image.
#
# Create a new image based on the specified image resized by the
# specified amount.
#
# $1: Path to the original image.
# $2: Resize value (default is 50%).
# See also: https://imagemagick.org/script/command-line-processing.php#geometry
#
# Usage examples:
#
# * resize-image ./path/to/image.jpg 30%
# * resize-image ./path/to/image.jpg 1000x1000!
resize-image() {
# 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 path="$(dirname "$1")"
declare fileName="$(basename "$1")"
declare geometry="${2:-50%}"
convert \
"$1" \
-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 \
"$path/_$fileName" \
&& printf "* %s (%s)\n" \
"$path/_$fileName" \
"$geometry"
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# 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.
}