aboutsummaryrefslogtreecommitdiff
path: root/scripts/note
blob: 491e3dec2cce88f35aef937df5c90a4e3f54b882 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/bash

# Owner: claw0ry <claw0ry@proton.me>
# Description: Take notes using vim. Theres a lot of assumptions here, so it might not fit your setup.
# Assumptions:
#     - Your notes directory is in your $HOME
#     - Your home directory is '/home/<user>/'
#     - Your notes should be in markdown

__list_files() {
    find "$1" -mindepth 1 -type f | while read file; do
        echo "$file" | cut -d'/' -f5-
    done
}

__remove_file() {
    if [[ -f "$1" ]]; then
        rm -v "$1"
    else
        echo "ERR: file not found"
        return 1
    fi
}

__open_file() {
    # inject template if files does not already exist
    if [[ ! -f "$1" ]]; then
        local now=$(date -R)
        local templ="# TITLE\n\nDate: $now\n\n"

        # opens a new vim buffer with $templ and sets the filename to $1 without writing to it
        echo -e $templ | $EDITOR - -c ':set ft=markdown' +"file ${1}"
    else
        $EDITOR "$1"
    fi
}

__print_usage() {
    echo "usage: note [COMMAND] [<filename>]"
    echo "Commands:"
    echo "  list"
    echo "    List all notes"
    echo "  rm"
    echo "    Delete all notes"
    echo "  help"
    echo "    Prints usage"
}

note() {
    if [[ -z "$1" ]]; then
        __print_usage
        return 1
    fi

    case "$1" in
        "list")
            __list_files "$HOME/notes";;
        "rm")
            __remove_file "$HOME/notes/$2";;
        "help")
            __print_usage;;
        *)
            __open_file "$HOME/notes/$1";;
    esac
}

note "$@"