about summary refs log tree commit diff
path: root/nixos/scripts/dm-note
blob: 318c613d72cef256b392a958d26195e5f658afa8 (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
#!/usr/bin/env bash
#
# Script name: dm-note
# Description: Store multiple one-line texts or codes and copy one of them when needed.
# Dependencies: dmenu, fzf, rofi, xclip
# GitLab: https://www.gitlab.com/dwt1/dmscripts
# License: https://www.gitlab.com/dwt1/dmscripts/LICENSE
# Contributors: Fawzakin
#               Derek Taylor

# Set with the flags "-e", "-u","-o pipefail" cause the script to fail
# if certain things happen, which is a good thing.  Otherwise, we can
# get hidden bugs that are hard to discover.
set -euo pipefail

# shellcheck disable=SC1091
source ./_dm-helper.sh 2>/dev/null || source _dm-helper.sh 2>/dev/null

source_dmscripts_configs

if configs_are_different; then
    echo "$(date): configs are different" >>"$DM_CONFIG_DIFF_LOGFILE"
    sleep 1
fi

# TODO: Program is broken with FZF, fix later.
main() {
    # Picking our options.
    choice=$(printf 'Copy note\nNew note\nDelete note\nQuit' | ${MENU} 'Notes:')

    # Choose what we should do with our choice.
    case "$choice" in
    'Copy note')
        # shellcheck disable=SC2154
        note_pick=$(${MENU} 'Copy:' <"${note_dir}")
        [ -n "${note_pick}" ] && echo "${note_pick}" | cp2cb && notify-send -u normal "Note copied" "${note_pick}"
        ;;
    'New note')
        note_new=$(echo "" | ${MENU} 'Enter new note:')
        # Making sure the input is not empty and not already exist in note_dir.
        # The sed command should prevent grep from taking certain characters as a regex input.
        [ -n "$note_new" ] && ! grep -q "^$(echo "${note_new}" | sed -e 's/\[/\\[/g;s/\]/\\]/g')\$" "${note_dir}" \
            && echo "${note_new}" >>"${note_dir}" && notify-send -u normal "Note created" "${note_new}"
        ;;
    'Delete note')
        note_del=$(${MENU} 'Delete:' <"${note_dir}")
        # grep should always returns 0 regardless what happens.
        grep -v "^$(echo "${note_del}" | sed -e 's/\[/\\[/g;s/\]/\\]/g')\$" "${note_dir}" >"/tmp/dmnote" || true
        [ -n "${note_del}" ] && cp -f "/tmp/dmnote" "${note_dir}" && notify-send -u normal "Note deleted" "${note_del}"
        ;;
    'Quit')
        echo "Program terminated." && exit 0
        ;;
    *)
        exit 0
        ;;
    esac
}

MENU="$(get_menu_program "$@")"
[[ "${BASH_SOURCE[0]}" == "${0}" ]] && main