aboutsummaryrefslogtreecommitdiff
path: root/extras/HOME/.local/bin/notes-select.sh
blob: e630eca18b92c93de0f0a7bce7f3a412ee79c0ff (plain) (blame)
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
#!/bin/bash

mkdir -p "$HOME/.notes"
options="$(cd "$HOME/.notes" && find -type f -not -path '*/.*' | sed 's#^./##')"
selection="$(
  echo -e "$options" | $ROFI -i -p "Notes" \
    -kb-custom-1 "Alt+d" \
    -kb-custom-2 "Control+c" \
    -theme-str '* {theme-color: #f0f0a0;}' \
    )"

ec="$?"
if [[ "$ec" -eq 10 ]] ; then
  action="delete"
elif [[ "$ec" -eq 11 ]] ; then
  action="copy"
else
  action="edit"
fi

if [ -z "${selection}" ] ; then
  exit 1
fi

function do_edit {
  local path="$(readlink -fm "$1")"
  mkdir -p "$(dirname "$path")"
  alacritty --class floating-terminal -e vim "$path"

  if [[ "$path" == *"/shared/"* ]] ; then
    cd "$HOME/.notes/shared"

    git add "$path"
    git commit -m "Added $(echo "$path" | sed 's#'"$HOME"'#.notes/shared/#')"
  fi
}

fullnotepath="$HOME/.notes/${selection}"
echo "$action"
case "$action" in
  "copy")
    mkdir -p $(dirname "$fullnotepath")

    if [[ -e "$fullnotepath" ]] ; then
      cat "$fullnotepath" | sed -z '$ s/\n$//' | xclip -selection clipboard
    else
      do_edit "$fullnotepath"
    fi
    ;;
  "edit")
    do_edit "$fullnotepath"
    ;;
  "delete")
    echo "delete"
    rm "${fullnotepath}"
esac