-
Notifications
You must be signed in to change notification settings - Fork 0
/
lb
executable file
·141 lines (122 loc) · 4.13 KB
/
lb
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
#!/bin/bash
# log-book function, creates a template for working through problems.
# Requires a folder called logbook in the home directory
# Usage: lb <problem summary>
function open_file {
FILENAME=$1
LINE_JUMP=${2:-0}
# if using some type of vim, go to the line where the current logbook begins
# via the '+' parameter
case ${EDITOR:-vi} in
vi|nvim|vim) # vim and variants
$EDITOR $FILENAME \+$LINE_JUMP
;;
subl|atom) # Sublime Text and Atom
$EDITOR $FILENAME:$LINE_JUMP
;;
idea) # IntelliJ IDE
$EDITOR --line $LINE_JUMP $FILENAME
;;
*)
${EDITOR:-vi} $FILENAME
;;
esac
}
function list {
grep -B1 "^=\+" $LOGBOOK_DIR/*.md | grep "md-" | grep -Eo "/[0-9]{4}-[0-9]{2}-[0-9]{2}.*" | sed -e 's#\.md\-#|#' -e 's#^/##g' | awk -F'|' '$1!=prev{print $1;section+=1;entry=0;}{entry+=1; prev=$1; print "\t"section"."entry" "$2;}'
}
function entry_number_to_location {
entry_no=$1
SECTION_ENTRY=$(echo $entry_no | sed 's/\./\\./')
ENTRY_TITLE=$(list | grep "^\s$SECTION_ENTRY" | sed -e 's#[[:space:]][0-9]*\.[0-9]* ##')
fgrep -n "$ENTRY_TITLE" $LOGBOOK_DIR/*.md | grep -v "^README.md" | awk -F':' '{print $1, $2}'
}
if [ -z "$LOGBOOK_DIR" ]
then
LOGBOOK_DIR=$HOME/logbook
fi
# create directory if it doesn't exist
mkdir -p $LOGBOOK_DIR
now=$(date '+%Y-%m-%d')
# if no parameters provided and logbook entries exist, open the logbook
# with the maximum date.
if [[ $# -eq 0 && $(ls $LOGBOOK_DIR | grep "^[0-9].*md$" | wc -l) -gt 0 ]]
then
LATEST_ENTRY=$LOGBOOK_DIR/$(ls $LOGBOOK_DIR | grep "^[0-9].*md$" | sort | tail -n1)
LATEST_UNFINISHED_ENTRY_LNO=$(nl -ba $LATEST_ENTRY | grep -E "(Completed|\s=+)" | grep -B1 "<use" | head -n1 | awk '{print $1-1}')
open_file $LATEST_ENTRY $LATEST_UNFINISHED_ENTRY_LNO
exit 0
elif [[ $# -eq 1 && ("$1" == "--help" || "$1" == "-h") ]]
then
echo "lb: a logbook utility
Usage: lb <title> | <command>
Commands:
lb <title>
creates a new entry in the logbook
lb list
lists the current logbook etries you've created, also annotates with [entry-number]s
lb goto [entry-number]
jumps to the specified logbook entry
lb link [entry-number ...]
provides links to the github/gitlab web pages where the entry can be found
lb search [grep-options] PATTERN
runs grep against your logbook entries
"
exit 0
elif [[ $# -eq 1 && "$1" == "list" ]]
then
list
exit 0
elif [[ $# -gt 1 && $1 == "link" ]]
then
if [ ! -d "${LOGBOOK_DIR}/.git" ]
then
echo "Logbook dir ($LOGBOOK_DIR) is not a git respository."
exit 1
fi
GIT_BASE_DIR=$(git --git-dir=$LOGBOOK_DIR/.git --work-tree=$LOGBOOK_DIR config --get remote.origin.url | awk -F'[@:/]+' '
BEGIN{
OFS="/"
}/^ssh/{
print $3, $5, $6
}/^http/{
print $2
}/^git/{
print $2, $3, $4
}' | sed -e 's#\.git$##' -e 's#$#/blob/master/#')
for entry_no in "${@:2}"
do
LOCATION=$(entry_number_to_location $entry_no)
echo "http://"${GIT_BASE_DIR}$(echo $LOCATION | awk -F'[/ ]' '{print $(NF-1)}')"#L"$(echo $LOCATION | awk '{print $2}')
done
exit 0
elif [[ $# -gt 1 && $1 == "goto" ]]
then
# escape any dots to prepare for grep
LOCATION=$(entry_number_to_location $2)
open_file $(echo $LOCATION | awk '{print $1}') $(echo $LOCATION | awk '{print $2}')
exit 0
elif [[ $# -gt 1 && $1 == "search" ]]
then
grep -n ${@:2} $LOGBOOK_DIR/*.md
exit 0
elif [[ $# -eq 1 ]]
then
printf "\n$1
$(printf "=%.0s" $(seq 1 ${#1}))
Started: $(date '+%Y-%m-%d %H:%M:%S')\n
### 1. Consider the problem you’re attempting to solve\n
### 2. Describe your method for solving it\n
### 3. Record what happened\n
### 4. How could it be improved?\n
Completed: <use: date '+%%Y-%%m-%%d %%H:%%M:%%S'>" >> $LOGBOOK_DIR/$now.md
elif [[ $# -gt 1 ]]
then
echo "Error: too many parameters"
echo "Usage: lb [<problem summary>]"
exit 1
else
echo "No entries found in $LOGBOOK_DIR. Create and entry via: lb <title>"
exit 1
fi
open_file $LOGBOOK_DIR/$now.md $(nl -b a $LOGBOOK_DIR/$now.md | grep "$1$" | awk '{print $1}')