Bmark / From Bash to Rust

From Bash to Rust

I have for about a year had two simple scripts, one that could create bookmarks in my file system, and another that could open a terminal in a bookmarked location selected with dmenu. The management script looked like this:

bfile=$HOME/.config/bookmarks/local-bookmarks.txt
afile=$HOME/.config/bookmarks/baliases.sh

case $1 in
    "list")
        echo "NAME - PATH" ; echo "-----------------------------" ; cat $bfile ; exit 0
        ;;
    "edit")
        $EDITOR $bfile
        bmark update_aliases
        exit 0
        ;;
    "rm")
        line=$(grep "^$2 - " $bfile) || eval 'echo "no bookmark named \"$2\"" ; exit 1'
        echo "$line"
        echo $(cat $bfile | sed "/$line/d" )
        bmark update_aliases
        exit 0
        ;;
    "update_aliases")
        echo "# Autogenerated aliases for bookmarked places" > $afile
        cat $bfile | awk -F ' - ' '{print "alias _\"" $1 "\"=\"cd " $2"\""}' >> $afile
        echo "updated bookmark aliases."
        exit 0
        ;;
    "add")
        [[ "$2" = "" ]] && echo "please give bookmark a name" && exit 1
        bmark="$2 - $(pwd)"
        echo "$bmark" >> $bfile && echo "Created bookmark: $bmark"
        bmark update_aliases
        ;;
    *)
        echo "bmark options are: add, list, edit, rm and update_aliases" && exit 0
esac

This script takes one argument, which is the action that the user wants to do. The script works by having a file called local-bookmarks.txt that stores bookmarks and their names like this:

bookmark-name - /path/to/directory
other_bookmark - /path/to/other/directory

The add command adds a bookmark to the current working directory with a given name, by appending a new line to the local-bookmarks.txt file, and the rm command removes a bookmark by name.

The list command simply outputs the content of the local-bookmarks.txt file, and the edit command opens the file in neovim.

Lastly the update_aliases command, creates a shell script called baliases.sh that defines an alias for each bookmark. This enables navigation to bookmarked location like this:

_bookmark-name

It is very simple, but it has been SO useful!

The second script is even simpler:

places="$HOME/.config/bookmarks/local-bookmarks.txt"
cat $places | dmenu -i -c -l 10 | awk -F ' - ' '{print $2}' | xargs -I{} kitty {}

It opens dmenu with a list of bookmarked locations, after selecting one a kitty-terminal window is opened at the bookmarked location.

It seems like such a small thing, but this is just SO NICE! I have bound this script to META+E, and it is a joy to use. The fact that you can open a terminal right where you need it, and not have to move around with cd every time, removes a lot of friction in my terminal based workflow.

If it ain’t broke, don’t fix it

…well i fixed it.

After getting a new computer, i wanted to create something that was more generally usable and easy to install. This was a totally arbitrary goal, and was mostly just an excuse to try programming something in Rust. I had looked at rust before, but never made anything useful, therefore i thought that this would be the perfect project, as it was a very simple program. Another thing that appealed to me, was the ability install my tool with cargo:

cargo install bmark

Feature Creep

I quickly created a working version for my setup, but i felt that i should make it work for everyone, as it now existed on crates.io for everyone to see. If someone was to find it useful i would like for them to just be able to install bmark and use it. To achieve this i introduced a configuration file with options for changing the behavior of bmark. At this point i have six options, but i have plans for more.

And that is how a two simple bash scripts becomes a 500 line Rust program in a few days…

I am sure that i am doing many things wrong when writing Rust because i am very new to it, but it will hopefully improve as i get better and better at Rust. For now i am just having fun with it:)