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