Cli Notebook

Cli Notebook Shell Script

forked from ihttps://github.com/dcchambers/note-keeper

  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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/bin/bash
# forked from ihttps://github.com/dcchambers/note-keeper
# modify by sandylaw <[email protected]> 2020-10-16
YEAR=$(date +'%Y')
MONTH=$(date +'%m')
DAY=$(date +'%d')
NOTE_DIR="$HOME/notes/$category"
NOTE_NAME="$YEAR-$MONTH-$DAY.md"
Author=sandylaw
Email=[email protected]

NOTERC="${XDG_CONFIG_HOME:-$HOME/.config}/notekeeper/noterc"
# shellcheck source=/dev/null
if [ -f "$NOTERC" ]; then . "$NOTERC"; fi

menu() {
	pushd "$HOME/notes" &>/dev/null || exit
	printf "# My Wiki\n\n" >"$HOME"/notes/README.md
	for dir in $(ls -l . | grep "^d" | awk '{print $9}'); do
		echo -e "[$dir]($dir/index.md)\n" >>"$HOME"/notes/README.md
		rm -rf "$dir"/index.md
		echo -e "[Parent](../README.md)\n\n# $dir\n" >"$dir"/index.md
		pushd "$dir" &>/dev/null || exit
		#	for x in $(ls -I index.md $dir);do echo -e "[${x%%.*}](./$x)\n" >> "$dir"/index.md; done
		for x in ./*; do
			y="${x%%.md*}"
			[[ $y == "./index" ]] && continue
			echo -e "[${y/.\//}]($x)\n" >> ./index.md
		done
		popd &>/dev/null || exit
	done
	popd &>/dev/null || exit
}

add_note() {
	NOTE_DIR="$HOME/notes/$category"
	if [ ! -f "$NOTE_DIR/$NOTE_NAME" ]; then
		mkdir -p "$NOTE_DIR"
		touch "$NOTE_DIR/$NOTE_NAME"
		cat <<EOF | tee "$NOTE_DIR/$NOTE_NAME"

[index](./index.md)

# $(echo "${NOTE_NAME%%.*}" | awk -F "-" '{print $4}') 

---

Author:$Author 

Email :$Email

Date  :$(date +%F)

tags  : 

---


EOF
		printf "Created new note: %s/%s\n" "$NOTE_DIR" "$NOTE_NAME"
		menu
	fi
}

print_info() {
	if [ -z "$category" ]; then category=default; fi
	NOTE_DIR="$HOME/notes/$category"
	printf "Note preview:\n====================\n\n"
	head -n 8 "$NOTE_DIR/$NOTE_NAME"
	printf "\n====================\n"
	printf "Note Stats:\n"
	stat "$NOTE_DIR/$NOTE_NAME"
	printf "\n====================\n"
	printf "File Information:\n"
	ls -lah "$NOTE_DIR/$NOTE_NAME"
}

print_help() {
	echo -e "note - Note Keeper 0.5.0 (28 July 2020)

Usage: note [arguments]

Arguments:
  -h | --help                         Display usage guide.

  -c | --category                     Set category for note.
  -n | --name                         Set filename for note. Will be created in \$NOTE_DIR

  -e | --edit <DATE: year-month-day>  Open a specific note for editing.
  -p | --print                        Print the contents of a note.
  -a | --add                          Create a note but don't open it for editing.

  -i | --info                         Print information about a note.
  -t | --time                         Add a timestamp when opening a note.
  -m | --menu                         Update README and $category/index.md

The script loads configuration variables from \${XDG_CONFIG_HOME:-\$HOME/.config}/notekeeper/noterc.

Example:
# Directory where the current note should be stored
NOTE_DIR=\"\$HOME/notes/\$category\"

# Name of the Note
NOTE_NAME=\"\$YEAR-$MONTH-$DAY.md\"\n"
}

open_note() {
	if [ -z "$category" ]; then category=default; fi
	NOTE_DIR="$HOME/notes/$category"
	if ! [ -f "$NOTE_DIR/$NOTE_NAME" ]; then printf "Note %s/%s doesn't exist.\n" "$NOTE_DIR" "$NOTE_NAME" && exit 1; fi
	if [[ $EDITOR = *"vim"* ]] || [[ $EDITOR = *"nvim"* ]]; then
		# Open Vim or Neovim in insert mode.
		$EDITOR "+normal G$" "$NOTE_DIR/$NOTE_NAME"
	elif [[ $EDITOR = *"emacs"* ]]; then
		# Open Emacs with cursor at EOF.
		emacs -nw "$NOTE_DIR/$NOTE_NAME" --eval "(goto-char (point-max))"
	elif [[ $EDITOR = "" ]]; then
		# If no default editor, use Vim and open in insert mode.
		vim "+normal G$" "$NOTE_DIR/$NOTE_NAME"
	else
		$EDITOR "$NOTE_DIR/$NOTE_NAME"
	fi
}

if (($# > 0)); then
	while [[ $# -gt 0 ]]; do
		key="$1"
		case $key in
		-e | --edit)
			printf "(e)dit is not yet implemented :(\n"
			exit 0
			;;
		-p | --print)
			cat "$NOTE_DIR/$NOTE_NAME"
			shift
			;;
		-a | --add)
			if [ -z "$category" ]; then category=default; fi
			add_note
			shift
			;;
		-i | --info)
			print_info
			shift
			;;
		-c | --category)
			category="$2"
			if [ -z "$category" ]; then category=default; fi
			shift
			shift
			if [ "$#" == 0 ]; then open_note; fi
			;;
		-n | --name)
			NOTE_NAME="$YEAR-$MONTH-$DAY-$2.md"
			if [ -z "$NOTE_NAME" ]; then printf "No name found. Please provide a name.\n" && exit 1; fi
			if [ -z "$category" ]; then category=default; fi
			# Hacky way of checking if -n is the only option
			if [ "$#" -eq 2 ]; then
				shift
				shift
				add_note
				open_note
			fi
			shift
			shift
			;;
		-h | --help)
			print_help
			shift
			;;
		-t | --time)
			if [ -z "$category" ]; then printf "No category name found. Please provide a category name.\n" && exit 1; fi
			printf "%s\n" "[$(date +%T)]" >>"$NOTE_DIR/$NOTE_NAME"
			shift
			open_note
			;;
		-m | --menu)
			menu
			shift
			;;
		*)
			printf "Unknown Argument %s \n" "$1"
			printf "Try \"note --help\" to see usage information.\n"
			shift
			;;
		esac
	done
else
	open_note
fi
点击刷新