Created
January 1, 2023 00:52
-
-
Save apainintheneck/e43fbcf078097667213d1c8dfe39b21c to your computer and use it in GitHub Desktop.
bdir - bookmark directories using the fish shell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function bdir --description 'bookmark directories' | |
if test (count $argv) = 0 | |
set --function no_args 1 | |
end | |
set --local options --exclusive 's,d,f,l,h' --max-args=1 | |
set --append options 's/save=' 'd/delete=' 'f/find=' l/list h/help | |
argparse --name bdir $options -- $argv | |
or return | |
# Show help page | |
if set --query _flag_help; or set --query no_args | |
echo "[bdir]" | |
echo "------" | |
echo "Bookmark directories for easy access in the future." | |
echo | |
echo "Options" | |
echo "-------" | |
echo "<bookmark>" | |
echo " : Move to bookmarked directory" | |
echo "-l/--list" | |
echo " : List bookmarks" | |
echo "-f/--find <bookmark>" | |
echo " : Find a bookmark by name" | |
echo "-s/--save <bookmark>" | |
echo " : Save the current directory as a bookmark" | |
echo "-d/--delete <bookmark>" | |
echo " : Delete a bookmark by name" | |
echo "-h/--help" | |
echo " : Display this page" | |
return 0 | |
end | |
# Read in bookmarks | |
set --local bdir_file "$HOME/.bdir_store" | |
set --local name_list | |
set --local path_list | |
if test -f $bdir_file | |
while read --delimiter '=' --local name path | |
if set --query name; and set --query path | |
set --append name_list $name | |
set --append path_list $path | |
end | |
end < $bdir_file | |
end | |
# Execute command | |
if set --query _flag_save | |
if not string match --quiet --regex '^[A-Za-z0-9_-]+$' $_flag_save | |
echo "Invalid bookmark name: $_flag_save" | |
echo "Valid names must only include the following:" | |
echo " - English letters (uppercase or lowercase)" | |
echo " - Digits 0-9" | |
echo " - Underscores and dashes" | |
return 1 | |
end | |
if contains $_flag_save $name_list | |
set --local idx (contains --index $_flag_save $name_list) | |
set --erase name_list[$idx] path_list[$idx] | |
end | |
set --prepend name_list $_flag_save | |
set --prepend path_list $PWD | |
else if set --query _flag_delete | |
if contains $_flag_delete $name_list | |
set --local idx (contains --index $_flag_delete $name_list) | |
set --erase name_list[$idx] path_list[$idx] | |
else | |
echo "No bookmark with the name: $_flag_delete" | |
return 1 | |
end | |
else if set --query _flag_find | |
if contains $_flag_find $name_list | |
set --local idx (contains --index $_flag_find $name_list) | |
echo "$name_list[$idx] -> $path_list[$idx]" | |
return 0 | |
else | |
echo "No bookmark with the name: $_flag_find" | |
return 1 | |
end | |
else if set --query _flag_list | |
set --local bookmark_count (count $name_list) | |
if test $bookmark_count = 0 | |
echo "No bookmarks to list" | |
else | |
for i in (seq $bookmark_count) | |
echo "$i) $name_list[$i] -> $path_list[$i]" | |
end | |
end | |
return 0 | |
else | |
if contains $argv[1] $name_list | |
set --local idx (contains --index $argv[1] $name_list) | |
# Move bookmark to the front of the list | |
set --local name $name_list[$idx] | |
set --local path $path_list[$idx] | |
set --erase name_list[$idx] | |
set --erase path_list[$idx] | |
set --prepend name_list $name | |
set --prepend path_list $path | |
# Set new directory to move to | |
set --function new_directory $path | |
else | |
echo "No bookmark with the name: $argv[1]" | |
return 1 | |
end | |
end | |
# Save bookmarks | |
set --local bookmark_count (count $name_list) | |
set --local backup_file "$bdir_file.bak" | |
rm -f $backup_file | |
if test $bookmark_count -gt 0 | |
for i in (seq $bookmark_count) | |
echo "$name_list[$i]=$path_list[$i]" >> $backup_file | |
end | |
else | |
touch $backup_file | |
end | |
mv -f $backup_file $bdir_file | |
# Move to new directory | |
if set --query new_directory | |
cd $new_directory | |
return $status | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment