dotfiles/.config/fish/functions/pb.fish

99 lines
3.1 KiB
Fish

# This function lets the user conveniently upload a paste to sr.ht,
# and copy its url. You can also copy the url of a paste
# you've already uploaded.
function pb --description 'Upload a paste to sr.ht'
function select_paste
set -l lines (string split \n (hut paste list) | string trim | string match -rv '^$')
if test (count $lines) -le 1
false
return
end
for i in (seq 1 2 (count $lines))
set -l meta $lines[$i]
set -l filename $lines[(math $i + 1)]
set -l append "$filename: $meta"
if not set -q list
set --function list $append
else
set --function list $list $append
end
end
set -l selection (printf '%s\n' $list | fzf)
set -l hash (string split ' ' $selection)[2]
echo $hash
end
function get_user
if set -q srht_user
echo "~$srht_user"
else
string split ' ' (hut meta show)[1][1]
end
end
if test (count $argv) -lt 1
echo "Utility for working with sr.ht pastes.
All paste selection is interactive via fzf.
Use -- followed by arguments to pass directly to hut paste.
The following commands are available:
create - [file_path] create a paste
copy - copy a paste's URL to your clipboard
delete - delete a paste
show - view a paste
id - select a paste and print its id
list - list the existing pastes"
return
end
set -l cmd $argv[1]
set -l args $argv[2..]
switch $cmd
case create
if test (count $args) = 0
set output (hut paste create (fzf))
else
set output (hut paste create $args)
end
if not test $status = 0
echo 'Error creating paste!'
false
return
end
set -l url (string match -r 'https://paste\.sr\.ht/.+/[a-zA-Z0-9]+$' $output)
echo $url | wl-copy
echo "$url copied to clipboard."
case copy
set -l hash (select_paste; or 1)
if test hash = 1
echo 'Error selecting a paste!'
return 1
end
set -l url "https://paste.sr.ht/$(get_user)/$hash"
string trim $url | wl-copy
echo "$url copied to clipboard."
case delete
set -l hash (select_paste; or 1)
if test hash = 1
echo 'Error selecting a paste!'
return 1
end
hut paste delete $hash
case show
set -l hash (select_paste; or 1)
if test hash = 1
echo 'Error selecting a paste!'
return 1
end
hut paste show $hash
case id
select_paste; or return 1
case list
hut paste list
case --
hut paste $args
case '*'
echo 'Unknown argument!'
echo 'Must be one of: create, copy, delete, show, or id!'
echo 'use -- as argument to pass through to hut paste!'
return 1
end
end