-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.hsrc
205 lines (180 loc) · 4.29 KB
/
.hsrc
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/bin/bash
# -*- mode: shell-script; -*-
HISTIGNORE="$HISTIGNORE:hs *:hl:hl *:he *:hd *:hcat *:&:"
hs_mknewscript(){
local newsnipname="$1"
echo "# -*- mode: shell-script; -*- " > "${newsnipname}"
chmod +x "${newsnipname}"
}
hs_checkinit(){
[ -d ~/.hs/ ] || mkdir -p ~/.hs
[ -d ~/.hs/.git ] || (cd ~/.hs || exit; git init . )
[ -f ~/.hs/.gitignore ] || {
cat > ~/.hs/.gitignore <<EOF
*~
EOF
}
}
hs_usage() {
echo "hs:"
echo " hs newsnipname"
echo " hs newsnipname <ncmds>"
echo " hs newsnipname <ncmds> <filter>"
}
hs() {
# 1: name of new snip
# 2: number of commands to get
# 3: pattern to filter commands
hs_checkinit
local label="$1"
local ncmds=$2
local filter="$3"
fn=$(hs_expandfilearg "$1")
[ $? -ne 0 ] && {
echo "use he to edit existing snips: $fn"
return
}
if [[ "$1" = "" ]]; then
echo "snipname required."
hs_usage
return
fi
fn=~/.hs/"$label"
hs_mknewscript "$fn"
if [ "$2" = "" ]; then
ncmds=10
fi
if [ -n "$3" ]; then
HISTTIMEFORMAT="" history $ncmds | cut -d ' ' -f 4- | grep "${filter}" >> "$fn"
else
HISTTIMEFORMAT="" history $ncmds | cut -d ' ' -f 4- >> "$fn"
fi
pushd ~/.hs > /dev/null || return
$EDITOR "$fn"
git add "$fn"
git commit -m "created by hs"
popd > /dev/null || return
}
hs_expandfilearg() {
shopt -s nullglob
pushd ~/.hs > /dev/null || {
echo "pushd ~/.hs failed?"
return
}
files=$(git ls-files [^.]* | grep "${1}")
filessep=$(echo "$files" | sort | tr '\n' ' ')
popd > /dev/null || {
echo "popd failed?"
return
}
argl=($filessep)
[ ${#argl[@]} -eq 0 ] && {
echo "Couldn't find $1."
return 0
}
[ ${#argl[@]} -eq 1 ] || [ "${argl[0]}" = "$1" ] && {
echo "${argl[0]}"
return 1
}
echo "${#argl[@]} matches for $1: $files."
return ${#argl[@]}
}
hr() {
[ -z "$1" ] && {
echo "nothing to run"
return
}
fn=$(hs_expandfilearg "$1")
[ $? -ne 1 ] && {
echo "$fn"
return
}
fn="$HOME/.hs/$fn"
hr_start=$(date '+%s')
shift
set -- $@
. "$fn" "$@"
rv=$?
hr_finish=$(date '+%s')
pushd ~/.hs/ > /dev/null || return
lastchangerev=$(git rev-list -1 --all "$fn")
ts=$(date)
git notes append -m "ran $(basename "$fn") on ${ts}, returned ${rv} after $(( hr_finish - hr_start )) seconds" "$lastchangerev"
popd > /dev/null || return
return $rv
}
hlog() {
fn=$(hs_expandfilearg "$1")
pushd ~/.hs > /dev/null || return
git log "$fn"
popd > /dev/null || return
}
hl() {
pat=$1
pushd ~/.hs > /dev/null || return
if [ -n "$pat" ]; then
hfiles=$(git ls-files [^.]* | grep "$pat")
else
hfiles=$(git ls-files [^.]*)
fi
for f in $hfiles ; do
desc=$(awk 'FNR == 2 { print;}' ~/.hs/"$f")
printf "%-20s $desc\n" "$f"
done
popd > /dev/null || return
}
hd() {
hs_checkinit
fn=$(hs_expandfilearg "$1")
[ $? -ne 1 ] && {
echo "can't delete: $fn"
return
}
pushd ~/.hs > /dev/null || return
git rm "$fn"
git commit -m"removed by hd"
popd > /dev/null || return
}
# he edits only existing snips
he() {
[ -z "$EDITOR" ] && {
echo "no EDITOR set"
return
}
hs_checkinit
fn=$(hs_expandfilearg "$1")
[ $? -ne 1 ] && {
echo "couldn't find a unique existing snip named that, say more: $fn"
return
}
pushd ~/.hs > /dev/null || return
$EDITOR "$fn"
git add "$fn"
git commit -m "edited by he"
popd > /dev/null || return
}
hcat() {
hs_checkinit
fn=$(hs_expandfilearg "$1")
[ $? -ne 1 ] && {
echo "$fn"
return
}
cat ~/.hs/"$fn"
}
_complete_hsfiles() {
local cur prev opts base
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
pushd ~/.hs > /dev/null || return
hsfiles=$(git ls-files [^.]*)
popd > /dev/null || return
COMPREPLY=($(compgen -W "${hsfiles}" -- ${cur}))
return 0
}
complete -F _complete_hsfiles hr
complete -F _complete_hsfiles he
complete -F _complete_hsfiles hcat
complete -F _complete_hsfiles hd
complete -F _complete_hsfiles hlog