home/.zshc/func.zsh

135 lines
4.1 KiB
Bash

function clip {
if [[ $(uname) == "Darwin" ]];then
command pbcopy $@
elif (( "${XDG_SESSION_TYPE}" == "x11" )); then
if (( $+commands[xsel] )); then
command xsel -ib $@
elif (( $+commands[xclip] )); then
command xclip $@
else
echo "Detected X11, but xsel or xclip are not installed."
fi
elif (( -n {WAYLAND_DISPLAY} )); then
if (( $+commands[wl-copy] )); then
command wl-copy $@
else
echo "Detected Wayland, but wl-copy is not installed."
fi
elif (( -n {SSH_CONNECTION} )); then
if (( $+commands[lemonade] )); then
command lemonade copy $@
else
echo "Detected an SSH connection, but lemonade is not installed."
fi
else
echo "Cannot determine desktop session type, are you running a display server that is not X11 or Wayland?"
fi
}
function zipline {
# see spectacle-screenshot for example usage
readonly file=${1:?Please provide the path to the file you want to upload.}
readonly name=${2:?Please provide an application name to use in notify-send.}
readonly desktopentry=${3:?Please provide a path to a desktop entry file.}
readonly token=${ZIPLINE_TOKEN:?Environment variable ZIPLINE_TOKEN is not set. Please set it to your Zipline API token. I recommend placing it in a file at ~/.config/environment.d/zipline.conf}
readonly url=${ZIPLINE_URL:?Environment variable ZIPLINE_URL is not set. Please set it to the URL of your Zipline instance. I recommend placing it in a file at ~/.config/environment.d/zipline.conf}
temp_file=$(mktemp)
trap 'rm -f "$temp_file"' EXIT
if (( ! $+commands[jq] )); then
echo "jq is not installed."
return 1
fi
if (( ! $+commands[curl] )); then
echo "curl is not installed."
return 1
fi
if (( ! $+commands[notify-send] )); then
echo "notify-send is not installed."
return 1
fi
if [[ ! -f "${file}" ]]; then
echo "File does not exist."
return 1
fi
if ! does-desktop-entry-exist "$desktopentry"; then
echo "Desktop entry does not exist."
return 1
fi
if curl -sS -fH "authorization: $token" "${url%/}/api/upload" -F file="@$file" -H "Content-Type: multipart/form-data" >"$temp_file" 2>&1; then
readonly link=$(jq -r '.files[0]' <"$temp_file" | tr -d '\n')
echo -n "$link" | clip
echo "Upload successful. Link copied to clipboard: $link"
notify-send -a "$name" -u low -c transfer.complete -i "$file" -h "string:desktop-entry:$desktopentry" "Upload Successful" "Link copied to clipboard:\n$link"
else
echo "Upload failed. Error message: $(<"$temp_file")"
notify-send -a "$name" -u critical -c transfer.error -h "string:desktop-entry:$desktopentry" "Upload Failed" "$(<"$temp_file")"
return 1
fi
}
function spectacle-screenshot {
# takes one argument, the path to save the screenshot to - if not provided, a temp file will be used
if (( ! $+commands[spectacle] )); then
echo "spectacle is not installed."
return 1
fi
if [[ -z "${1}" ]]; then
readonly file=$(mktemp /tmp/spectacle-screenshot.XXXXXXXX.png)
else
readonly file=$1
fi
spectacle --nonotify --background --region --copy-image --output "$file"
[ ! -s "$file" ] && exit 1
trap 'rm -f "$file"' EXIT
zipline "$file" Spectacle org.kde.spectacle.desktop
}
function does-desktop-entry-exist {
readonly desktopentry=${1:?Please provide the full filename of the desktop entry.}
local entrypaths=(
"/usr/share/applications"
"/usr/local/share/applications"
"$HOME/.local/share/applications"
)
for entrypath in "${entrypaths[@]}"; do
if [[ -f "$entrypath/$desktopentry" ]]; then
return 0
fi
done
echo "Desktop entry $desktopentry does not exist."
return 1
}
function htop btop {
if (( $+commands[btop] )); then
command btop $@
else
command htop $@
fi
}
function fastfetch ff neofetch nf {
if (( $+commands[fastfetch] )); then
command fastfetch $@
elif (( $+commands[neofetch] )); then
echo "Warning: fastfetch is not installed, falling back to neofetch."
command neofetch $@
else
echo "Warning: fastfetch and neofetch are not installed, cannot fetch system information."
fi
}