~gpanders/dotfiles

ref: c8e63bac959db19c138dcc3198a56514f0b444ff dotfiles/.local/bin/note -rwxr-xr-x 4.7 KiB
c8e63bacGreg Anders Manage dotfiles directly in home directory 1 year, 3 months ago
                                                                                
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env bash
# Simple bash script for creating and maintaining plaintext notes

if [ -f "${XDG_CONFIG_HOME:-$HOME/.config}"/note/config ]; then
    source "${XDG_CONFIG_HOME:-$HOME/.config}"/note/config
elif [ -z "$NOTES_DIR" ]; then
    NOTES_DIR=${XDG_DATA_HOME:-$HOME/.local/share}/note
fi

if [ -z "$NOTES_DIR" ]; then
    echo "Notes directory not set!" >&2
    return 1
fi

if [ ! -d "$NOTES_DIR" ]; then
    mkdir -p "$NOTES_DIR"
fi

usage() {
    echo "Usage:
    note s[how] <name>
    note o[pen] <name>
    note n[ew] <name>
    note l[ist]
    note g[rep] <pattern>
    note t[ag] [tag]
    note h[elp]
    note p[review]"
}

note() {
    if [ $# -lt 1 ]; then
        usage >&2
        return 1
    fi

    case "$1" in
        h|he|hel|help)
            usage
            return 0
            ;;
        s|sh|sho|show)
            if [ $# -lt 2 ]; then
                usage >&2
                return 1
            fi

            local note
            note="$NOTES_DIR"/"${2%.txt}".txt
            if [ ! -f "$note" ]; then
                echo "Note '$2' not found" >&2
                return 1
            fi
            cat "$note"
            ;;
        o|op|ope|open)
            cd "$NOTES_DIR" || return 1
            if [ $# -lt 2 ]; then
                ${EDITOR:-vi}
            else
                shift
                local notes
                notes=()
                for note in "$@"; do
                    notes+=("$NOTES_DIR"/"${note%.txt}".txt)
                done

                ${EDITOR:-vi} "${notes[@]}"
            fi
            ;;
        n|ne|new)
            if [ $# -lt 2 ]; then
                usage >&2
                return 1
            fi

            shift
            local title date fname file
            title="$*"
            date=$(date +%Y%m%d%H%M%S)
            fname="${date} $(sed -e 's/[^[:alnum:]]/_/g' -e 's/^_\|_$//' <<< "$*" | tr -s '_')"
            file="$NOTES_DIR"/"${fname//_/ }".txt
            cat > "$file" << EOF
---
title:      $title
date:       $(date +'%B %d, %Y')
keywords:
...
EOF
            cd "$NOTES_DIR" || return 1
            ${EDITOR:-vi} "$file"
            ;;
        l|li|lis|list)
            cd "$NOTES_DIR" || return 1
            readarray -t notes < <(ls)
            if [ ${#notes} -eq 0 ]; then
                echo "You have no notes"
                return 0
            fi

            for note in "${notes[@]}"; do
                echo "${note%.*}"
            done | sort | less -R --no-init --quit-if-one-screen
            ;;
        g|gr|gre|grep)
            if [ $# -lt 2 ]; then
                usage >&2
                return 1
            fi

            shift
            cd "$NOTES_DIR" || return 1
            readarray -t files < <(ls)
            for pat in "$@"; do
                readarray -t files < <({ grep -Fil "$pat" "${files[@]}" ; printf "%s\n" "${files[@]}" | grep -Fi "$pat" ; })
                if [ "${#files}" -eq 0 ]; then
                    break
                fi
            done

            for file in "${files[@]}"; do
                echo "${file%.*}"
            done | sort -u | less -R --no-init --quit-if-one-screen
            ;;
        t|ta|tag)
            cd "$NOTES_DIR" || return 1
            if [ $# -lt 2 ]; then
                awk 'tolower($1) ~ /^(keywords|tags):/ {$1=""; print $0}' ./*.txt | tr ',' '\n' | sort -u | awk '{$1=$1;print}' | less -R --no-init --quit-if-one-screen
            else
                grep -H -i -E -- '^(keywords|tags):' * | grep -F -w "$2" | cut -d : -f 1 | while IFS= read -r note; do
                    echo "${note%.*}"
                done | sort | less -R --no-init --quit-if-one-screen
            fi
            ;;
        p|pr|pre|prev|previ|previe|preview)
            if [ $# -lt 2 ]; then
                usage >&2
                return 1
            fi

            local note
            note="$NOTES_DIR"/"${2%.txt}".txt
            if [ ! -f "$note" ]; then
                echo "Note '$2' not found" >&2
                return 1
            fi

            if ! command -v pandoc >/dev/null; then
                echo "Couldn't find pandoc" >&2
                return 1
            fi

            cd "$NOTES_DIR" || return 1
            tmp=$(mktemp).html
            pandoc --standalone --to html --from markdown --output "$tmp" "$note"

            if [ -n "$BROWSER" ]; then
                $BROWSER "$tmp"
            elif command -v xdg-open >/dev/null; then
                xdg-open "$tmp"
            elif command -v open >/dev/null; then
                open "$tmp"
            elif command -v firefox >/dev/null; then
                firefox "$tmp"
            fi
            ;;
        *)
            usage >&2
            return 1
            ;;
    esac
}
note "$@"