zsh: ned: big improvements. check the usage() function for details
This commit is contained in:
parent
ad4012b1c4
commit
d61794254c
1 changed files with 72 additions and 22 deletions
|
@ -1,35 +1,85 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
# NEd: Note Editor (and your best friend!)
|
# NEd: Note Editor (and your best friend!)
|
||||||
# Ned can edit and create notes in a folder you tell him to!
|
# Ned can edit and create notes in a folder you tell him to!
|
||||||
|
# Depends: fzf
|
||||||
|
|
||||||
# shellcheck disable=SC2068
|
# shellcheck disable=SC2068,SC2178,SC2128
|
||||||
|
|
||||||
# set default value for $NOTES_DIR
|
# set default values if things are unset
|
||||||
${NOTES_DIR:="$HOME/Documents/college/2y/2q/"}
|
NOTES_DIR="${NOTES_DIR:-"$HOME/Documents/college/current"}"
|
||||||
|
EDITOR="${EDITOR:-"nvim"}"
|
||||||
|
|
||||||
[ -d "$NOTES_DIR"/misc ] && mkdir "$NOTES_DIR/misc"
|
usage() {
|
||||||
|
cat <<-EOF
|
||||||
|
Usage:
|
||||||
|
ned <verb>
|
||||||
|
new <folder> [note name] create a new note in the matched class folder
|
||||||
|
edit [folder] search for and edit a text document in a folder
|
||||||
|
help print this message
|
||||||
|
|
||||||
# Create misc note if no input
|
Notes:
|
||||||
if [ -z "$1" ]; then
|
Right now, only the first letter of each option is considered, so \`ned n\` and \`ned neww\` do the same thing
|
||||||
"$EDITOR" "$NOTES_DIR/unsorted/$(date)"
|
Folder and file names are globbed
|
||||||
zsh
|
Current repository path: $NOTES_DIR
|
||||||
fi
|
EOF
|
||||||
|
|
||||||
new() {
|
|
||||||
if [ -z "$1" ]; then
|
|
||||||
"$EDITOR" "$NOTES_DIR/unsorted/$(date)"
|
|
||||||
zsh
|
|
||||||
fi
|
|
||||||
|
|
||||||
"$EDITOR" "$NOTES_DIR/$2"*"/notes/$3"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Parse arguments
|
edit() {
|
||||||
case "$1" in
|
check_setup
|
||||||
|
|
||||||
|
NOTE_PATH=("$NOTES_DIR/$2"*)
|
||||||
|
NOTE_PATH="${NOTE_PATH[0]}"
|
||||||
|
if ! [ -d "$NOTE_PATH" ]; then
|
||||||
|
echo "No such directory: '$NOTE_PATH'"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
NOTE_NAME="$(cd "$NOTE_PATH" && fzf --height=10 --layout=reverse)" || exit
|
||||||
|
"$EDITOR" "$NOTES_DIR/$NOTE_NAME"
|
||||||
|
}
|
||||||
|
|
||||||
|
new() {
|
||||||
|
check_setup
|
||||||
|
if [ -z "$3" ]; then
|
||||||
|
NOTE_NAME="$(date +'%F').md"
|
||||||
|
else
|
||||||
|
NOTE_NAME="$3.md"
|
||||||
|
fi
|
||||||
|
|
||||||
|
NOTE_PATH=("$NOTES_DIR/$2"*/notes)
|
||||||
|
NOTE_PATH="${NOTE_PATH[0]}"
|
||||||
|
if ! [ -d "$NOTE_PATH" ]; then
|
||||||
|
echo "No such directory: '$NOTE_PATH'"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
"$EDITOR" "$NOTE_PATH/$NOTE_NAME"
|
||||||
|
}
|
||||||
|
|
||||||
|
subshell() {
|
||||||
|
echo 'entering subshell'
|
||||||
|
zsh
|
||||||
|
}
|
||||||
|
|
||||||
|
check_setup() {
|
||||||
|
# perform preliminary checks to make sure the script can run properly
|
||||||
|
if ! [ -d "$NOTES_DIR" ]; then
|
||||||
|
echo "Error: Notes directory ($NOTES_DIR) does not exist or is not a directory"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# Parse arguments (first character only)
|
||||||
|
case "${1:0:1}" in
|
||||||
|
# help
|
||||||
|
'h') usage;;
|
||||||
# create and edit new note
|
# create and edit new note
|
||||||
'new'|'n') new $@;;
|
'n') new $@;;
|
||||||
# edit ned
|
# edit an existing document or file
|
||||||
'e') "$EDITOR" "$(realpath "$0")";;
|
'e') edit $@;;
|
||||||
|
# me: edit ned (the file you are looking at)
|
||||||
|
'm') "$EDITOR" "$(realpath "$0")";;
|
||||||
*) echo "no such option";;
|
*) echo "no such option";;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
|
# vim: et sw=3 ts=3
|
||||||
|
|
Loading…
Add table
Reference in a new issue