-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.functions
249 lines (208 loc) · 7.2 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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/usr/bin/env bash
#
# Functions
#
# @author Charlie Revett (@revcd).
# `asd` outputs all available functions from this file.
asd() {
echo "> Available bash functions"
echo "> See: ~/projects/github.com/revett/dotfiles/.functions"
f=("branch" "copy-files-for-prompt" "find-free-port" "generate-passphrase" "hops" "import-kindle-snippets" "render-bw-frame" "tre")
printf -- ' - %s\n' "${f[@]}"
}
# `branch` is used within a number of aliases, and returns the branch from the current git
# directory. Taken from oh-my-zsh.
# See: https://gist.github.com/DavidToca/3086571
branch() {
ref=$(git symbolic-ref HEAD 2> /dev/null) || return
echo ${ref#refs/heads/}
}
# `copy-files-for-prompt` copies the contents of the files passed as arguments to the clipboard, with
# the file names as comments above the contents.
copy-files-for-prompt() {
if [ $# -eq 0 ]; then
echo "error: no files are provided"
return 1
fi
output=""
for file in "$@"; do
if [[ -f "$file" ]]; then
output+="File: \`$file\`\n\n\`\`\`\n$(cat "$file")\n\`\`\`\n\n"
fi
done
if [[ -z "$output" ]]; then
echo "error: no valid files to copy from."
return 1
fi
echo -e "$output" | pbcopy
echo "Copied to clipboard."
}
# `find-free-port` finds a local port that is not in use
find-free-port() {
while true; do
random_port=$(( (RANDOM % 49152) + 10000 ))
if ! nc -z 127.0.0.1 $random_port &>/dev/null; then
echo $random_port
return 0
fi
done
}
# `generate-passphrase` generates a random passphrase based password.
generate-passphrase() {
CHUNKS=4
if [ "$#" -eq 1 ]; then
CHUNKS=$1
fi
if ! command -v gshuf &> /dev/null; then
echo "error: gshuf command could not be found."
return 1
fi
gshuf -n$CHUNKS /usr/share/dict/words | tr '\n' '-' | sed 's/.$//' | awk '{print tolower($0)}'
}
# `hops` is used to manage the packages installed via Homebrew. It is a wrapper around the `brew
# bundle` command, which is used to install packages from a Brewfile. It is used to install and
# upgrade packages, and to remove packages that are no longer in the Brewfile. It also checks that
# all packages in the Brewfile are installed.
hops() {
export HOMEBREW_BUNDLE_FILE="~/projects/github.com/revett/dotfiles/Brewfile"
echo "> Running hops"
echo "> Brewfile: $HOMEBREW_BUNDLE_FILE"
echo "\n> Listing brews in Brewfile"
brew bundle list --brews
echo "\n> Listing casks in Brewfile"
brew bundle list --casks
echo "\n> Listing taps in Brewfile"
brew bundle list --taps
echo "\n> Listing VS Code (Cursor) extensions in Brewfile"
brew bundle list --vscode
echo "\n> Removing packages not in Brewfile"
brew bundle cleanup
# Prompt to continue as this is a destructive action.
echo "STOP! Do any of the above packages need to be added to the Brewfile?"
echo "Press ENTER to proceed with cleanup, any other key to exit:"
read confirmation
if [ -n "$confirmation" ]; then
echo "Exiting without cleanup."
return
fi
brew bundle --force cleanup
echo "\n> Installing and upgrading packages from Brewfile"
brew bundle install
echo "\n> Checking all packages in Brewfile are installed"
brew bundle check
unset HOMEBREW_BUNDLE_FILE
}
# `import-kindle-snippets` is used to export Kindle highlights and notes to a Notion database.
# It checks for the required dependencies, such as Homebrew and Python 3, and ensures they are
# installed before running. Additionally, it sets up a virtual environment, installs the
# `kindle2notion` package, and runs the script with the provided Notion authentication token,
# Notion database ID, and Kindle clippings file. If any required dependencies are missing,
# it will error out and inform the user to install them.
import-kindle-snippets() {
# Function to print function usage.
usage() {
echo "usage: import-kindle-snippets -t NOTION_AUTH_TOKEN -d NOTION_DATABASE_ID -f KINDLE_CLIPPINGS_FILE"
return 1
}
# Parse command-line arguments.
while getopts ":t:d:f:" opt; do
case ${opt} in
t )
notion_auth_token=$OPTARG
;;
d )
notion_database_id=$OPTARG
;;
f )
kindle_clippings_file=$OPTARG
;;
\? )
usage
return 1
;;
esac
done
# Ensure all arguments are provided.
if [ -z "$notion_auth_token" ] || [ -z "$notion_database_id" ] || [ -z "$kindle_clippings_file" ]; then
usage
return 1
fi
# Check if Python 3 is installed.
if ! command -v python3 &> /dev/null; then
echo "error: Python 3 is not installed. Please install Python 3 first."
return 1
fi
# Create a virtual environment.
echo "creating virtual environment..."
python3 -m venv kindle2notion-env
if [ $? -ne 0 ]; then
echo "error: Failed to create virtual environment"
return 1
fi
source kindle2notion-env/bin/activate
if [ $? -ne 0 ]; then
echo "Error: Failed to activate virtual environment"
return 1
fi
# Install kindle2notion.
echo "installing kindle2notion package..."
pip install kindle2notion
if [ $? -ne 0 ]; then
echo "error: Failed to install kindle2notion"
deactivate
return 1
fi
# Run the Kindle2Notion script
echo "running kindle2notion script..."
kindle2notion "$notion_auth_token" "$notion_database_id" "$kindle_clippings_file"
if [ $? -ne 0 ]; then
echo "error: Failed to run kindle2notion script"
deactivate
return 1
fi
# Deactivate the virtual environment
deactivate
echo "Done!"
return 0
}
# `render-bw-frame` is a function to add a white border to an image and make it square with a black
# background. The function takes one or more image files as arguments and processes each image
# file. The processed images are saved in the same directory as the original image files with the
# suffix `-bw_frame.jpg`.
render-bw-frame() {
# Function to add white border and make the image square with a black background
process_image() {
local input="$1"
local filename=$(basename "$input")
local output="${filename%.*}-bw_frame.jpg"
# Add white border.
ffmpeg -i "$input" -vf "pad=iw+180:ih+180:90:90:white" -update 1 -frames:v 1 "${filename%.*}-white_border.jpg"
# Make the image square with a black background.
ffmpeg -i "${filename%.*}-white_border.jpg" -vf "scale=iw*min(3840/iw\,3840/ih):ih*min(3840/iw\,3840/ih),pad=3840:3840:(3840-iw)/2:(3840-ih)/2:color=black" "$output"
# Remove the intermediate white border image.
rm "${filename%.*}-white_border.jpg"
}
# Check if at least one argument is provided.
if [ $# -eq 0 ]; then
echo "Please provide at least one image file as an argument."
return 1
fi
# Process each image file.
for image in "$@"; do
if [ -f "$image" ]; then
process_image "$image"
else
echo "File not found: $image"
fi
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() {
if ! command -v tree &> /dev/null; then
echo "error: tree command could not be found."
return 1
fi
tree -aC -I '.git|node_modules|.cache' --dirsfirst "$@" | less -FRNX;
}