Changed default behaviour to use environment-specified $EDITOR

This commit is contained in:
Jon-William Lewis
2017-05-09 07:57:07 -05:00
parent 7577a682ae
commit f63bc4c908

17
sns.sh
View File

@@ -18,7 +18,7 @@ readonly SNS_RESET_COLOR='\033[0m'
readonly SNS_ERR_NO_STORE=5 # The sns store needs to be initialized readonly SNS_ERR_NO_STORE=5 # The sns store needs to be initialized
readonly SNS_ERR_NO_OPTS=10 # No mode argument was specified readonly SNS_ERR_NO_OPTS=10 # No mode argument was specified
readonly SNS_ERR_NS_ARGS=11 # The specified mode requires an argument readonly SNS_ERR_NS_ARGS=11 # The specified mode requires an argument
readonly SNS_ERR_NO_EDTR=15 # No editor specified in environment
# Dependency Errors # Dependency Errors
readonly SNS_ERR_DEPS=20 # Base error - add the following codes readonly SNS_ERR_DEPS=20 # Base error - add the following codes
readonly SNS_ERR_NO_gpg2=1 # GPG is not installed readonly SNS_ERR_NO_gpg2=1 # GPG is not installed
@@ -31,7 +31,6 @@ typeset -a SNS_ACTION=("")
typeset -a MISSING_DEPS typeset -a MISSING_DEPS
typeset -i SNS_EXIT=0 typeset -i SNS_EXIT=0
declare SNS_PUBKEY= declare SNS_PUBKEY=
# Function Definitions # Function Definitions
# sns_printError Prints an error message # sns_printError Prints an error message
# sns_NoteHeader Prints standard note header to stdout # sns_NoteHeader Prints standard note header to stdout
@@ -68,6 +67,14 @@ function sns_checkStore(){
if [ -d "$SNS_STORE" ]; then true; else false; fi if [ -d "$SNS_STORE" ]; then true; else false; fi
} }
function sns_sanityCheck { function sns_sanityCheck {
set +u
if [ "$SNS_EDITOR" != "vim" ] && [ -z "$EDITOR" ]; then
echo "$SNS_EDITOR"
sns_printError "No editor specified in environment."
SNS_EXIT="$SNS_ERR_NO_EDTR"
return
fi
set -u
if ! sns_checkDeps; then if ! sns_checkDeps; then
SNS_EXIT="$SNS_ERR_DEPS" SNS_EXIT="$SNS_ERR_DEPS"
for DEP in "${MISSING_DEPS[@]}"; do for DEP in "${MISSING_DEPS[@]}"; do
@@ -173,8 +180,9 @@ printf "\n"
#Determine Run Mode #Determine Run Mode
if [ -r "$SNS_STORE/.vim" ]; then if [ -r "$SNS_STORE/.vim" ]; then
# Vim mode
readonly SNS_EDITOR="vim"
readonly SNS_DEPS=("vim" "tree" "git") readonly SNS_DEPS=("vim" "tree" "git")
function sns_create(){ function sns_create(){
if [ -r "$SNS_STORE/$(echo "$@" | awk '{print $1}')" ]; then if [ -r "$SNS_STORE/$(echo "$@" | awk '{print $1}')" ]; then
sns_printError "Note already exists." sns_printError "Note already exists."
@@ -191,6 +199,7 @@ if [ -r "$SNS_STORE/.vim" ]; then
sns_printError "Printing of Vim-encrypted notes is not supported at this time." sns_printError "Printing of Vim-encrypted notes is not supported at this time."
} }
else else
readonly SNS_EDITOR="$EDITOR"
readonly SNS_DEPS=("gpg2" "tree" "git") readonly SNS_DEPS=("gpg2" "tree" "git")
function sns_create(){ function sns_create(){
@@ -215,7 +224,7 @@ else
if [ ! -r /tmp/"$SNS_SID" ]; then gpg2 -d -o /tmp/"$SNS_SID" "$SNS_NOTE"; fi if [ ! -r /tmp/"$SNS_SID" ]; then gpg2 -d -o /tmp/"$SNS_SID" "$SNS_NOTE"; fi
# Edit the note # Edit the note
vim /tmp/"$SNS_SID" "$EDITOR" /tmp/"$SNS_SID"
# Make sure the notebook/section exists # Make sure the notebook/section exists
if [ ! -d "$(dirname "$SNS_NOTE")" ]; then mkdir -p "$(dirname "$SNS_NOTE")"; fi if [ ! -d "$(dirname "$SNS_NOTE")" ]; then mkdir -p "$(dirname "$SNS_NOTE")"; fi