-
Notifications
You must be signed in to change notification settings - Fork 0
/
.functions
163 lines (146 loc) · 4.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
# Create and run Java class
run-java() {
command javac $1 && java $1:gs/.java/
}
# Change display brightness (percentage)
bright() {
brightnessctl set ${1}%
}
# Run job as asynchronous and without output
#
# Parameters: $1: Command; $2: Arguments of the command; $3: A `--no-disown`
# optional argument that keeps the process owned by the terminal
#
ra() {
$1 $2 > /dev/null 2>&1 &
[[ ! $3 == '--no-disown' ]] && disown || return 0
}
# List the first n files in a directory based on their date (the most
# recently-modified appears on the top)
#
# Parameters: $1: {h|t}; $2: The number of files
#
lsn() {
# NOTE:
# The piping to `sed` removes the `%T@` timestamp and the `./` character that
# precedes the output of `find`
find . -type f -printf "%T@ %p\n" \
| sort -nr \
| $([[ $1 == h ]] && echo "head" || echo "tail") -n $2 \
| sed 's/^.\{24\}//g'
}
# Convert video from Apple's MOV format to MP4 and compressing it using the
# H.264 codec
#
# Parameters: $1 Name of the input file without the `.mov` extension; $2 Name
# of the desired output file without the `.mp4` extension
#
mov2mp4() {
ffmpeg -i $1.mov -c:v libx264 -pix_fmt yuv420p $2.mp4
}
# Remove audio from video, i.e., making it silent
#
# Parameters: $1 Name of the input file
#
silence() {
ffmpeg -i "$1" -c copy -an "${1%.*}_silent.${1#*.}"
}
# Convert `.ipynb` files to `.html`
#
# Parameters: $1: The `.ipynb` file name without the extension (if the
# extension is included, the function will remove it automatically)
#
jupyter2html() {
random_hex=$(openssl rand -hex 4)
[[ $1 == *.ipynb ]] && file=$(echo $1 | sed -e "s/\.ipynb$//") || file=$1
temp_file=$file.no_metadata_widgets.$random_hex.ipynb
jq -M 'del(.metadata.widgets)' $file.ipynb > $temp_file
jupyter nbconvert --to html $temp_file --output $file.html
gio trash $temp_file
}
# Zip and unzip EPUB files
#
# When zipping, this function is designed to be used from *inside* the
# directory
#
# Parameters: $1: {zip|unzip}; $2: The name of the new `.epub` file; $3 For
# $1=unzip only, specify the name of the directory for the unzipped file
#
epubctl() {
if [[ $1 == "zip" ]]; then
zip -rX ../$2.epub ./* # mimetype META-INF/ OEBPS/
elif [[ $1 == "unzip" ]]; then
unzip $2 -d $3
else
>&2 echo 'Invalid argument for `$1`' && false # Output error code 1
fi
}
# Copy plain text file content to clipboard
#
# Parameters: $1: The name of the file
#
copypt() {
xclip -selection c < $1
}
# Copy plain text file content to clipboard and move to trash afterwards
#
# Parameters: $1: The name of the file
#
copyptrm() {
xclip -selection c < $1 && gio trash $1
}
# Mount `rclone` volumes
#
# Parameters: $1: Remote name (e.g. onedrive); $2: Local directory (e.g.
# ~/OneDrive)
#
rcmount() {
if [[ $1 == 'onedrive' ]]; then
rclone mount onedrive: ~/OneDrive --onedrive-delta --vfs-cache-mode full
elif [[ $1 == 'googledrive' ]]; then
rclone mount googledrive: ~/GoogleDrive --vfs-cache-mode full
else
>&2 echo 'Invalid argument for `$1`' && false # Output error code 1
fi
}
# Find an item inside a directory
#
# Parameters: $1: The type of item; $2: The regex of the item excluding the `./`
#
search() {
find . -type $1 -regex "./$2"
}
# Make the image's width and height equal by creating paddings on the
# appropriate axis (left & rignt or top & bottom).
#
# Function will fail if ImageMagick (i.e. `convert`) is not installed or not on
# `$PATH`.
#
# Parameters: $1: Input; $2: Output; $3: Background (e.g. 'transparent',
# 'white')
#
square-image() {
if ! command -v convert &> /dev/null ; then
>&2 echo 'ImageMagick is either not installed or not on `$PATH`'
return 1
fi
local w=$(identify -format '%w' $1)
local h=$(identify -format '%h' $1)
local d=$(($w - $h))
local da=${d#-}
local additional
local extent_args
local splice_args
[[ $(($da / 2 + $da / 2)) == $da ]] && additional=0 || additional=1
if [[ $(($w)) > $(($h)) ]]; then
extent_args="${w}x$(($h + $da / 2))"
splice_args="0x$(($da / 2 + $additional))"
else
extent_args="$(($w + $da / 2))x${h}"
splice_args="$(($da / 2 + $additional))x0"
fi
command convert \
-extent $extent_args -background $3 \
-splice $splice_args -background $3 \
$1 $2
}