-
Notifications
You must be signed in to change notification settings - Fork 0
/
til
executable file
·78 lines (65 loc) · 1.92 KB
/
til
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/bin/bash
# Date, Time and the TimeZone
TODAY=`date +%Y-%m-%d`
DEFAULT_EDITOR="vim"
DEFAULT_REMOTE="origin"
DEFAULT_BRANCH="master"
if [ "$#" -eq 1 ] && [ $1 == "commit" ]; then
git add -A
git commit -m "This is what I learned on $TODAY."
read -r -p "Cool! Do you want to push it to repo? [Y/n] " response
if [ -z $response ] || [[ $response =~ ^(yes|y|Y|YES) ]]; then
git push $DEFAULT_REMOTE $DEFAULT_BRANCH
fi
echo -e "\033[92mKeep moving forward! >>>"
exit 0
fi
if [ "$#" -lt 2 ]; then
echo "You must enter at least 2 arguments, first is category, next one is subject, and the last is your Editor (default: $DEFAULT_EDITOR)"
exit 1
fi
CATEGORY=${1// /-}
SUBJECT=$2
## In case you dont wanna type the quote mark when call til, just use
## the - or = for word seperator.
case $SUBJECT in
*-*)
FILENAME=$SUBJECT
SUBJECT=${SUBJECT//-/ }
;;
*=*)
SUBJECT=${SUBJECT//=/ }
FILENAME=${SUBJECT// /-}
;;
*\ *)
FILENAME=${SUBJECT// /-}
;;
*)
FILENAME=${SUBJECT}
;;
esac
EDITOR=${3:-$DEFAULT_EDITOR}
## Filename: The same as subject, except for replace all space with dash
## and lowercase all chars.
FILENAME=${FILENAME,,}
MDFILE=$CATEGORY/$FILENAME.md
[ -d $CATEGORY ] || mkdir $CATEGORY
[ -f $MDFILE ] && echo -e "\n---\n" >> $MDFILE
echo "- Date : $TODAY" >> $MDFILE
echo "- Tags : #$CATEGORY" >> $MDFILE
# Avoid to add a addition newline
echo -e "\n\c" >> $MDFILE
echo "## $SUBJECT" >> $MDFILE
echo -e "\n\n" >> $MDFILE
echo -e "\n\c"
echo -e "Tell me what you learned today about '$CATEGORY: $SUBJECT' ;)\nThen commit it by \033[92m./til commit"
echo -e "\n\c"
sleep 2
# Check if the editor is the default one then launch it and put the cursor exactly at edit point
if [ $EDITOR == $DEFAULT_EDITOR ]
then
let "EDITLINE=`wc -l $MDFILE | cut -d" " -f1` - 1"
$EDITOR +$EDITLINE $MDFILE
else
$EDITOR $MDFILE
fi