summaryrefslogtreecommitdiff
path: root/dotfiles/common/.zshrc.d/utilities.sh
diff options
context:
space:
mode:
Diffstat (limited to 'dotfiles/common/.zshrc.d/utilities.sh')
-rw-r--r--dotfiles/common/.zshrc.d/utilities.sh206
1 files changed, 206 insertions, 0 deletions
diff --git a/dotfiles/common/.zshrc.d/utilities.sh b/dotfiles/common/.zshrc.d/utilities.sh
new file mode 100644
index 0000000..431cac0
--- /dev/null
+++ b/dotfiles/common/.zshrc.d/utilities.sh
@@ -0,0 +1,206 @@
+# utilities.sh
+# Craig Jennings <c@cjennings.net>
+# General utility functions
+
+# =============================================================================
+# Archive Extraction
+# =============================================================================
+extract() {
+ if [ -f "$1" ]; then
+ case "$1" in
+ *.tar.bz2) tar xjf "$1" ;;
+ *.tar.gz) tar xzf "$1" ;;
+ *.bz2) bunzip2 "$1" ;;
+ *.rar) rar x "$1" ;;
+ *.gz) gunzip "$1" ;;
+ *.tar) tar xf "$1" ;;
+ *.tbz2) tar xjf "$1" ;;
+ *.tgz) tar xzf "$1" ;;
+ *.zip) unzip "$1" ;;
+ *.Z) uncompress "$1" ;;
+ *) echo "$1 cannot be extracted via extract()" ;;
+ esac
+ else
+ echo "$1 is not a valid file"
+ fi
+}
+
+# =============================================================================
+# Archive Compression
+# =============================================================================
+compress() {
+ if [ $# -ne 2 ]; then
+ echo "Usage: compress <format> <file_or_directory>"
+ echo "Formats: tar.bz2, tar.gz, bz2, tar, tbz2, tgz, zip, gz, Z"
+ return 1
+ fi
+
+ format="$1"
+ target="$2"
+
+ if [ ! -e "$target" ]; then
+ echo "Error: '$target' does not exist"
+ return 1
+ fi
+
+ basename=$(basename "$target")
+
+ case "$format" in
+ tar.bz2|tbz2) output="${basename}.tar.bz2" ;;
+ tar.gz|tgz) output="${basename}.tar.gz" ;;
+ bz2) output="${target}.bz2" ;;
+ gz) output="${target}.gz" ;;
+ tar) output="${basename}.tar" ;;
+ zip) output="${basename}.zip" ;;
+ Z) output="${target}.Z" ;;
+ *)
+ echo "Error: Unknown format '$format'"
+ return 1
+ ;;
+ esac
+
+ if [ -e "$output" ]; then
+ printf "Warning: '%s' already exists. Overwrite? (y/N): " "$output"
+ read -r response
+ case "$response" in
+ [yY]|[yY][eE][sS]) rm -f "$output" ;;
+ *) echo "Aborted." && return 1 ;;
+ esac
+ fi
+
+ case "$format" in
+ tar.bz2|tbz2) tar -cjf "$output" "$target" ;;
+ tar.gz|tgz) tar -czf "$output" "$target" ;;
+ bz2)
+ [ -d "$target" ] && echo "Error: bz2 only works on files" && return 1
+ bzip2 -k "$target"
+ ;;
+ gz)
+ [ -d "$target" ] && echo "Error: gz only works on files" && return 1
+ gzip -k "$target"
+ ;;
+ tar) tar -cf "$output" "$target" ;;
+ zip)
+ [ -d "$target" ] && zip -r "$output" "$target" || zip "$output" "$target"
+ ;;
+ Z)
+ [ -d "$target" ] && echo "Error: Z only works on files" && return 1
+ command compress -c "$target" > "$output"
+ ;;
+ esac
+
+ [ $? -eq 0 ] && echo "Created $output" || echo "Compression failed"
+}
+
+# =============================================================================
+# DD Helper
+# =============================================================================
+dd_with_bs() {
+ OUT_DIR=$(dirname "$2")
+ if [ ! -e "$1" ] || [ ! -e "$OUT_DIR" ]; then
+ echo "$1 or $OUT_DIR doesn't exist"
+ return 1
+ fi
+ IN_BS=$(stat -c "%o" "$1")
+ OUT_BS=$(stat -c "%o" "$OUT_DIR")
+ echo dd \"if=$1\" \"of=$2\" \"ibs=$IN_BS\" \"obs=$OUT_BS\"
+}
+
+# =============================================================================
+# Clock, Timer, Alarm, Stopwatch
+# =============================================================================
+export BEEP="/usr/share/sounds/freedesktop/stereo/bell.oga"
+alias beep='paplay $BEEP'
+
+clock() {
+ while true; do
+ tput clear
+ echo ""
+ date +" %l : %M %p" | figlet -f roman -w 200
+ sleep 1
+ done
+}
+
+timer() {
+ if [ "$#" -lt 2 ]; then
+ echo "Pass the time and a notification. Example: timer 1h30m Parking expiring"
+ return 1
+ fi
+ message="${*:2}"
+ start_time=$(date +"%H:%M:%S %p")
+ printf "\nStarting %s timer at %s\n" "$1" "$start_time"
+ snore "$1" && paplay "$BEEP" && notify-send "Timer" "$message"
+}
+
+alarm() {
+ if [ "$#" -lt 2 ]; then
+ echo "Pass both the time and a message. Example: alarm 1:45pm Time to eat!"
+ return 1
+ fi
+
+ if ! date -d "$1" >/dev/null 2>&1; then
+ echo "Invalid time: $1"
+ return 1
+ fi
+
+ echo "paplay \$BEEP && notify-send \"Alarm\" \"$*\"" | at "$1" >/dev/null 2>&1
+ echo ""
+ echo "Alarm '${*:2}' is queued for $1."
+ echo "To see all alarms: atq"
+ echo "To remove an alarm: atrm <number>"
+ echo ""
+}
+
+# Stopwatch
+sw_start_time=0
+sw_started=0
+
+swreset() {
+ sw_start_time=0
+ sw_started=0
+ echo "Stopwatch reset"
+}
+
+swshow() {
+ if [ "$sw_started" = 0 ]; then
+ echo "Error: Stopwatch not started" >&2
+ return 1
+ fi
+
+ current_time=$(date +%s)
+ elapsed_time=$((current_time - sw_start_time))
+
+ if [ "$elapsed_time" -lt 60 ]; then
+ echo "Elapsed time: $elapsed_time seconds"
+ elif [ "$elapsed_time" -lt 3600 ]; then
+ minutes=$((elapsed_time / 60))
+ seconds=$((elapsed_time % 60))
+ echo "Elapsed time: $minutes minutes, $seconds seconds"
+ else
+ hours=$((elapsed_time / 3600))
+ minutes=$(((elapsed_time / 60) % 60))
+ seconds=$((elapsed_time % 60))
+ echo "Elapsed time: $hours hours, $minutes minutes, $seconds seconds"
+ fi
+}
+
+swstop() {
+ swshow
+ swreset
+}
+
+swstart() {
+ if [ "$sw_started" = 1 ]; then
+ printf "Stopwatch is already started. Reset? (y/n): "
+ read -r answer
+ case "$answer" in
+ [yY]) swreset ;;
+ [nN]) echo "Stopwatch not reset." && swshow && return ;;
+ *) echo "Error: Invalid input." >&2 && return 1 ;;
+ esac
+ fi
+
+ sw_started=1
+ sw_start_time=$(date +%s)
+ printf "Stopwatch started at %s\n\n" "$(date +"%H:%M:%S %p")"
+}