homeblogs about

note-taking

HOW TO CREATE A TODO NOTE CLI APP WITH BASH

2024-05-11 12:23PM #fzf #bash #cli

A TODO app, the most 'basic' app we (as dev) always build, or found on Tutorials when we're tryingto learn a new stack or a new programming language (after a 'hello world' ofc).

The most simple app that covers a lot in terms of dealing memory, manipulating basic data-structure and store data.

REQUIREMENTS

THE NOTE CLI

The bash script is straighforward and is basically easy to understand what it does.


note(){
    local code_editor="nvim"
    local notes_dir=$HOME/notes/
    local content_view="cat $notes_dir/{1}-notes.md"
    local today_note_file="$notes_dir/$(date '+%Y-%m-%d')-notes.md"
    local enter_command="$code_editor $notes_dir/{1}-notes.md"

    function _grep_filter(){
        cat $notes_dir* | grep "$@"
    }
    function _ag_filter(){
        ag "$@" $notes_diraa
    }

    # We create the notes directory if it doesn't exist
    if [ ! -d "$notes_dir" ]; then mkdir -p $notes_dir; fi;

    # We browse context (topic) elements if only one argument is passed
    if [ "$#" = "1" ]; then ls $notes_dir | _grep_filter $1 ; fi;

    # We browse the content of notes if no arguments are passed
    if [ -z $1 ]; then
        # if fzf is installed, use it as a live browser, otherwise,
        # cat the list of note from today
        if [! command -v fzf &> /dev/null ]; then cat $today_note_file; else
            ls $notes_dir | sed 's/-notes.md//g' | fzf --header "NOTES LIST" \
                --preview "${content_view}" --preview-window "right:98" \
                --bind "enter:execute:${enter_command}" \
                --bind "ctrl-d:preview-down,ctrl-u:preview-up" \
                --tac; # for the reverse order
        fi
    else
        local content_message="${@:2}"
        # We only save a > 3 note
        if [ ${#content_message} -ge 3 ]; then
            echo -e "- **$(date '+%H:%M:%S')** > [$1] $content_message \n" >> $today_note_file;
        fi;
    fi
}

Example of Uses cases :

# the fist arg been the topic by default.
$ note topic note to my self... # will add a new note for the <topic>
$ note topic # will seach for notes on this <topic>
$ note # will list for you all notes taken globally using fzf

demo-gif


<< blogs


githublinkedintwittertelegramemail