Implement optional grouping by day of the week
[khome.git] / home / lib / login_functions.sh
1 #
2
3 d() {
4 local -r word=$(fzf < /usr/share/dict/words)
5 dict "$word"
6 }
7
8 shell_activity_report() {
9 # TODO: optional concrete number output
10 # TODO: manual weekday calc (since forking date is so expensive)
11 # TODO: optional combinations of granularities: hour, weekday, month, year
12 local group_by="$1"
13 case "$group_by" in
14 'mon') ;;
15 'dow') ;;
16 '') group_by='dow';;
17 *)
18 echo "Usage: $0 DIRECTORY [mon|dow]" >&2
19 exit 1
20 esac
21 history \
22 | awk -v group_by="$group_by" '
23 function date2dow(y, m, d, _t, _i) {
24 # Contract:
25 # y > 1752, 1 <= m <= 12.
26 # Source:
27 # Sakamoto`s methods
28 # https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week#Sakamoto%27s_methods
29 _t[ 0] = 0
30 _t[ 1] = 3
31 _t[ 2] = 2
32 _t[ 3] = 5
33 _t[ 4] = 0
34 _t[ 5] = 3
35 _t[ 6] = 5
36 _t[ 7] = 1
37 _t[ 8] = 4
38 _t[ 9] = 6
39 _t[10] = 2
40 _t[11] = 4
41 y -= m < 3
42 _i = int(y + y/4 - y/100 + y/400 + _t[m - 1] + d) % 7
43 _i = _i == 0 ? 7 : _i # Make Sunday last
44 return _i
45
46 }
47
48 {
49 # NOTE: $2 & $3 are specific to oh-my-zsh history output
50 date = $2
51 time = $3
52 d_fields = split(date, d, "-")
53 t_fields = split(time, t, ":")
54 if (t_fields && d_fields) {
55 # +0 to coerce number from string
56 year = d[1] + 0
57 month = d[2] + 0
58 day = d[3] + 0
59 hour = t[1] + 0
60 dow = date2dow(year, month, day)
61 g = group_by == "mon" ? month : dow # dow is default
62 c = count[g, hour]++
63 }
64 if (c > max)
65 max = c
66 }
67
68 END {
69 w[1] = "Monday"
70 w[2] = "Tuesday"
71 w[3] = "Wednesday"
72 w[4] = "Thursday"
73 w[5] = "Friday"
74 w[6] = "Saturday"
75 w[7] = "Sunday"
76
77 m[ 1] = "January"
78 m[ 2] = "February"
79 m[ 3] = "March"
80 m[ 4] = "April"
81 m[ 5] = "May"
82 m[ 6] = "June"
83 m[ 7] = "July"
84 m[ 8] = "August"
85 m[ 9] = "September"
86 m[10] = "October"
87 m[11] = "November"
88 m[12] = "December"
89
90 n = group_by == "mon" ? 12 : 7 # dow is default
91
92 for (gid = 1; gid <= n; gid++) {
93 group = group_by == "mon" ? m[gid] : w[gid]
94 printf "%s\n", group;
95 for (hour=0; hour<24; hour++) {
96 c = count[gid, hour]
97 printf " %2d ", hour
98 for (i = 1; i <= (c * 100) / max; i++)
99 printf "|"
100 printf "\n"
101 }
102 }
103 }'
104 }
105
106 top_commands() {
107 history \
108 | awk '
109 {
110 count[$4]++
111 }
112
113 END {
114 for (cmd in count)
115 print count[cmd], cmd
116 }' \
117 | sort -n -r -k 1 \
118 | head -50 \
119 | awk '
120 {
121 cmd[NR] = $2
122 c = count[NR] = $1 + 0 # + 0 to coerce number from string
123 if (c > max)
124 max = c
125 }
126
127 END {
128 for (i = 1; i <= NR; i++) {
129 c = count[i]
130 printf "%s %d ", cmd[i], c
131 scaled = (c * 100) / max
132 for (j = 1; j <= scaled; j++)
133 printf "|"
134 printf "\n"
135 }
136 }' \
137 | column -t
138 }
139
140 # Top Disk-Using directories
141 # TODO: Consider using numfmt instead of awk
142 tdu() {
143 du "$1" \
144 | sort -n -k 1 -r \
145 | head -50 \
146 | awk '
147 {
148 size = $1
149 path = $0
150 sub("^" $1 "\t+", "", path)
151 gb = size / 1024 / 1024
152 printf("%f\t%s\n", gb, path)
153 }' \
154 | cut -c 1-115
155 }
156
157 # Top Disk-Using Files
158 tduf() {
159 find "$1" -type f -printf '%s\t%p\0' \
160 | sort -z -n -k 1 -r \
161 | head -z -n 50 \
162 | gawk -v RS='\0' '
163 {
164 size = $1
165 path = $0
166 sub("^" $1 "\t+", "", path)
167 gb = size / 1024 / 1024 / 1024
168 printf("%f\t%s\n", gb, path)
169 }'
170 }
171
172 # Most-recently modified file system objects
173 recent() {
174 # NOTES:
175 # - intentionally not quoting the parameters, so that some can be ignored
176 # if not passed, rather than be passed to find as an empty string;
177 # - %T+ is a GNU extension;
178 # - gawk is able to split records on \0, while awk cannot.
179 find $@ -printf '%T@ %T+ %p\0' \
180 | tee >(gawk -v RS='\0' 'END { printf("[INFO] Total found: %d\n", NR); }') \
181 | sort -z -k 1 -n -r \
182 | head -n "$(stty size | awk 'NR == 1 {print $1 - 5}')" -z \
183 | gawk -v RS='\0' '
184 {
185 sub("^" $1 " +", "") # Remove epoch time
186 sub("+", " ") # Blank-out the default separator
187 sub("\\.[0-9]+", "") # Remove fractional seconds
188 print
189 }'
190 }
191
192 recent_dirs() {
193 recent "$1" -type d
194 }
195
196 recent_files() {
197 recent "$1" -type f
198 }
199
200 pa_def_sink() {
201 pactl info | awk '/^Default Sink:/ {print $3}'
202 }
203
204 void_pkgs() {
205 curl "https://xq-api.voidlinux.org/v1/query/x86_64?q=$1" | jq '.data'
206 }
207
208 # Colorful man
209 man() {
210 LESS_TERMCAP_md=$'\e[01;31m' \
211 LESS_TERMCAP_me=$'\e[0m' \
212 LESS_TERMCAP_se=$'\e[0m' \
213 LESS_TERMCAP_so=$'\e[01;44;33m' \
214 LESS_TERMCAP_ue=$'\e[0m' \
215 LESS_TERMCAP_us=$'\e[01;32m' \
216 command man "$@"
217 }
218
219 experiment() {
220 cd "$(~/bin/experiment $@)" || exit 1
221 }
222
223 hump() {
224 ledit -l "$(stty size | awk '{print $2}')" ocaml $@
225 }
226
227 howto() {
228 cat "$(find ~/Archives/Documents/HOWTOs -mindepth 1 -maxdepth 1 | sort | fzf)"
229 }
230
231 yt() {
232 local _yt_uri
233 local _yt_id
234 local _yt_title
235 local _yt_dir
236
237 _yt_uri="$1"
238 _yt_id=$(youtube-dl --get-id "$_yt_uri")
239 _yt_title=$(youtube-dl --get-title "$_yt_uri")
240 _yt_dir="${DIR_YOUTUBE}/individual-videos/${_yt_title}--${_yt_id}"
241
242 mkdir -p "$_yt_dir"
243 cd "$_yt_dir" || exit 1
244 echo "$_yt_uri" > 'uri'
245 youtube-dl -c --write-description --write-info-json "$_yt_uri"
246 }
247
248 gh_fetch_repos() {
249 curl "https://api.github.com/$1/$2/repos?page=1&per_page=10000"
250 }
251
252 gh_clone() {
253 gh_user_type="$1"
254 gh_user_name="$2"
255 gh_dir="${DIR_GITHUB}/${gh_user_name}"
256 mkdir -p "$gh_dir"
257 cd "$gh_dir" || exit 1
258 gh_fetch_repos "$gh_user_type" "$gh_user_name" \
259 | jq --raw-output '.[] | select(.fork | not) | .git_url' \
260 | parallel -j 25 \
261 git clone {}
262 }
263
264 gh_clone_user() {
265 gh_clone 'users' "$1"
266 }
267
268 gh_clone_org() {
269 gh_clone 'orgs' "$1"
270 }
271
272 gh_clone_repo() {
273 gh_username=$(echo "$1" | awk -F / '"$1 == "https" && $3 == github.com" {print $4}')
274 gh_dir="${DIR_GITHUB}/${gh_username}"
275 mkdir -p "$gh_dir"
276 cd "$gh_dir" || exit 1
277 git clone "$1"
278 }
279
280 work_log_template() {
281 cat << EOF
282 $(date '+%F %A')
283 ==========
284
285 Morning report
286 --------------
287
288 ### Previous
289
290 ### Current
291
292 ### Blockers
293
294 Day's notes
295 -----------
296 EOF
297 }
298
299 work_log() {
300 mkdir -p "$DIR_WORK_LOG"
301 file_work_log_today="${DIR_WORK_LOG}/$(date +%F).md"
302 if [ ! -f "$file_work_log_today" ]
303 then
304 work_log_template > "$file_work_log_today"
305 fi
306 vim -c 'set spell' "$file_work_log_today"
307
308 }
309
310 note() {
311 mkdir -p "$DIR_NOTES"
312 vim -c 'set spell' "$DIR_NOTES/$(date +'%Y_%m_%d--%H_%M_%S%z')--$1.md"
313 }
314
315 weather() {
316 curl "http://wttr.in/$WEATHER_LOCATION"
317 }
318
319 bt_devs_paired() {
320 bluetoothctl -- paired-devices \
321 | awk '{print $2}' \
322 | xargs bluetoothctl -- info
323 }
324
325 bt_devs() {
326 bluetoothctl -- devices \
327 | awk '{print $2}' \
328 | xargs bluetoothctl -- info
329 }
330
331 run() {
332 stderr="$(mktemp)"
333 $@ 2> >(tee "$stderr")
334 code="$?"
335 urgency=''
336 case "$code" in
337 0) urgency='normal';;
338 *) urgency='critical'
339 esac
340 notify-send -u "$urgency" "Job done: $code" "$(cat $stderr)"
341 rm "$stderr"
342 }
This page took 0.129209 seconds and 5 git commands to generate.