~srivathsan/note

ref: 36e16e4dcbf22f7e95e8b42c2b6dd3082673c037 note/note -rwxr-xr-x 2.1 KiB
36e16e4dSrivathsan Murali fix: note ls [subdir] not working 1 year, 10 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
#!/bin/sh

NOTE_VERSION="0.1.0"
NOTES_DIR="${NOTES_DIR:-$HOME/.local/share/notes}"

git_cmd() {
  git -C "$NOTES_DIR" "$@"
}

git_add() {
  git -C "$NOTES_DIR" add "$1"
}

git_commit() {
  git -C "$NOTES_DIR" commit -m "$1"
}

cmd_prune() {
  find "$NOTES_DIR" -not -iwholename '*.git*' -type d -empty -delete
}

cmd_init() {
  [ -d "$NOTES_DIR" ] && echo "Notes directory is already initialized." && exit 1;
  mkdir -v -p "$NOTES_DIR"
  git_cmd init
}

cmd_open() {
  localpath="$1.md"
  filepath="$NOTES_DIR/$localpath"
  folder_path=$(dirname "$filepath")
  
  mkdir -p "$folder_path"
  $EDITOR "$filepath"

  git_add "$localpath"
  git_commit "$localpath changed"
}

cmd_move() {
  end_dir=$(dirname "$NOTES_DIR/$2")
  mkdir -p "$end_dir"
  git -C "$NOTES_DIR" mv "$1.md" "$2.md"
  git_commit "moved $1 -> $2"
  cmd_prune
}

cmd_remove() {
  git -C "$NOTES_DIR" rm "$1.md"  
  git_commit "deleted $1"
  cmd_prune
}

cmd_list() {
  path=${1:-"."}
  (
    cd "$NOTES_DIR"
	  tree -C -l --noreport "$path" \
    | tail -n +1 \
    | sed -E -e 's/\.md(\x1B\[[0-9]+m)?( ->|$)/\1\2/g' # remove .md at end of line, but keep colors
  )
}

PROGRAM="${0##*/}"
COMMAND="$1"

cmd_usage() {
	cat <<-_EOF
  note version $NOTE_VERSION
	Usage:
	    $PROGRAM init 
	        Initialize new notes directory.
          NOTES_DIR can be set for custom location.
	    $PROGRAM [ls] [subfolder]
	        List notes.
	    $PROGRAM open  note-name
          Opens the note in the \$EDITOR
	    $PROGRAM rm [--recursive,-r] [--force,-f] note-name
	        Remove existing note or directory, optionally forcefully.
	    $PROGRAM mv old-path new-path
	        Renames or moves old-path to new-path.
	    $PROGRAM git git-command-args...
	        If the notes dir is a git repository, execute a git command
	        specified by git-command-args.
	    $PROGRAM help
	        Show this text.
	_EOF
}

case "$1" in
  init) shift; cmd_init ;;
  open) shift; cmd_open $@;;
  ls|list) shift; cmd_list $@;; 
  mv|move) shift; cmd_move $@ ;;
  rm|remove) shift; cmd_remove $@ ;;
  git) shift; git_cmd "$@" ;;
  help|*) cmd_usage ;;
esac