From 4477b9907c25a3eae6a4758dd174648ffed44d8f Mon Sep 17 00:00:00 2001 From: Ben Savage Date: Wed, 7 Sep 2022 14:01:11 +1000 Subject: [PATCH] some changes to assets, make it possibly to cl a date-compatibile string to open only that file --- cl | 133 ++++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 96 insertions(+), 37 deletions(-) diff --git a/cl b/cl index 095c08b..d866b62 100755 --- a/cl +++ b/cl @@ -103,7 +103,7 @@ function review() { } function schedule() { - if [ "$1" == "edit" ]; then + if [ ${1:--1} -eq -1 ] || [ "$1" == "edit" ]; then if [ -f "$CLDIR/schedule"* ];then vi -p "$CLDIR/schedule"* @@ -148,25 +148,55 @@ function captains_log() { # Create files if they don't exist - for i in YESTERDAY TODAY TOMORROW; do - if ! [ -f "$CLDIR/${!i}" ]; then + if [ ${1-x} == 'x' ]; then + for i in YESTERDAY TODAY TOMORROW; do + generate_file ${!i} + done + if [ -f "$CLDIR/backlog" ]; then + vi $CLDIR/{$YESTERDAY,$TODAY,$TOMORROW,backlog} -s <( echo -e ":vsplit\n:wincmd w\n:next\n:vsplit\n:wincmd w\n:next\n:split\n:wincmd w\n:next") + else + vi -O $CLDIR/{$YESTERDAY,$TODAY,$TOMORROW} + fi + else + generate_file $1 + vi $CLDIR/$(date -d "$1" "+%F") + fi + + + + asset generate_links + + if [ $GIT -ne 0 ]; then + echo Updating git... + if [ $( git -C "$CLDIR" status -s |wc -l) -ne 0 ]; then + git -C "$CLDIR" add -A + git -C "$CLDIR" commit -qm "Auto-Commit by $(basename $0)" + git -C "$CLDIR" push -q + fi + fi +} + +function generate_file() { + # Create files if they don't exist + F=$(date -d "${1:-$(date +%F)}" "+%F") + + if ! [ -f "$CLDIR/${F}" ]; then # If there's a template and it's executable, execute it and output the result to the file, # If it's not executable, replate {{DATE }} with said date/formate # Otherwise do the boilerplate - debug "File $i doesn't exist, creating it" + debug "File $F doesn't exist, creating it" if [ -x "$CLDIR/template" ]; then debug "From executable template" - "$CLDIR/template" > $CLDIR/${!i} + "$CLDIR/template" > $CLDIR/${F} elif [ -f "$CLDIR/template" ]; then DATE_ARGS="$(grep '{{DATE[^}]*}}' "$CLDIR/template"|tr -d {}|cut -d' ' -f2-)" if [ "$DATE_ARGS" == 'DATE' ]; then DATE_ARGS='%A %d %B %Y'; fi debug "From static template with DATE_ARGS=$DATE_ARGS" - sed "s/{{DATE[^}]*}}/$(date "+$DATE_ARGS" -d ${!i})/g" $CLDIR/template > $CLDIR/${!i} + sed "s/{{DATE[^}]*}}/$(date "+$DATE_ARGS" -d ${F})/g" $CLDIR/template > $CLDIR/${F} else - echo -e "$(date "+%A %d %B %Y" -d ${!i})\nWhat do you want to accomplish today?\n\nWhat are your notes for today?\n\nWhat do you need to follow up tomorrow?\n" > "$CLDIR/${!i}" + echo -e "$(date "+%A %d %B %Y" -d ${!F})\nWhat do you want to accomplish today?\n\nWhat are your notes for today?\n\nWhat do you need to follow up tomorrow?\n" > "$CLDIR/${!F}" fi fi - done # Read from the "scheduled" file, and put lines into the appropriate file if extant @@ -175,21 +205,19 @@ function captains_log() { SCHEDULED=0 SCHED=$(echo $line|cut -d' ' -f1) TASK=$(echo $line|cut -d' ' -f2-) - for i in YESTERDAY TODAY TOMORROW; do - # Check if the schedule is DATE_FORMAT=MATCH_EXPRESSION otherwise assume %F - if ( [[ "$SCHED" =~ = ]] && [[ "$( date "+$(echo "$SCHED"|cut -d'=' -f1)" -d "${!i}" )" =~ $(echo $SCHED |cut -d'=' -f2-) ]] ) || ( ! [[ "$SCHED" =~ = ]] && [ "$(date +%F -d $SCHED)" == "${!i}" ] ); then - if ! grep -qE "^Scheduled to Review today:" "$CLDIR/${!i}"; then - echo "Scheduled to Review today:" >> "$CLDIR/${!i}" - fi - if ! grep -qE "^ . $TASK" "$CLDIR/${!i}";then - echo " - $TASK" >> "$CLDIR/${!i}" - fi - - if ! [[ "$SCHED" =~ = ]]; then - SCHEDULED=1 - fi + # Check if the schedule is DATE_FORMAT=MATCH_EXPRESSION otherwise assume %F + if ( [[ "$SCHED" =~ = ]] && [[ "$( date "+$(echo "$SCHED"|cut -d'=' -f1)" -d "${F}" )" =~ $(echo $SCHED |cut -d'=' -f2-) ]] ) || ( ! [[ "$SCHED" =~ = ]] && [ "$(date +%F -d $SCHED)" == "${F}" ] ); then + if ! grep -qE "^Scheduled to Review today:" "$CLDIR/${F}"; then + echo "Scheduled to Review today:" >> "$CLDIR/${F}" fi - done + if ! grep -qE "^ . $TASK" "$CLDIR/${F}";then + echo " - $TASK" >> "$CLDIR/${F}" + fi + + if ! [[ "$SCHED" =~ = ]]; then + SCHEDULED=1 + fi + fi if [ $SCHEDULED -eq 0 ]; then echo "$line" >> "$CLDIR/scheduled.tmp" fi @@ -202,28 +230,58 @@ function captains_log() { if [ -f "$CLDIR/scheduled.tmp" ]; then mv "$CLDIR/scheduled.tmp" "$CLDIR/scheduled" fi +} - if [ -f "$CLDIR/backlog" ]; then - vi $CLDIR/{$YESTERDAY,$TODAY,$TOMORROW,backlog} -s <( echo -e ":vsplit\n:wincmd w\n:next\n:vsplit\n:wincmd w\n:next\n:split\n:wincmd w\n:next") - else - vi -O $CLDIR/{$YESTERDAY,$TODAY,$TOMORROW} - fi - if [ $GIT -ne 0 ]; then - echo Updating git... - if [ $( git -C "$CLDIR" status -s |wc -l) -ne 0 ]; then - git -C "$CLDIR" add -A - git -C "$CLDIR" commit -qm "Auto-Commit by $(basename $0)" - git -C "$CLDIR" push -q + + +function asset_generate_links() { + # Either do files modified in the last 7 days, or all files + if [ "$1" == "all" ]; then + MTIME='' + else + MTIME='-mtime -7' + fi + + # Exclude things we don't care about (swap files, git, the db itself) + FILES=$(find ~/.captains_log/default/ -type f $MTIME \! \( -iname *.sw* -o -wholename "*/.git*" -wholename "*/assets/db" \)) + + # Look for references to assets + MATCHES="$(grep -oE '\[?ASSET:)?[a-zA-Z0-9]+\]?' $FILES)" + while read line; do + # Nicely format things + FILE="$(echo $line | cut -d':' -f1)" + FILE="${FILE#$CLDIR/}" + REF="$(echo $line|cut -d ':' -f2-|grep -oE '[a-zA-Z0-9]{8}')" + # Assets contain a self reference so that it can be easily copied while editing. Ignore these refs + if ! [[ "$REF" =~ $(basename $FILE 2>/dev/null) ]]; then + DB_STRING="$REF:link $FILE" + + #Add it to the DB if it's not already there + if ! grep -q "$DB_STRING" $ADB 2>/dev/null; then + echo $DB_STRING >> $ADB + fi fi - fi + done < <(echo "$MATCHES") + + + } + function asset() { - ASSET_DIR="$CLDIR/assets/" - ADB="$ASSET_DIR/db" + export ASSET_DIR="$CLDIR/assets/" + export ADB="$ASSET_DIR/db" + + while [ $1 ]; do + case $1 in + generate_links) shift; asset_generate_links $@; return;; + *) break;; + esac + done + if [ ${1:-0} != 0 ]; then if [[ $1 =~ (\[?ASSET:)?[a-zA-Z0-9]+\]? ]]; then ID="$(echo "$1"|grep -oE '[a-zA-Z0-9]{8}')" @@ -275,9 +333,10 @@ while [ $1 ]; do review) shift; review "$@"; exit;; schedule) shift; schedule "$@";exit;; asset) shift; asset "$@"; exit;; + *ASSET*) asset "$@"; exit;; help|--help|-h) help; exit;; *) break;; esac done -captains_log +captains_log $@