-
Notifications
You must be signed in to change notification settings - Fork 2
/
daryl
executable file
·169 lines (146 loc) · 4.38 KB
/
daryl
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/bin/bash
# Copyright (C) 2019 Constantin Clauzel <[email protected]>
# Author: Constantin Clauzel <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
RED="\033[0;31m"
GREEN="\033[0;32m"
CYAN="\033[0;36m"
YELLOW="\033[0;33m"
NC="\033[0m"
pushd () {
command pushd "$@" > /dev/null
}
popd () {
command popd "$@" > /dev/null
}
set_current () {
if [ -n "$FORCE_CURRENT" ]; then
CURRENT="${FORCE_CURRENT/.txt/}.txt"
else
if ! [ -e "$DIR/current" ]; then
echo "Call 'daryl r' first. Or set FORCE_CURRENT variable (by doing 'FORCE_CURRENT=214124421421.txt d c' for ex, do a 'd a' to find note names)."
exit
fi
CURRENT="$(cat $DIR/current)"
fi
FILE_PATH="$DIR/$CURRENT"
}
if [ $# -lt 1 ]; then
echo "Cannot be called without parameters."
echo "Please see https://github.com/vitaminwater/Daryl for some documentation."
exit
fi
DIR="$HOME/.daryl"
DONE_DIR="$DIR/.done"
# get random note, store it as the current
if [ "$1" == "r" ]; then
R=$(ls $DIR | grep -v current | sort -R | tail -1)
echo -e "$(date -d @${R/.txt/}) - ${GREEN}$R${NC}"
cat $DIR/$R
printf "$R" > $DIR/current
# prints the current note or sets the second parameter as the new current note, second parameter should be the name of the timestamp of the note
elif [ "$1" == "c" ]; then
# Sets the current note when second parameter is a note timestamp
if [ $# == 2 ]; then
NEW_CURRENT="${2/.txt/}.txt"
if [ -e "$DIR/$NEW_CURRENT" ]; then
printf "$NEW_CURRENT" > $DIR/current
else
echo -e "${RED}Can't find the note $NEW_CURRENT.${NC}"
exit
fi
fi
set_current
echo -e "$(date -d @${CURRENT/.txt/}) - ${GREEN}$CURRENT${NC}\n$(cat $FILE_PATH)" | less -FRSXc~
# Dump all notes
elif [ "$1" == "a" ]; then
j=1
if [ $# == 2 ]; then
FILES="$(grep -l -i -E "$2" $DIR/*.txt)"
echo $FILES
else
FILES="$(ls $DIR/*.txt)"
fi
for i in $FILES; do
NOTE=$(basename $i)
echo -e "#$j - $(date -d @${NOTE/.txt/}) - ${GREEN}$NOTE${NC}"
cat $i
echo -e "${YELLOW}---------------------------------------${NC}"
let j=$j+1
done | less -FRSXc~
# open current in $EDITOR
elif [ "$1" == "e" ]; then
if [ -z "$EDITOR" ]; then
EDITOR=vi # default to vi ?
fi
set_current
$EDITOR "$FILE_PATH"
# add log to a note
elif [ "$1" == "--" ]; then
set_current
NOTE="${@:2}"
echo -e "========= ${CYAN}Logged on $(date)${NC} =========" >> $FILE_PATH
echo "$NOTE" >> $FILE_PATH
echo "" >> $FILE_PATH
echo -e "Added log to $CURRENT: ${GREEN}ok${NC}"
# end the note stored as current
elif [ "$1" == "d" ]; then
set_current
printf "The note $CURRENT will be ${RED}marked as done${NC}, sure ? (y/N): "
read confirm
if [ "$confirm" != "y" ]; then
echo -e "${RED}Abort${NC}"
exit
fi
# TODO DRY with the '--' command
echo "========= ${CYAN}Logged on $(date)${NC} =========" >> $FILE_PATH
echo "Done" >> $FILE_PATH
echo "" >> $FILE_PATH
echo -e "Added log 'Done' to $CURRENT: ${GREEN}ok${NC}"
mkdir -p $DONE_DIR
mv $FILE_PATH $DONE_DIR/
echo -e "Marked note $FILE_PATH as done: ${GREEN}ok${NC}"
if [ "$CURRENT" == "$(cat $DIR/current)" ]; then
rm $DIR/current
fi
# Github sync
elif [ "$1" == "s" ]; then
if ! [ -e "$DIR/.git" ]; then
echo -e "Git repository is not configured in $DIR"
exit
fi
pushd $DIR
git add .
git commit -am 'Sync command'
git pull
git push
echo -e "Syncing to $(git remote get-url origin): ${GREEN}ok${NC}"
popd
# else create new note
else
NOTE="$*"
TS=`date +"%s"`
FILE_PATH="$DIR/$TS.txt"
mkdir -p $DIR
printf "Creating note ${GREEN}$TS.txt${NC}, sure ? (Y/n): "
read confirm
if [ "$confirm" == "n" ]; then
echo -e "${RED}Abort${NC}"
exit
fi
echo $NOTE > $FILE_PATH
echo "" >> $FILE_PATH
echo -e "Saving as $FILE_PATH: ${GREEN}ok${NC}"
fi