#!/bin/bash ## ##------------------------------------------ ## --- thisfile=$(readlink -f "$0") thisdir=$(dirname "$thisfile") thisname=$(basename "$thisfile") ## --- verbose_out=/dev/null fatal_out=/dev/stderr ## --- notes_editor='micro' notes_editor='vi' notes_editor_name=$(basename "$notes_editor") ## --- # ------------------------------ app=$thisname app_desc="A quick notes app" app_author="kapad'21" app_version="1b.22.266" # date +'%y.%j' app_version="1b.23.234" app_version="24.354.0456" app_version="24.354.2229" # ------------------------------ app_banner(){ echo "\$app: $thisfile" >&2 echo "$app - $app_desc - v${app_version}/${app_author}" >&2 } ## ##------------------------------------------ ##default .rc ##------------------------------------------ ## --- md_date_tag='~' md_title_size_tag='##' md_title_open_tag='📒' #📖 📒 🔖 📋 md_title_close_tag='' md_post_size_tag='###' md_post_open_tag='📝' #✍ ✏ ✒ 🖋 🖊 🖌 🖍 📝 md_post_close_tag='' md_post_date_tag='⎇' #else `$md_date_tag` # basedir of save/read # notesdir=./@notesd # edit or import config to override. # default config: ./.notesrc # ## TODO: //24.354.2144 # must play with user/project config up here. # find `$notesrc`, then deal with this, when its' time come. # ## ##------------------------------------------ ## --- CATVIEWER='' [ -z "$CATVIEWER" ] && which bcat 2>/dev/null >/dev/null && CATVIEWER=bcat [ -z "$CATVIEWER" ] && which batcat 2>/dev/null >/dev/null && CATVIEWER=batcat [ -z "$CATVIEWER" ] && CATVIEWER=cat ## ##------------------------------------------ ## --- TIMESTAMP=$(date +"%a %d %b %Y %H:%M:%S %Z") TIMESTAMPON= version="" desc=" " tags= ## ##------------------------------------------ ## --- hecho(){ echo "$@"|egrep --color=always "^|^[^:]*"; } echo $*|grep -q '\-v' && verbose_out=/dev/stderr # ##TODO: # if a '.notesrc' file exists in current folder, # consider current dir as '$notesdir', # and just apply the reset of the options. # ## ##------------------------------------------ ## --- # $1 = filepath # $2 = topic/desc create_md(){ echo '```' echo "--------------" echo "-- File: "${1} echo "-- Description: "${2} echo "-- User: "${USER} echo "-- Created: "${TIMESTAMP} echo "-- Tags: "${tags:-md} echo "--------------" echo '```' echo "" echo "" echo "#~~~" echo "${md_title_size_tag} ${md_title_open_tag} ${1} ${md_title_close_tag}" echo "${md_date_tag} \`\` ${TIMESTAMP} \`\`" # echo "${md_desc_size_tag} ${md_desc_open_tag} ${2} ${md_desc_close_tag}" echo '```' echo "$2" echo '```' echo "" } ###✍ hello3 #⎇ *Tue 21 Jun 2022 12:27:21 PM EEST ~* # add_stamp(){ echo "" echo '#~' echo "${md_post_size_tag} ${md_post_open_tag} ${@} ${md_post_close_tag}" echo "${md_post_date_tag} \`\` ${TIMESTAMP} \`\`" echo '``` ```' echo "" echo "" } lsnotesdir(){ { echo "[i] config:" $notesrc echo "[i] .notesdir:" $notesdir echo "[i] >" $(du -sh "$notesdir"|awk '{print $1}'|egrep --color=always '[A-Z]') $(ls -AFhldo --color "$notesdir" ) echo } | egrep --color "^|^[^:]*" ls --group-directories-first --color -AFL -t "$notesdir/" } ##------------------------------------------------------------ ## find and apply custom config... ##------------------------------------------------------------ ## --- notesrc= notesdir=./@notesd if [[ -r ./.notesrc ]]; then notesrc=$(readlink -f ./.notesrc) elif [[ -r ~/.notesrc ]]; then notesrc=$(readlink -f ~/.notesrc) fi [ -r "$notesrc" ] || { echo "[!] Cant import file '$notesrc' !" > $fatal_out exit 2 } while [ -n "$notesrc" -a -r "$notesrc" ];do notesrc_bak=$notesrc notesrc= echo "[i] Import:" $notesrc_bak > $verbose_out . $notesrc_bak done notesrc=$notesrc_bak notesrc_bak= # echo "- notesrc_bak: "$notesrc_bak hecho "- notesrc: "$notesrc > $verbose_out hecho "- notesdir: "$notesdir > $verbose_out echo "$notesdir"|grep -q "^\/" || { #echo "[i] Found relative dir for 'notesdir': $notesdir" notesdir=$(dirname "$notesrc")/$notesdir #echo "[i] Resolve 'notesdir' as: $notesdir" notesdir=$(realpath "$notesdir") echo "[i] Build and Normalise 'notesdir' as: $notesdir" > $verbose_out } notesdir=$(readlink -f "$notesdir") mkdir -p "$notesdir" 2>/dev/null >/dev/null [ ! -d "$notesdir" ] && { echo "[!] Notes dir '$notes', can not be found !" > $fatal_out exit 1 } [ "$1" = '-q' ] && echo $notesdir && exit 0 long_help=$( cat "$0" \ |tr -s '\t' ' '| tr -s ' '| sed -E s/'^[[:space:]]'/"\t"/ \ |egrep --color "^[[:space:]]*[a-zA-Z\-][^\)\(]*\)"|tr -d ')' ) ##------------------------------------------------------------ ##------------------------------------------------------------ ## --- [ -z "$1" ] && { app_banner; lsnotesdir; echo "$long_help"; exit $?; } ##------------------------------------------------------------ ##view / ... ##------------------------------------------------------------ ## --- echo "$1" |egrep -q "^[[:space:]]*\-" && { act="$1" ;shift 2>/dev/null || true mdfile="$1" ;shift 2>/dev/null || true [ -n "$mdfile" ] && thismd="$notesdir/${mdfile}.md" case $act in -h) #long help | echo "$long_help"|egrep --color "^[^\)]*" exit $?; ;; -fm) #file-manager | x-file-manager "$notesdir" exit $?; ;; -f) if [ -n "$mdfile" ]; then lsmd $notesdir -q ${mdfile} else lsmd $notesdir fi exit $?; ;; -t) #show env | { echo " act: $act" echo " rc: $notesrc" echo "notesdir: $notesdir" echo " mdfile: ${mdfile}" echo " thismd: $thismd" echo " ~: "`ls --color -AFlh "$thismd" 2>/dev/null` }|egrep --color "^|^[^:]*" exit $?; ;; esac ##------------------------------------------------------------ ## --- [ -z "$mdfile" ] && { acts=$( echo "$long_help" |cut -d\) -f1 |tr -s ' ' ) echo "Usage: $app "$acts" ... " >&2 exit 1 } echo "[*] $act : '$thismd' " >&2 [ ! -f "$thismd" ] && echo "[!] File '$thismd', not exist." >&2 && exit 0 case $act in -c) #cat | $CATVIEWER "$thismd"; exit $?; ;; # -fm) #front-matter | # front-matter.php "$thismd"; # exit $?; ;; -d) #delete | head -n10 "$thismd"|egrep '^[[:space:]]*\-\-[^\-]'|batcat - echo -n '[!] delete this file ? '; read -N1 a; echo echo -n "[?] "; \rm -vi "$thismd" echo "[$?] deleting '$thismd'" exit $?; ;; -x) #x-file-editor | (( >/dev/null /dev/null || true ; } echo "$1"|egrep -q "^[[:alpha:]]" && thistitle="$@" { echo "[i] Notes dir: $notesdir (Default: $defmd)" echo "" echo " md: $thismd" echo "title: $thistitle" echo " date: $TIMESTAMP" }|egrep --color "^|^[^:]*" if [ ! -f "$thismd" ]; then thismd_dirname=$(dirname "$thismd") mkdir -p "$thismd_dirname" create_md "$( basename "$thismd")" "${thistitle:-$thisid}" >> "$thismd" else [ -n "$thistitle" ] && { add_stamp "$thistitle" >> "$thismd" ; } fi case $notes_editor_name in vi|vim) $notes_editor + +star "$thismd"; ;; micro) $notes_editor "$thismd"; ;; *) echo "[!] Editor '$notes_editor', not supported !" >&2; ;; esac