~srivathsan/note

ref: 3937558b52e18c7c7d120da650fd6d9b7673bca0 note/note -rwxr-xr-x 2.0 KiB
3937558bSrivathsan Murali Makefile added 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
#!/bin/sh

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:-""}
	tree -C -l --noreport "$NOTES_DIR/$path" | tail -n +2 | sed -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
	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