-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.functions
203 lines (166 loc) · 6.15 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
#!/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" "dlyt" "find-free-port" "generate-passphrase" "hops" "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."
}
# `dlyt` downloads a YouTube video to ~/Movies/YouTube, organised by channel
# and named with the upload date (YYYYMMDD). Downloads in 1080p.
# Requires: yt-dlp, ffmpeg (for merging formats)
# Usage: dl-yt <youtube_url>
dlyt() {
# Check for yt-dlp dependency
if ! command -v yt-dlp &> /dev/null; then
echo "error: yt-dlp command not found." >&2
echo "Install via Homebrew: brew install yt-dlp ffmpeg" >&2
return 1
fi
# Check for URL argument
if [ -z "$1" ]; then
echo "error: No YouTube URL provided." >&2
echo "Usage: dl-yt <youtube_url>" >&2
return 1
fi
local url="$1"
# Use $HOME for reliability in scripts
local output_dir="$HOME/Movies/YouTube"
# Format: <OutputDir>/<ChannelName>/<YYYYMMDD> - <VideoTitle>.<Ext>
local output_template="$output_dir/%(channel)s/%(upload_date)s - %(title)s.%(ext)s"
# Select best 1080p video + best audio, fallback to best pre-merged 1080p
local format_spec='bestvideo[height=1080]+bestaudio/best[height=1080]'
echo "> Downloading YouTube video:"
echo " URL: $url"
echo " Quality: 1080p"
echo " Output: $output_template"
# Execute yt-dlp. It handles directory creation and prints progress/errors.
yt-dlp -f "$format_spec" -o "$output_template" "$url"
}
# `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 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
}
# `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;
}