2024-10-11 18:33:12 -04:00
function clip {
2024-10-16 14:30:01 -04:00
echo "clip: info: checking for clipboard manager"
2024-10-11 18:33:12 -04:00
if [ [ $( uname) = = "Darwin" ] ] ; then
2024-10-16 14:30:01 -04:00
echo "Detected macOS"
2024-10-11 18:33:12 -04:00
command pbcopy $@
2024-10-11 18:45:13 -04:00
elif [ [ " ${ XDG_SESSION_TYPE } " = = "x11" ] ] ; then
2024-10-16 14:30:01 -04:00
echo "clip: info: Detected X11"
2024-10-11 18:45:13 -04:00
if ( ( $+commands[ xsel] ) ) ; then
command xsel -ib $@
2024-10-11 18:33:12 -04:00
elif ( ( $+commands[ xclip] ) ) ; then
2024-10-11 18:45:13 -04:00
command xclip $@
else
2024-10-16 14:30:01 -04:00
echo "clip: fatal: xsel or xclip are not installed."
2024-10-11 18:45:13 -04:00
fi
2024-10-11 18:33:12 -04:00
2024-10-11 18:45:13 -04:00
elif [ [ -n " ${ WAYLAND_DISPLAY } " ] ] ; then
2024-10-16 14:30:01 -04:00
echo "clip: info: Detected Wayland"
2024-10-11 18:45:13 -04:00
if ( ( $+commands[ wl-copy] ) ) ; then
command wl-copy $@
else
2024-10-16 14:30:01 -04:00
echo "clip: fatal: wl-copy is not installed."
2024-10-11 18:45:13 -04:00
fi
2024-10-11 18:33:12 -04:00
2024-10-11 18:45:13 -04:00
elif [ [ -n " ${ SSH_CONNECTION } " ] ] ; then
2024-10-16 14:30:01 -04:00
echo "clip: info: Detected an SSH connection"
2024-10-11 18:45:13 -04:00
if ( ( $+commands[ lemonade] ) ) ; then
command lemonade copy $@
else
2024-10-16 14:30:01 -04:00
echo "clip: fatal: lemonade is not installed."
2024-10-11 18:45:13 -04:00
fi
2024-10-11 18:33:12 -04:00
else
2024-10-16 14:30:01 -04:00
echo "clip: fatal: Cannot determine desktop session type, are you running a display server that is not X11 or Wayland?"
2024-10-11 18:33:12 -04:00
fi
}
function zipline {
2024-10-11 23:24:25 -04:00
# see spectacle-screenshot and flameshot-screenshot for example usage
2024-10-11 18:33:12 -04:00
2024-10-16 14:24:33 -04:00
readonly file_path = ${ 1 : ?Please provide the path to the file you want to upload. }
2024-10-11 23:24:25 -04:00
readonly name = ${ 2 }
2024-10-16 14:24:33 -04:00
readonly desktop_entry = ${ 3 }
2024-10-11 18:33:12 -04:00
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 }
2024-10-11 23:24:25 -04:00
temp_file = $( mktemp /tmp/zipline-upload.XXXXXXXX.json)
2024-10-12 00:05:48 -04:00
trap 'rm -f "$temp_file"' ERR
2024-10-11 18:33:12 -04:00
if ( ( ! $+commands[ jq] ) ) ; then
echo "jq is not installed."
2024-10-11 23:24:25 -04:00
return 127
2024-10-11 18:33:12 -04:00
fi
if ( ( ! $+commands[ curl] ) ) ; then
echo "curl is not installed."
2024-10-11 23:24:25 -04:00
return 127
2024-10-11 18:33:12 -04:00
fi
2024-10-16 14:24:33 -04:00
if [ [ ! -f " ${ file_path } " ] ] ; then
echo " File at $file_path does not exist. "
2024-10-11 23:24:25 -04:00
return 66
2024-10-11 18:33:12 -04:00
fi
2024-10-16 14:24:33 -04:00
if [ [ -n " ${ name } " && -n " ${ desktop_entry } " ] ] ; then
if ! does-desktop-entry-exist " $desktop_entry " ; then
2024-10-11 23:24:25 -04:00
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
2024-10-11 18:33:12 -04:00
fi
2024-10-16 14:24:33 -04:00
readonly content_type = $( command file -b --mime-type " ${ file_path } " )
2024-10-16 14:07:18 -04:00
2024-10-16 14:24:33 -04:00
if curl -sS -fH " authorization: $token " " ${ url %/ } /api/upload " -F" file=@ $file_path ;type= $content_type " >" $temp_file " 2>& 1; then
2024-10-11 18:33:12 -04:00
readonly link = $( jq -r '.files[0]' <" $temp_file " | tr -d '\n' )
echo -n " $link " | clip
echo " Upload successful. Link copied to clipboard: $link "
2024-10-11 23:24:25 -04:00
if $use_send_notify ; then
2024-10-16 14:24:33 -04:00
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 "
2024-10-11 23:24:25 -04:00
fi
2024-10-12 00:06:59 -04:00
rm -f " $temp_file "
2024-10-11 18:33:12 -04:00
else
echo " Upload failed. Error message: $( <" $temp_file " ) "
2024-10-11 23:24:25 -04:00
if $use_send_notify ; then
2024-10-16 14:24:33 -04:00
notify-send -a " $name " -u critical -c "transfer.error" -h " string:desktop-entry: $desktop_entry " "Upload Failed" " $( <" $temp_file " ) "
2024-10-11 23:24:25 -04:00
fi
2024-10-12 00:06:59 -04:00
rm -f " $temp_file "
2024-10-11 18:33:12 -04:00
return 1
fi
}
function spectacle-screenshot {
2024-10-11 23:38:13 -04:00
# 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
2024-10-11 18:33:12 -04:00
if ( ( ! $+commands[ spectacle] ) ) ; then
echo "spectacle is not installed."
2024-10-11 23:24:25 -04:00
return 127
fi
2024-10-11 23:38:13 -04:00
if [ [ -z " ${ 1 } " ] ] ; then
readonly save_recording = false
else
readonly save_recording = $1
fi
if [ [ -f " ${ 2 } " ] ] ; then
2024-10-11 23:24:25 -04:00
echo "File already exists. Please provide a different file path, or use the zipline function to upload the file."
echo " Example: zipline \" $file \" "
2024-10-11 18:33:12 -04:00
return 1
fi
2024-10-11 23:38:13 -04:00
if [ [ -z " ${ 2 } " ] ] ; then
if $save_recording ; then
2024-11-04 11:56:17 -05:00
readonly file = $( mktemp /tmp/spectacle-recording.XXXXXXXX.mp4)
2024-10-11 23:38:13 -04:00
else
readonly file = $( mktemp /tmp/spectacle-screenshot.XXXXXXXX.png)
fi
2024-10-11 18:33:12 -04:00
else
2024-10-11 23:38:13 -04:00
readonly file = $2
2024-10-11 18:33:12 -04:00
fi
2024-10-12 00:05:48 -04:00
trap 'rm -f "$file"' ERR
2024-10-11 18:33:12 -04:00
2024-10-11 23:38:13 -04:00
if $save_recording ; then
2024-10-22 21:39:31 -04:00
command spectacle --nonotify --background --record= region --pointer --copy-image --output " $file "
2024-10-11 23:38:13 -04:00
else
2024-10-22 21:39:31 -04:00
command spectacle --nonotify --background --region --pointer --copy-image --output " $file "
2024-10-11 23:38:13 -04:00
fi
2024-10-11 23:24:25 -04:00
[ ! -s " $file " ] && return 1
2024-10-11 18:33:12 -04:00
2024-10-16 14:24:33 -04:00
zipline " $file " Spectacle org.kde.spectacle.desktop
2024-10-12 00:05:48 -04:00
rm -f " $file "
2024-10-11 18:33:12 -04:00
}
2024-10-11 23:24:25 -04:00
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
2024-10-12 00:05:48 -04:00
trap 'rm -f "$file"' ERR
2024-10-11 23:24:25 -04:00
command flameshot gui --clipboard --raw > $file
echo " Screenshot saved to $file "
[ ! -s " $file " ] && return 1
2024-10-16 14:24:33 -04:00
zipline " $file " Flameshot org.flameshot.Flameshot.desktop
2024-10-12 00:05:48 -04:00
rm -f " $file "
2024-10-11 23:24:25 -04:00
}
2024-10-11 18:33:12 -04:00
function does-desktop-entry-exist {
readonly desktopentry = ${ 1 : ?Please provide the full filename of the desktop entry. }
2024-10-14 13:37:00 -04:00
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-------------------- "
2024-10-11 18:33:12 -04:00
for entrypath in " ${ entrypaths [@] } " ; do
2024-10-14 13:37:00 -04:00
echo " Checking for $entrypath / $desktopentry "
2024-10-11 18:33:12 -04:00
if [ [ -f " $entrypath / $desktopentry " ] ] ; then
2024-10-14 13:37:00 -04:00
echo " $desktopentry found in $entrypath "
2024-10-11 18:33:12 -04:00
return 0
fi
done
2024-10-14 13:37:00 -04:00
2024-10-11 18:33:12 -04:00
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 $@
2024-10-11 18:35:38 -04:00
elif ( ( $+commands[ neofetch] ) ) ; then
2024-10-11 18:33:12 -04:00
echo "Warning: fastfetch is not installed, falling back to neofetch."
command neofetch $@
else
2024-10-11 23:24:25 -04:00
echo "Fatal: fastfetch and neofetch are not installed, cannot fetch system information."
2024-10-11 18:33:12 -04:00
fi
}