Skip to content

Instantly share code, notes, and snippets.

@knutholst
Last active February 4, 2019 06:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save knutholst/a1663b55b6346978d2b84810427b23da to your computer and use it in GitHub Desktop.
Save knutholst/a1663b55b6346978d2b84810427b23da to your computer and use it in GitHub Desktop.
DID
#!/bin/bash
# What did i do today?
# from https://theptrk.com/2018/07/11/did-txt-file/
# original from https://gist.github.com/alexisjanvier/bfe71d18f68434e29c08637e4d837c74
export MDV_THEME=729.8953
export DID_PATH=~/.did
function did(){
export LC_ALL=C
mkdir -p ${DID_PATH}
if [ ! -f ${DID_PATH}/$(date +%Y-%V).md ]; then
echo "# Week $(date +"%V (%B %Y)") \n\n## $(date +"%A %Y-%m-%d")" > ${DID_PATH}/$(date +%Y-%V).md
fi
FILE_EDITION_DATE="$(stat -c "%y" ${DID_PATH}/$(date +%Y-%V).md)"
NOW="$(date +"%Y-%m-%d")"
if [ ${FILE_EDITION_DATE:0:10} != ${NOW} ]
then
echo "\n## $(date +"%A %Y-%m-%d")\n" >> ${DID_PATH}/$(date +%Y-%V).md
fi
unset LC_ALL
vim +'normal Go' ${DID_PATH}/$(date +%Y-%V).md
}
function didv(){
if [ $1 ]
then
vmd ${DID_PATH}/${2}.md
else
mkdir -p ${DID_PATH}
if [ ! -f ${DID_PATH}/$(date +%Y-%V).md ]; then
LC_ALL=C echo "# Week $(date +"%V (%B %Y)") \n\n## $(date +"%A %Y-%m-%d")" > ${DID_PATH}/$(date +%Y-%V).md
fi
vmd ${DID_PATH}/$(date +%Y-%V).md
fi
}
function week2Month(){
export LC_ALL=C
year=$(echo $1 | cut -f1 -d-)
week=$(echo $1 | cut -f2 -d-)
local dayofweek=1 # 1 for monday
date -d "$year-01-01 +$(( $week * 7 + 1 - $(date -d "$year-01-04" +%w ) - 3 )) days -2 days + $dayofweek days" +"%B %Y"
unset LC_ALL
}
function didl(){
for file in `ls ${DID_PATH}/*.md | sort -Vr`; do
filenameRaw="$(basename ${file})"
filename="${filenameRaw%.*}"
echo "${filename} ($(week2Month ${filename}))"
done
}
function dids(){
export LC_ALL=C
if [ $1 ]
then
for file in `ls ${DID_PATH}/*.md | sort -Vr`; do
NB_OCCURENCE="$(grep -c ${2} ${file})"
if [ ${NB_OCCURENCE} != "0" ]
then
filenameRaw="$(basename ${file})"
filename="${filenameRaw%.*}"
echo -e "\n\e[32m=> ${filename} ($(week2Month ${filename}), ${NB_OCCURENCE} results) \e[0m" && grep -n -B 1 ${2} ${file}
fi
done
else
echo "You must add a something to search..."
fi
unset LC_ALL
}
case "$1" in
'view')
didv
;;
'list')
didl
;;
'search')
dids
;;
*)
did
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment