Compare commits

..

2 Commits

  1. 158
      cl

158
cl

@ -35,6 +35,10 @@ $0 - Captain's log
Directories created under $BASEDIR are treated as profiles (if they exist). The default profile is assumed to be 'default'. Directories created under $BASEDIR are treated as profiles (if they exist). The default profile is assumed to be 'default'.
Specify -p <profile> as the first argument to $0 to operate on a different one Specify -p <profile> as the first argument to $0 to operate on a different one
Run with "asset" to spin up a new empty file with an asset tag. This is currently assumed to be plain text that complements the log, but makes little sense to included.
At some point this will be expanded to handle other files
Run with "asset <ID>" to open said asset.
Run with "review" ($0 review), and you will get a rundown of complete and incomplete tasks for the week. Run with "review" ($0 review), and you will get a rundown of complete and incomplete tasks for the week.
Optionally, supply with date-compatible strings as "$0 review <start> [end] to use a range, end defaults to yesterday Optionally, supply with date-compatible strings as "$0 review <start> [end] to use a range, end defaults to yesterday
@ -58,7 +62,6 @@ EOF
} }
function review() { function review() {
shift
DATE_START=${1:-"-7 days"} DATE_START=${1:-"-7 days"}
DATE_END=${2:-"yesterday"} DATE_END=${2:-"yesterday"}
@ -100,8 +103,7 @@ function review() {
} }
function schedule() { function schedule() {
shift if [ ${1:--1} -eq -1 ] || [ "$1" == "edit" ]; then
if [ "$1" == "edit" ]; then
if [ -f "$CLDIR/schedule"* ];then if [ -f "$CLDIR/schedule"* ];then
vi -p "$CLDIR/schedule"* vi -p "$CLDIR/schedule"*
@ -146,25 +148,55 @@ function captains_log() {
# Create files if they don't exist # Create files if they don't exist
if [ ${1-x} == 'x' ]; then
for i in YESTERDAY TODAY TOMORROW; do for i in YESTERDAY TODAY TOMORROW; do
if ! [ -f "$CLDIR/${!i}" ]; then 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 there's a template and it's executable, execute it and output the result to the file,
# If it's not executable, replate {{DATE <date-compatible formate>}} with said date/formate # If it's not executable, replate {{DATE <date-compatible formate>}} with said date/formate
# Otherwise do the boilerplate # 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 if [ -x "$CLDIR/template" ]; then
debug "From executable template" debug "From executable template"
"$CLDIR/template" > $CLDIR/${!i} "$CLDIR/template" > $CLDIR/${F}
elif [ -f "$CLDIR/template" ]; then elif [ -f "$CLDIR/template" ]; then
DATE_ARGS="$(grep '{{DATE[^}]*}}' "$CLDIR/template"|tr -d {}|cut -d' ' -f2-)" DATE_ARGS="$(grep '{{DATE[^}]*}}' "$CLDIR/template"|tr -d {}|cut -d' ' -f2-)"
if [ "$DATE_ARGS" == 'DATE' ]; then DATE_ARGS='%A %d %B %Y'; fi if [ "$DATE_ARGS" == 'DATE' ]; then DATE_ARGS='%A %d %B %Y'; fi
debug "From static template with DATE_ARGS=$DATE_ARGS" 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 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
fi fi
done
# Read from the "scheduled" file, and put lines into the appropriate file if extant # Read from the "scheduled" file, and put lines into the appropriate file if extant
@ -173,21 +205,19 @@ function captains_log() {
SCHEDULED=0 SCHEDULED=0
SCHED=$(echo $line|cut -d' ' -f1) SCHED=$(echo $line|cut -d' ' -f1)
TASK=$(echo $line|cut -d' ' -f2-) 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 # 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 ( [[ "$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/${!i}"; then if ! grep -qE "^Scheduled to Review today:" "$CLDIR/${F}"; then
echo "Scheduled to Review today:" >> "$CLDIR/${!i}" echo "Scheduled to Review today:" >> "$CLDIR/${F}"
fi fi
if ! grep -qE "^ . $TASK" "$CLDIR/${!i}";then if ! grep -qE "^ . $TASK" "$CLDIR/${F}";then
echo " - $TASK" >> "$CLDIR/${!i}" echo " - $TASK" >> "$CLDIR/${F}"
fi fi
if ! [[ "$SCHED" =~ = ]]; then if ! [[ "$SCHED" =~ = ]]; then
SCHEDULED=1 SCHEDULED=1
fi fi
fi fi
done
if [ $SCHEDULED -eq 0 ]; then if [ $SCHEDULED -eq 0 ]; then
echo "$line" >> "$CLDIR/scheduled.tmp" echo "$line" >> "$CLDIR/scheduled.tmp"
fi fi
@ -200,23 +230,91 @@ function captains_log() {
if [ -f "$CLDIR/scheduled.tmp" ]; then if [ -f "$CLDIR/scheduled.tmp" ]; then
mv "$CLDIR/scheduled.tmp" "$CLDIR/scheduled" mv "$CLDIR/scheduled.tmp" "$CLDIR/scheduled"
fi 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")
function asset_generate_links() {
# Either do files modified in the last 7 days, or all files
if [ "$1" == "all" ]; then
MTIME=''
else else
vi -O $CLDIR/{$YESTERDAY,$TODAY,$TOMORROW} MTIME='-mtime -7'
fi fi
if [ $GIT -ne 0 ]; then # Exclude things we don't care about (swap files, git, the db itself)
echo Updating git... FILES=$(find ~/.captains_log/default/ -type f $MTIME \! \( -iname *.sw* -o -wholename "*/.git*" -wholename "*/assets/db" \))
if [ $( git -C "$CLDIR" status -s |wc -l) -ne 0 ]; then
git -C "$CLDIR" add -A # Look for references to assets
git -C "$CLDIR" commit -qm "Auto-Commit by $(basename $0)" MATCHES="$(grep -oE '\[?ASSET:)?[a-zA-Z0-9]+\]?' $FILES)"
git -C "$CLDIR" push -q 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
done < <(echo "$MATCHES")
}
function asset() {
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}')"
vi "$ASSET_DIR/$ID"
fi
else
if ! [ -d "$ASSET_DIR" ]; then
mkdir "$ASSET_DIR"
touch $ADB
fi fi
# Generate a random ID that doesn't already exist
while grep -qi "$ID" "${ADB}"; do
ID="$(openssl rand -hex 4)"
# Alternatively
#hexdump -n 4 -e '/4 "%08X" 1 "\n"' /dev/urandom
done
echo "$ID:type text" >> "$ADB"
echo "[ASSET:$ID]" > "$ASSET_DIR/$ID"
vi "$ASSET_DIR/$ID"
fi fi
} }
BASEDIR="$HOME/.captains_log" BASEDIR="$HOME/.captains_log"
# Check if there are any profiles, otherwise just use the basedir # Check if there are any profiles, otherwise just use the basedir
@ -225,16 +323,20 @@ if [ "$(find $BASEDIR -type d|wc -l)" -gt 1 ]; then
else else
CLDIR="$BASEDIR" CLDIR="$BASEDIR"
fi fi
while [ $1 ]; do while [ $1 ]; do
case $1 in case $1 in
-p|--profile) CLDIR="$BASEDIR/$2"; shift 2;; -p|--profile) CLDIR="$BASEDIR/$2"; shift 2;;
--no-git) GIT=0;shift;; --no-git) GIT=0;shift;;
--debug) DEBUG=1; shift;; --debug) DEBUG=1; shift;;
review) review "$@"; exit;; review) shift; review "$@"; exit;;
schedule) schedule "$@";exit;; schedule) shift; schedule "$@";exit;;
asset) shift; asset "$@"; exit;;
*ASSET*) asset "$@"; exit;;
help|--help|-h) help; exit;; help|--help|-h) help; exit;;
*) break;; *) break;;
esac esac
done done
captains_log captains_log $@

Loading…
Cancel
Save