Implement optional grouping by day of the week
[khome.git] / home / lib / login_functions.sh
CommitLineData
e0dbdb36
SK
1#
2
3d() {
4 local -r word=$(fzf < /usr/share/dict/words)
5 dict "$word"
6}
7
94df2def 8shell_activity_report() {
ec81fc0a
SK
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
d72bfd8f
SK
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
94df2def 21 history \
d72bfd8f
SK
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
94df2def 48 {
ec81fc0a
SK
49 # NOTE: $2 & $3 are specific to oh-my-zsh history output
50 date = $2
94df2def 51 time = $3
ec81fc0a
SK
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
d72bfd8f 56 year = d[1] + 0
ec81fc0a 57 month = d[2] + 0
d72bfd8f 58 day = d[3] + 0
ec81fc0a 59 hour = t[1] + 0
d72bfd8f
SK
60 dow = date2dow(year, month, day)
61 g = group_by == "mon" ? month : dow # dow is default
62 c = count[g, hour]++
94df2def 63 }
ec81fc0a
SK
64 if (c > max)
65 max = c
94df2def
SK
66 }
67
ec81fc0a 68 END {
d72bfd8f
SK
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
ec81fc0a
SK
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"
d72bfd8f
SK
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;
94df2def 95 for (hour=0; hour<24; hour++) {
d72bfd8f 96 c = count[gid, hour]
ec81fc0a
SK
97 printf " %2d ", hour
98 for (i = 1; i <= (c * 100) / max; i++)
94df2def
SK
99 printf "|"
100 printf "\n"
101 }
ec81fc0a
SK
102 }
103 }'
94df2def
SK
104}
105
d265cd11
SK
106top_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
c7de24d9 140# Top Disk-Using directories
58bdbfbf 141# TODO: Consider using numfmt instead of awk
c7de24d9
SK
142tdu() {
143 du "$1" \
6b543680 144 | sort -n -k 1 -r \
c7de24d9
SK
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
27456eb6
SK
157# Top Disk-Using Files
158tduf() {
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
909ece30
SK
172# Most-recently modified file system objects
173recent() {
68992a1d
SK
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.
909ece30 179 find $@ -printf '%T@ %T+ %p\0' \
68992a1d 180 | tee >(gawk -v RS='\0' 'END { printf("[INFO] Total found: %d\n", NR); }') \
909ece30
SK
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 }'
909ece30
SK
190}
191
192recent_dirs() {
193 recent "$1" -type d
194}
195
196recent_files() {
197 recent "$1" -type f
198}
199
c7de24d9
SK
200pa_def_sink() {
201 pactl info | awk '/^Default Sink:/ {print $3}'
202}
203
204void_pkgs() {
205 curl "https://xq-api.voidlinux.org/v1/query/x86_64?q=$1" | jq '.data'
206}
207
208# Colorful man
209man() {
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}
64ec9f23
SK
218
219experiment() {
9e4c43c0 220 cd "$(~/bin/experiment $@)" || exit 1
64ec9f23 221}
801dd7bd
SK
222
223hump() {
2e8cf226 224 ledit -l "$(stty size | awk '{print $2}')" ocaml $@
801dd7bd 225}
632b7c4a
SK
226
227howto() {
0136ca23 228 cat "$(find ~/Archives/Documents/HOWTOs -mindepth 1 -maxdepth 1 | sort | fzf)"
632b7c4a 229}
f4e0bb58 230
60e43329
SK
231yt() {
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
f4e0bb58
SK
248gh_fetch_repos() {
249 curl "https://api.github.com/$1/$2/repos?page=1&per_page=10000"
250}
251
252gh_clone() {
8aa18398
SK
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" \
f4e0bb58
SK
259 | jq --raw-output '.[] | select(.fork | not) | .git_url' \
260 | parallel -j 25 \
261 git clone {}
262}
263
264gh_clone_user() {
265 gh_clone 'users' "$1"
266}
267
268gh_clone_org() {
269 gh_clone 'orgs' "$1"
270}
e09a8d5a 271
610785ef
SK
272gh_clone_repo() {
273 gh_username=$(echo "$1" | awk -F / '"$1 == "https" && $3 == github.com" {print $4}')
8aa18398 274 gh_dir="${DIR_GITHUB}/${gh_username}"
610785ef
SK
275 mkdir -p "$gh_dir"
276 cd "$gh_dir" || exit 1
277 git clone "$1"
610785ef
SK
278}
279
c45bdb58
SK
280work_log_template() {
281cat << EOF
d8da04c5 282$(date '+%F %A')
c45bdb58
SK
283==========
284
285Morning report
286--------------
287
c44fbbc2 288### Previous
c45bdb58 289
c44fbbc2 290### Current
c45bdb58
SK
291
292### Blockers
293
294Day's notes
295-----------
296EOF
297}
298
299work_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
3008cd51 306 vim -c 'set spell' "$file_work_log_today"
c45bdb58
SK
307
308}
309
065977fd
SK
310note() {
311 mkdir -p "$DIR_NOTES"
3008cd51 312 vim -c 'set spell' "$DIR_NOTES/$(date +'%Y_%m_%d--%H_%M_%S%z')--$1.md"
065977fd
SK
313}
314
e09a8d5a
SK
315weather() {
316 curl "http://wttr.in/$WEATHER_LOCATION"
317}
2c0865d1
SK
318
319bt_devs_paired() {
320 bluetoothctl -- paired-devices \
321 | awk '{print $2}' \
322 | xargs bluetoothctl -- info
323}
324
325bt_devs() {
326 bluetoothctl -- devices \
327 | awk '{print $2}' \
328 | xargs bluetoothctl -- info
329}
dfbaafa4
SK
330
331run() {
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.081976 seconds and 4 git commands to generate.