fix to identical file and dir prefixes, updated config with max nest and max listing options, added --all parameter to show more than notes

This commit is contained in:
mtekman 2017-09-16 17:41:59 +02:00
parent 6f3dce6972
commit b092e7fdf1
2 changed files with 168 additions and 75 deletions

View File

@ -3,12 +3,13 @@
_org(){ _org(){
local state local state
local f_dirname=~/.config/org_notes.location local f_dirname=~/.config/org_notes.conf
touch $f_dirname touch $f_dirname
local f_dir=$(cat $f_dirname) local f_dir=$(grep "^location" $f_dirname | sed -r 's|^location\s*=\s*(.*)\s*|\1|' )
if [ "$f_dir" = "" ]; then
echo -e "-> No location set in $f_dirname" if [ "$f_dir" = "" ] || ! [ -d $f_dir ]; then
echo -e "-> No valid location set in $f_dirname"
return -1 return -1
fi fi

View File

@ -45,54 +45,69 @@ __org_get_pass(){
echo $pass echo $pass
} }
org(){ org(){
local org_loc=~/.config/org_notes.location local org_loc=~/.config/org_notes.conf
local switch="$1"
if ! [ -e $org_loc ] || [ "$(cat $org_loc)" = "" ];then ## SWITCHES that do NOT rely on valid settings
echo "Cannot find orgnotes git location. Please set the location in the file: case $switch in
$org_loc --help)
or run the install script." local name_padding=$(cat `basename $0` | sed 's/.*/ /g')
echo "
`basename $0` <filename> [--delete] [--move=<dest>] [--encrypt] [--decrypt]
or $name_padding [subfolder] [--all]
or $name_padding [--help] [--sync] [--create-config]
Generates an org-notes file in a folder location set in the $org_loc file, or a given sub-folder";
return 0
;;
--create-config)
if [ -e $org_loc ]; then
echo -n "Config already exists at $org_loc. Overwrite? [y/N] "; read ans;
[ "$ans" != "y" ] && echo "Leaving untouched." && return -1
fi
mkdir -p `dirname $org_loc` 2>/dev/null
cat << EOF > $org_loc
location = /path/to/your/notes
# Preferably a path within a git folder for the '--sync' parameter to work
maxlist = 15
# The max number of lines to print when displaying tree structure. Leave blank to show all.
maxnest = 3
# The max levels of subdirectories to recurse down into when displaying tree structure.
# Leave blank for all.
EOF
echo -e "\nCreated config at $org_loc.\nPlease modify to suit your preferences.\n"
sleep 2
emacs $org_loc
return 0
;;
esac
## End SWITCHES that do NOT rely on valid settings
#### All below are switches that rely on a valid location file
if ! [ -e $org_loc ] || [ "$(grep ^location $org_loc)" = "" ]; then
echo "Cannot find orgnotes location file, or location is not set within the file. Please run:
`basename $0` --create-config
"
return -1 return -1
fi fi
local org_dir=$(cat $org_loc) #### Now discover org dir settings
local regex_extract="\s*=\s*(.*)"
local file=$1 local org_dir=$(grep "^location" $org_loc | sed -r "s|^location$regex_extract|\1|")
local command=$2 local org_maxlist=$(grep "^maxlist" $org_loc | sed -r "s|^maxlist$regex_extract|\1|")
local org_maxnest=$(grep "^maxnest" $org_loc | sed -r "s|^maxnest$regex_extract|\1|")
# Is file an existing subfolder? ## SWITCHES that rely on valid settings
local sub_dir="" if [ "$switch" = "--sync" ]; then
if [ -d ${org_dir}/$file ]; then
sub_dir=$file
file=""
fi
case $file in
"") # applies to subdirs too
local title="You have the following org-mode notes:"
if [ "$sub_dir" != "" ]; then
title=`echo $title | sed "s|:| [${sub_dir}]:|"`
fi
local root_dir=${org_dir}/${sub_dir}
tree -CD -P '*.org' -rt ${root_dir} -L 3\
| sed "s|${root_dir}|\n${title}\n|"\
| sed -r 's|([^[].*)\s(\[.*\])\s(.*)|\t\2\t\1\3|'
return -1;
;;
-h);&
--help)
echo "
`basename $0` [--help] [--sync] [sub-folder] <filename [--delete] [--move=<dest>] [--encrypt] [--decrypt]>
Generates an org-notes file in a folder location set in the $org_loc file, or a given sub-folder
";
return -1;
;;
--sync)
cd ${org_dir}; cd ${org_dir};
git pull; git pull;
local updates=`git status ./ -s`; local updates=`git status ./ -s`;
@ -103,31 +118,107 @@ Generates an org-notes file in a folder location set in the $org_loc file, or a
git push -u origin master git push -u origin master
fi fi
cd - >/dev/null cd - >/dev/null
return 0;; return 0
fi
## End SWITCHES that rely on valid settings
## End all SWITCHES
unset switch
## FILES or SUBDIRECTORIES
local fileorsub_wo_ext=$1
local command=$2
# Is file an existing subfolder?
local sub_dirtree=""
local file_wo_ext=""
# Root directory for blank, or --all
case $fileorsub_wo_ext in
"")
sub_dirtree=""
file_wo_ext=""
;;
"--all") # First argument as --all occurs when root directory is implied
sub_dirtree="" #
file_wo_ext="" #
command="--all" # shift to next argument
;;
*)
# Ensure that fileorsub has no extension, give just the absolute husk
# e.g. /root/to/org/anime/naruto <-- no ext
#
fileorsub_wo_ext=`dirname $fileorsub_wo_ext`/`basename $fileorsub_wo_ext .org`
local abshusk=${org_dir}/${fileorsub_wo_ext}
if [ -d $abshusk ]; then
# Is there ALSO a filename with this start?
# - Not, then definitely a subdirectory
if [ -f ${abshusk}.org ]; then
sub_dirtree=""
file_wo_ext=${fileorsub_wo_ext}
else
sub_dirtree=${fileorsub_wo_ext}
file_wo_ext=""
fi
else
sub_dirtree=""
file_wo_ext=${fileorsub_wo_ext}
fi
unset abshusk;
;;
esac esac
if [ "$command" != "" ];then ## Not a file, print subdir tree
if [ "$command" = "--delete" ] || [[ "$command" =~ "--move=" ]] || [ "$command" = "--encrypt" ] || [ "$command" = "--decrypt" ]; then ## SUBDIRECTORY section
else if [ "$file_wo_ext" = "" ]; then
echo "Unable to parse: $command" local title="You have the following org-mode notes:"
return -1
fi # Give title for subdirectories (root dir does not need one)
if [ "$sub_dir" != "" ]; then
title=`echo $title | sed "s|:| [${sub_dir}]:|"`
fi fi
local ext=$( echo $file | awk -F'.' '{print $NF}' ) # --all switch for showing all files in a subdir
[ "$ext" != ".org" ] && file=${file}.org local file_filter="-P '*.org'"
[ "$command" = "--all" ] && file_filter=""
local eloc=$org_dir/$file local root_dir=${org_dir}/${sub_dirtree}
[ "$org_maxnest" = "" ] && org_maxnest=10
[ "$org_maxlist" = "" ] && org_maxlist=1000
org_maxlist=$(( $org_maxlist + 3 )) # offset: top two lines are text, and bottom blank
local output=`eval tree -CD ${file_filter} -rt ${root_dir} -L ${org_maxnest}\
| sed "s|${root_dir}|\n${title}\n|"\
| sed -r 's|([^[].*)\s(\[.*\])\s(.*)|\t\2\t\1\3|'\
| sed 's/.org//'`
# Print the tree, blank, and then report
echo -e "$output" | head -n -1 | head -${org_maxlist}
echo ""
echo -e "$output" | tail -1
return -1;
fi
## End SUBDIRECTORY section
## FILE section
# Prettify file_wo_ext to remove './' prefix
file_wo_ext=$(echo $file_wo_ext | sed 's|^./||')
local eloc=$org_dir/${file_wo_ext}.org
# Handle command switches
if [ "$command" != "" ]; then if [ "$command" != "" ]; then
if [ -e $eloc ]; then if [ -e $eloc ]; then
case $command in case $command in
"--encrypt") "--encrypt")
! [ -e ${eloc} ] && echo "File does not yet exist." && return -1 ! [ -e ${eloc} ] && echo "File does not yet exist." && return -1
[ "`echo $eloc | grep -oP '.crypt.org$'`" != "" ] && echo "File is already encrypted." && return -1 [ "`echo $eloc | grep -oP '.crypt.org$'`" != "" ] && echo "File is already encrypted." && return -1
echo -n "Encrypt $file? [y/n] "; read ans; echo -n "Encrypt ${file_wo_ext}? [y/n] "; read ans;
if [ "$ans" = "y" ]; then if [ "$ans" = "y" ]; then
local pass=$(__org_get_pass) local pass=$(__org_get_pass)
@ -144,7 +235,7 @@ Generates an org-notes file in a folder location set in the $org_loc file, or a
! [ -e ${eloc} ] && echo "File does not yet exist." && return -1 ! [ -e ${eloc} ] && echo "File does not yet exist." && return -1
[ "`echo $eloc | grep -oP '.crypt.org$'`" = "" ] && echo "File is already plain-text." && return -1 [ "`echo $eloc | grep -oP '.crypt.org$'`" = "" ] && echo "File is already plain-text." && return -1
echo -n "Decrypt $file? [y/n]"; read ans; echo -n "Decrypt ${file_wo_ext}? [y/n]"; read ans;
if [ "$ans" = "y" ]; then if [ "$ans" = "y" ]; then
local pass=$(__org_get_pass) local pass=$(__org_get_pass)
@ -158,7 +249,7 @@ Generates an org-notes file in a folder location set in the $org_loc file, or a
;; ;;
"--delete") "--delete")
echo -n "Remove $file? [y/n] "; read ans; echo -n "Remove ${file_wo_ext}? [y/n] "; read ans;
if [ "$ans" = "y" ]; then if [ "$ans" = "y" ]; then
rm ${eloc} && echo "Deleted." rm ${eloc} && echo "Deleted."
__cleanup_org $eloc $org_dir __cleanup_org $eloc $org_dir
@ -168,33 +259,34 @@ Generates an org-notes file in a folder location set in the $org_loc file, or a
"--move="*) "--move="*)
local dest=`echo $command | sed 's|--move=||'` local dest=`echo $command | sed 's|--move=||'`
echo -n "Move $file to $dest ? [y/n] "; read ans; echo -n "Move ${file_wo_ext} to $dest ? [y/n] "; read ans;
if [ "$ans" = "y" ]; then if [ "$ans" = "y" ]; then
local ext1=$( echo $dest | awk -F'.' '{print $NF}' ) local ext1=$( echo $dest | awk -F'.' '{print $NF}' )
[ "$ext1" != ".org" ] && dest=${dest}.org [ "$ext1" != ".org" ] && dest=${dest}.org
local edest=$org_dir/$dest local edest=$org_dir/$dest
mkdir -p $org_dir/`dirname $dest` mkdir -p $org_dir/`dirname $dest`
mv $eloc $edest && echo "Moved: $file --> $dest" mv $eloc $edest && echo "Moved: ${file_wo_ext} --> $dest"
__cleanup_org $eloc $org_dir __cleanup_org $eloc $org_dir
return 0 return 0
fi fi
;; ;;
*)
echo "Unable to parse: $command"
return -1
;;
esac esac
else else
echo "Cannot find $file. Aborting." echo "Cannot find ${file_wo_ext}. Aborting."
return -1 return -1
fi fi
else else
# no command, just create or open # no command, just create or open
local dirn=`dirname $eloc`
local eloc=${org_dir}/$file mkdir -p $dirn
local dirn=`dirname $file`
mkdir -p $org_dir/$dirn
# decrypt first if neccesary # decrypt first if neccesary
if [ "`echo $file | grep -oP '.crypt.org$'`" != "" ];then if [ "`echo $(basename $eloc) | grep -oP '.crypt.org$'`" != "" ];then
local pass=$(__org_get_pass) local pass=$(__org_get_pass)
[ $pass = -1 ] && return $pass # exit code [ $pass = -1 ] && return $pass # exit code