home/.zshc/func.zsh
2024-10-22 13:57:37 -04:00

212 lines
6.5 KiB
Bash
Executable file

function clip {
echo "clip: info: checking for clipboard manager"
if [[ $(uname) == "Darwin" ]];then
echo "Detected macOS"
command pbcopy $@
elif [[ "${XDG_SESSION_TYPE}" == "x11" ]]; then
echo "clip: info: Detected X11"
if (( $+commands[xsel] )); then
command xsel -ib $@
elif (( $+commands[xclip] )); then
command xclip $@
else
echo "clip: fatal: xsel or xclip are not installed."
fi
elif [[ -n "${WAYLAND_DISPLAY}" ]]; then
echo "clip: info: Detected Wayland"
if (( $+commands[wl-copy] )); then
command wl-copy $@
else
echo "clip: fatal: wl-copy is not installed."
fi
elif [[ -n "${SSH_CONNECTION}" ]]; then
echo "clip: info: Detected an SSH connection"
if (( $+commands[lemonade] )); then
command lemonade copy $@
else
echo "clip: fatal: lemonade is not installed."
fi
else
echo "clip: fatal: Cannot determine desktop session type, are you running a display server that is not X11 or Wayland?"
fi
}
function zipline {
# see spectacle-screenshot and flameshot-screenshot for example usage
readonly file_path=${1:?Please provide the path to the file you want to upload.}
readonly name=${2}
readonly desktop_entry=${3}
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 /tmp/zipline-upload.XXXXXXXX.json)
trap 'rm -f "$temp_file"' ERR
if (( ! $+commands[jq] )); then
echo "jq is not installed."
return 127
fi
if (( ! $+commands[curl] )); then
echo "curl is not installed."
return 127
fi
if [[ ! -f "${file_path}" ]]; then
echo "File at $file_path does not exist."
return 66
fi
if [[ -n "${name}" && -n "${desktop_entry}" ]]; then
if ! does-desktop-entry-exist "$desktop_entry"; then
echo "Desktop entry does not exist."
return 66
fi
if (( ! $+commands[notify-send] )); then
echo "notify-send is not installed."
return 127
fi
readonly use_send_notify=true
else
readonly use_send_notify=false
fi
readonly content_type=$(command file -b --mime-type "${file_path}")
if curl -sS -fH "authorization: $token" "${url%/}/api/upload" -F"file=@$file_path;type=$content_type" >"$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"
if $use_send_notify; then
notify-send -a "$name" -u low -c "transfer.complete" -i "$file_path" -h "string:desktop-entry:$desktop_entry" "Upload Successful" "Link copied to clipboard:\n$link"
fi
rm -f "$temp_file"
else
echo "Upload failed. Error message: $(<"$temp_file")"
if $use_send_notify; then
notify-send -a "$name" -u critical -c "transfer.error" -h "string:desktop-entry:$desktop_entry" "Upload Failed" "$(<"$temp_file")"
fi
rm -f "$temp_file"
return 1
fi
}
function spectacle-screenshot {
# takes two arguments:
# 1 - whether to save a recording or a screenshot (boolean) - defaults to screenshot (false) - RECORDING DOES NOT WORK ON X11
# 2 - the path to save the screenshot/recording to - if not provided, a temp file will be used
if (( ! $+commands[spectacle] )); then
echo "spectacle is not installed."
return 127
fi
if [[ -z "${1}" ]]; then
readonly save_recording=false
else
readonly save_recording=$1
fi
if [[ -f "${2}" ]]; then
echo "File already exists. Please provide a different file path, or use the zipline function to upload the file."
echo "Example: zipline \"$file\""
return 1
fi
if [[ -z "${2}" ]]; then
if $save_recording; then
readonly file=$(mktemp /tmp/spectacle-recording.XXXXXXXX.webm)
else
readonly file=$(mktemp /tmp/spectacle-screenshot.XXXXXXXX.png)
fi
else
readonly file=$2
fi
trap 'rm -f "$file"' ERR
if $save_recording; then
command spectacle --nonotify --background --record=region --copy-image --output "$file"
else
command spectacle --nonotify --background --region --copy-image --output "$file"
fi
[ ! -s "$file" ] && return 1
zipline "$file" Spectacle org.kde.spectacle.desktop
rm -f "$file"
}
function flameshot-screenshot {
# takes one argument, the path to save the screenshot to - if not provided, a temp file will be used
if (( ! $+commands[flameshot] )); then
echo "flameshot is not installed."
return 127
fi
if [[ -f "${1}" ]]; then
echo "File already exists. Please provide a different file path, or use the zipline function to upload the file."
echo "Example: zipline \"${1}\""
return 1
fi
if [[ -z "${1}" ]]; then
readonly file=$(mktemp /tmp/flameshot-screenshot.XXXXXXXX.png)
else
readonly file=$1
fi
trap 'rm -f "$file"' ERR
command flameshot gui --clipboard --raw > $file
echo "Screenshot saved to $file"
[ ! -s "$file" ] && return 1
zipline "$file" Flameshot org.flameshot.Flameshot.desktop
rm -f "$file"
}
function does-desktop-entry-exist {
readonly desktopentry=${1:?Please provide the full filename of the desktop entry.}
if (( $+commands[qtpaths] )); then
local entrypaths=("${(@f)$(qtpaths --paths ApplicationsLocation | tr ':' '\n')}")
else
echo "qtpaths is not installed, falling back to XDG_DATA_DIRS."
local entrypaths=(${(f)"$(echo $XDG_DATA_DIRS | tr ':' '\n' | sed 's|$|/applications|')"})
entrypaths+=("$HOME/.local/share/applications")
fi
echo "Checking the following paths for $desktopentry:\n${(F)entrypaths}\n--------------------"
for entrypath in "${entrypaths[@]}"; do
echo "Checking for $entrypath/$desktopentry"
if [[ -f "$entrypath/$desktopentry" ]]; then
echo "$desktopentry found in $entrypath"
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 "Fatal: fastfetch and neofetch are not installed, cannot fetch system information."
fi
}