Files
omarchy/default/bash/functions
David Heinemeier Hansson a3c5e589f6 Extract pkg cmd abstractions and clean up all migrations (#1291)
* No using custom chromium.desktop any more

* Spacing

* Rely on $OMARCHY_PATH

* Add common pkg and cmd functions for use in migrations and elsewhere

* Simple pkg-adds

* Later migration does it

* Ensure running migrations on older installs have the new pkg/cmds available

* Spacing

* Use new abstractions

* Installed in later migration

* Needless comment

* Use new commands

* Fix package name from 'batt' to 'bat'
2025-08-30 07:41:05 +02:00

119 lines
3.0 KiB
Plaintext

# Compression
compress() { tar -czf "${1%/}.tar.gz" "${1%/}"; }
alias decompress="tar -xzf"
# Write iso file to sd card
iso2sd() {
if [ $# -ne 2 ]; then
echo "Usage: iso2sd <input_file> <output_device>"
echo "Example: iso2sd ~/Downloads/ubuntu-25.04-desktop-amd64.iso /dev/sda"
echo -e "\nAvailable SD cards:"
lsblk -d -o NAME | grep -E '^sd[a-z]' | awk '{print "/dev/"$1}'
else
sudo dd bs=4M status=progress oflag=sync if="$1" of="$2"
sudo eject $2
fi
}
# Format an entire drive for a single partition using ext4
format-drive() {
if [ $# -ne 2 ]; then
echo "Usage: format-drive <device> <name>"
echo "Example: format-drive /dev/sda 'My Stuff'"
echo -e "\nAvailable drives:"
lsblk -d -o NAME -n | awk '{print "/dev/"$1}'
else
echo "WARNING: This will completely erase all data on $1 and label it '$2'."
read -rp "Are you sure you want to continue? (y/N): " confirm
if [[ "$confirm" =~ ^[Yy]$ ]]; then
sudo wipefs -a "$1"
sudo dd if=/dev/zero of="$1" bs=1M count=100 status=progress
sudo parted -s "$1" mklabel gpt
sudo parted -s "$1" mkpart primary ext4 1MiB 100%
sudo mkfs.ext4 -L "$2" "$([[ $1 == *"nvme"* ]] && echo "${1}p1" || echo "${1}1")"
sudo chmod -R 777 "/run/media/$USER/$2"
echo "Drive $1 formatted and labeled '$2'."
fi
fi
}
# Transcode a video to a good-balance 1080p that's great for sharing online
transcode-video-1080p() {
ffmpeg -i $1 -vf scale=1920:1080 -c:v libx264 -preset fast -crf 23 -c:a copy ${1%.*}-1080p.mp4
}
# Transcode a video to a good-balance 4K that's great for sharing online
transcode-video-4K() {
ffmpeg -i $1 -c:v libx265 -preset slow -crf 24 -c:a aac -b:a 192k ${1%.*}-optimized.mp4
}
# Transcode any image to JPG image that's great for shrinking wallpapers
img2jpg() {
magick $1 -quality 95 -strip ${1%.*}.jpg
}
# Transcode any image to JPG image that's great for sharing online without being too big
img2jpg-small() {
magick $1 -resize 1080x\> -quality 95 -strip ${1%.*}.jpg
}
# Transcode any image to compressed-but-lossless PNG
img2png() {
magick "$1" -strip -define png:compression-filter=5 \
-define png:compression-level=9 \
-define png:compression-strategy=1 \
-define png:exclude-chunk=all \
"${1%.*}.png"
}
pkg-present() {
for pkg in "$@"; do
pacman -Q "$pkg" &>/dev/null || return 1
done
return 0
}
pkg-missing() {
for pkg in "$@"; do
if ! pacman -Q "$pkg" &>/dev/null; then
return 0
fi
done
return 1
}
pkg-add() {
for pkg in "$@"; do
if ! pacman -Q "$pkg" &>/dev/null; then
sudo pacman -S --noconfirm --needed "$pkg"
fi
done
}
pkg-remove() {
for pkg in "$@"; do
if pacman -Q "$pkg" &>/dev/null; then
sudo pacman -Rns --noconfirm "$pkg"
fi
done
}
cmd-present() {
for cmd in "$@"; do
command -v "$cmd" &>/dev/null || return 1
done
return 0
}
cmd-missing() {
for cmd in "$@"; do
if ! command -v "$cmd" &>/dev/null; then
return 0
fi
done
return 1
}