dotfiles/zsh/.config/zsh/functions.zsh
2025-05-05 14:36:02 -03:00

298 lines
7.1 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Lazy load zoxide
z() {
# Remove the placeholder function
unfunction "$0"
# Load zoxide
eval "$(zoxide init zsh)"
# Execute the command
$0 "$@"
}
# Ensure we have all the zoxide command variants covered
zi() { z "$@" }
za() { z "$@" }
zrm() { zoxide remove "$@" }
function _package_manager {
local pkg_manager=""
if [[ -f bun.lockb ]]; then
pkg_manager="bun"
elif [[ -f pnpm-lock.yaml ]]; then
pkg_manager="pnpm"
elif [[ -f yarn.lock ]]; then
pkg_manager="yarn"
elif [[ -f package-lock.json ]]; then
pkg_manager="npm"
else
pkg_manager="pnpm"
fi
# Check if the first argument is a script in package.json
if [[ -f package.json ]] && [[ $1 != "run" ]] && [[ $1 != "install" ]] && [[ $1 != "add" ]] && [[ $1 != "remove" ]] && [[ $1 != "i" ]] && [[ $1 != "rm" ]]; then
# Check if the command exists in package.json scripts using jq
if jq -e ".scripts[\"$1\"] != null" package.json >/dev/null 2>&1; then
set -- "run" "$@"
fi
fi
# Use corepack when available
if command -v corepack >/dev/null 2>&1; then
corepack ${pkg_manager} "$@"
else
command ${pkg_manager} "$@"
fi
}
yarn() { echo 🖕; }
yarnpkg() { echo 🖕; }
pnpm() { echo 🖕; }
pn() { echo 🖕; }
pnpx() { echo 🖕; }
alias p='_package_manager'
alias unChonk="echo '🧹 Starting the great node_modules purge...' && find . -name 'node_modules' -type d -prune -exec sh -c 'echo \"💥 Deleting {}\" && rm -rf \"{}\"' \;"
# Open files using system default application
function open {
for i in $*
do
setsid nohup xdg-open $i > /dev/null 2> /dev/null
done
}
function upload_file() {
if [[ -z "$1" ]]; then
echo "Usage: $0 <file>"
return 1
fi
if [[ ! -f "$1" ]]; then
echo "Error: File not found: $1"
return 1
fi
# local url="https://dump.mz.uy"
local url="http://localhost:8080"
echo "Uploading $1 to $url..."
local full_response=$(curl -i -F"file=@$1" -F"one_time=" "$url")
local url_response=$(echo "$full_response" | tail -n 1)
local token=$(echo "$full_response" | grep -i "X-Token:" | awk '{print $2}' | tr -d '\r')
if [[ -n "$url_response" ]]; then
echo -n "$url_response" | wl-copy # Wayland
echo "✓ Uploaded: $url_response (copied to clipboard)"
if [[ -n "$token" ]]; then
echo "Management token: $token"
fi
else
echo "× Failed to upload file"
return 1
fi
}
# Create both commands as aliases to the same function
alias drop='upload_file'
alias dump='upload_file'
# Wacom tablet configuration
function wacom {
if [ "$XDG_SESSION_TYPE" = "wayland" ]; then
systemctl --user enable opentabletdriver --now
otd loadsettings ~/Sync/wacom/wacom.json
else
# Reset area and map the stylus to DisplayPort-0, then rotate
xsetwacom --set "Wacom One by Wacom S Pen stylus" ResetArea
xsetwacom --set "Wacom One by Wacom S Pen stylus" MapToOutput DisplayPort-0
xsetwacom --set "Wacom One by Wacom S Pen stylus" Rotate half
# Reset area and map the eraser to DisplayPort-0
xsetwacom --set "Wacom One by Wacom S Pen eraser" ResetArea
xsetwacom --set "Wacom One by Wacom S Pen eraser" MapToOutput DisplayPort-0
fi
}
# Enhanced git clone function
function gc {
if [[ "$1" == */* ]]
then
git clone "https://github.com/$1" "${@:2}"
else
git clone "https://github.com/marianozunino/$1" "${@:2}"
fi
}
# Improved file sharing with croc
function croc-send {
croc send --code mzunino "$@"
}
function croc-receive {
croc --yes mzunino
}
# Fix swaymsg when inside tmux
if [[ -v TMUX ]]; then
function swaymsg {
export SWAYSOCK=$XDG_RUNTIME_DIR/sway-ipc.$UID.$(pgrep -x sway).sock
command swaymsg "$@"
}
fi
# Expose local port to internet
function expose() {
if [ -z "$1" ]; then
echo "Usage: expose <port>"
return
fi
ssh marianozunino@srv.us -R 1:localhost:$1
}
# Helper function for command existence check
function _has {
return $( whence $1 >/dev/null )
}
# Sublime merge shortcut
function sm {
/opt/sublime_merge/sublime_merge $1
}
# Edit nvim config
function vimrc {
cd $XDG_CONFIG_HOME/nvim
nvim
cd -
}
# Edit zsh config
function zshrc {
cd $XDG_CONFIG_HOME/zsh
nvim
cd -
}
# Find process using a port
function ppid {
lsof -i :$1
}
alias pport=ppid
# Kubernetes port forwarding helper
function kf {
if [ -z "$1" ]; then
echo "Usage: kf <cluster> [service-name]"
return
fi
local namespace="oc-app"
local cluster="oc-$1-eks-cluster"
local svc_filter=""
if [ ! -z "$2" ]; then
svc_filter="-l app.kubernetes.io/instance=$2"
fi
sudo -E kubefwd svc -n ${namespace} -x ${cluster} ${svc_filter}
}
function cat {
if [ -n "$SSH_TTY" ] || [ -n "$SSH_CLIENT" ] || [ -n "$SSH_CONNECTION" ]; then
bat --plain --paging=never "$@"
else
bat "$@"
fi
}
# Enhanced PrivateBin sharing function
function pb {
local usage="Usage: pb [-b] [-e <expiration>] <file>"
local burn_after_reading=false
local expiration="1week"
while getopts ":be:" opt; do
case ${opt} in
b )
burn_after_reading=true
;;
e )
expiration=$OPTARG
;;
\? )
echo "Invalid option: -$OPTARG" >&2
echo $usage
return 1
;;
: )
echo "Option -$OPTARG requires an argument." >&2
echo $usage
return 1
;;
esac
done
shift $((OPTIND -1))
if [ $# -eq 0 ]; then
echo $usage
return 1
fi
local file=$1
if [ ! -f "$file" ]; then
echo "Error: File '$file' not found." >&2
return 1
fi
# Check for required commands
for cmd in privatebin wl-copy; do
if ! command -v $cmd &> /dev/null; then
echo "Error: '$cmd' command not found. Please install it." >&2
return 1
fi
done
# Prepare the command
local cmd="privatebin create --filename $file --expire $expiration"
if $burn_after_reading; then
cmd="$cmd --burn-after-reading"
fi
# Run command and handle result
local result
result=$(eval "$cmd" 2>&1)
if [ $? -ne 0 ]; then
echo "Error: Failed to upload file to PrivateBin. Details:" >&2
echo "$result" >&2
return 1
fi
echo $result
echo "$result" | wl-copy
echo "PrivateBin link copied to clipboard."
notify-send "PrivateBin" "Link copied to clipboard" 2>/dev/null || true
}
# Load autocomplete files
for completion_file in ~/.local/share/zsh/*-autocomplete.zsh; do
if [ -f "$completion_file" ]; then
source "$completion_file"
fi
done
# Initialize completions for common tools
if command -v rop &> /dev/null; then eval "$(rop completion zsh)"; fi
if command -v goq &> /dev/null; then eval "$(goq completion zsh)"; fi
if command -v kubefwd &> /dev/null; then eval "$(kubefwd completion zsh)"; fi
if command -v bombadil &> /dev/null; then eval "$(bombadil generate-completions zsh)"; fi
if command -v eza &> /dev/null; then compdef eza=ls; fi
function dc() {
git add .
git commit -m 'automated dev commit' --no-gpg-sign
git push
}