Implement top_commands function
[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 top_commands() {
9 history \
10 | awk '
11 {
12 count[$4]++
13 }
14
15 END {
16 for (cmd in count)
17 print count[cmd], cmd
18 }' \
19 | sort -n -r -k 1 \
20 | head -50 \
21 | awk '
22 {
23 cmd[NR] = $2
24 c = count[NR] = $1 + 0 # + 0 to coerce number from string
25 if (c > max)
26 max = c
27 }
28
29 END {
30 for (i = 1; i <= NR; i++) {
31 c = count[i]
32 printf "%s %d ", cmd[i], c
33 scaled = (c * 100) / max
34 for (j = 1; j <= scaled; j++)
35 printf "|"
36 printf "\n"
37 }
38 }' \
39 | column -t
40 }
41
42 # Top Disk-Using directories
43 # TODO: Consider using numfmt instead of awk
44 tdu() {
45 du "$1" \
46 | sort -n -k 1 -r \
47 | head -50 \
48 | awk '
49 {
50 size = $1
51 path = $0
52 sub("^" $1 "\t+", "", path)
53 gb = size / 1024 / 1024
54 printf("%f\t%s\n", gb, path)
55 }' \
56 | cut -c 1-115
57 }
58
59 # Top Disk-Using Files
60 tduf() {
61 find "$1" -type f -printf '%s\t%p\0' \
62 | sort -z -n -k 1 -r \
63 | head -z -n 50 \
64 | gawk -v RS='\0' '
65 {
66 size = $1
67 path = $0
68 sub("^" $1 "\t+", "", path)
69 gb = size / 1024 / 1024 / 1024
70 printf("%f\t%s\n", gb, path)
71 }'
72 }
73
74 # Most-recently modified file system objects
75 recent() {
76 # NOTES:
77 # - intentionally not quoting the parameters, so that some can be ignored
78 # if not passed, rather than be passed to find as an empty string;
79 # - %T+ is a GNU extension;
80 # - gawk is able to split records on \0, while awk cannot.
81 find $@ -printf '%T@ %T+ %p\0' \
82 | tee >(gawk -v RS='\0' 'END { printf("[INFO] Total found: %d\n", NR); }') \
83 | sort -z -k 1 -n -r \
84 | head -n "$(stty size | awk 'NR == 1 {print $1 - 5}')" -z \
85 | gawk -v RS='\0' '
86 {
87 sub("^" $1 " +", "") # Remove epoch time
88 sub("+", " ") # Blank-out the default separator
89 sub("\\.[0-9]+", "") # Remove fractional seconds
90 print
91 }'
92 }
93
94 recent_dirs() {
95 recent "$1" -type d
96 }
97
98 recent_files() {
99 recent "$1" -type f
100 }
101
102 pa_def_sink() {
103 pactl info | awk '/^Default Sink:/ {print $3}'
104 }
105
106 void_pkgs() {
107 curl "https://xq-api.voidlinux.org/v1/query/x86_64?q=$1" | jq '.data'
108 }
109
110 # Colorful man
111 man() {
112 LESS_TERMCAP_md=$'\e[01;31m' \
113 LESS_TERMCAP_me=$'\e[0m' \
114 LESS_TERMCAP_se=$'\e[0m' \
115 LESS_TERMCAP_so=$'\e[01;44;33m' \
116 LESS_TERMCAP_ue=$'\e[0m' \
117 LESS_TERMCAP_us=$'\e[01;32m' \
118 command man "$@"
119 }
120
121 experiment() {
122 cd "$(~/bin/experiment $@)" || exit 1
123 }
124
125 hump() {
126 ledit -l "$(stty size | awk '{print $2}')" ocaml $@
127 }
128
129 howto() {
130 cat "$(find ~/Archives/Documents/HOWTOs -mindepth 1 -maxdepth 1 | sort | fzf)"
131 }
132
133 yt() {
134 local _yt_uri
135 local _yt_id
136 local _yt_title
137 local _yt_dir
138
139 _yt_uri="$1"
140 _yt_id=$(youtube-dl --get-id "$_yt_uri")
141 _yt_title=$(youtube-dl --get-title "$_yt_uri")
142 _yt_dir="${DIR_YOUTUBE}/individual-videos/${_yt_title}--${_yt_id}"
143
144 mkdir -p "$_yt_dir"
145 cd "$_yt_dir" || exit 1
146 echo "$_yt_uri" > 'uri'
147 youtube-dl -c --write-description --write-info-json "$_yt_uri"
148 }
149
150 gh_fetch_repos() {
151 curl "https://api.github.com/$1/$2/repos?page=1&per_page=10000"
152 }
153
154 gh_clone() {
155 gh_user_type="$1"
156 gh_user_name="$2"
157 gh_dir="${DIR_GITHUB}/${gh_user_name}"
158 mkdir -p "$gh_dir"
159 cd "$gh_dir" || exit 1
160 gh_fetch_repos "$gh_user_type" "$gh_user_name" \
161 | jq --raw-output '.[] | select(.fork | not) | .git_url' \
162 | parallel -j 25 \
163 git clone {}
164 }
165
166 gh_clone_user() {
167 gh_clone 'users' "$1"
168 }
169
170 gh_clone_org() {
171 gh_clone 'orgs' "$1"
172 }
173
174 gh_clone_repo() {
175 gh_username=$(echo "$1" | awk -F / '"$1 == "https" && $3 == github.com" {print $4}')
176 gh_dir="${DIR_GITHUB}/${gh_username}"
177 mkdir -p "$gh_dir"
178 cd "$gh_dir" || exit 1
179 git clone "$1"
180 }
181
182 work_log_template() {
183 cat << EOF
184 $(date '+%F %A')
185 ==========
186
187 Morning report
188 --------------
189
190 ### Previous
191
192 ### Current
193
194 ### Blockers
195
196 Day's notes
197 -----------
198 EOF
199 }
200
201 work_log() {
202 mkdir -p "$DIR_WORK_LOG"
203 file_work_log_today="${DIR_WORK_LOG}/$(date +%F).md"
204 if [ ! -f "$file_work_log_today" ]
205 then
206 work_log_template > "$file_work_log_today"
207 fi
208 vim -c 'set spell' "$file_work_log_today"
209
210 }
211
212 note() {
213 mkdir -p "$DIR_NOTES"
214 vim -c 'set spell' "$DIR_NOTES/$(date +'%Y_%m_%d--%H_%M_%S%z')--$1.md"
215 }
216
217 weather() {
218 curl "http://wttr.in/$WEATHER_LOCATION"
219 }
220
221 bt_devs_paired() {
222 bluetoothctl -- paired-devices \
223 | awk '{print $2}' \
224 | xargs bluetoothctl -- info
225 }
226
227 bt_devs() {
228 bluetoothctl -- devices \
229 | awk '{print $2}' \
230 | xargs bluetoothctl -- info
231 }
232
233 run() {
234 stderr="$(mktemp)"
235 $@ 2> >(tee "$stderr")
236 code="$?"
237 urgency=''
238 case "$code" in
239 0) urgency='normal';;
240 *) urgency='critical'
241 esac
242 notify-send -u "$urgency" "Job done: $code" "$(cat $stderr)"
243 rm "$stderr"
244 }
This page took 0.097133 seconds and 5 git commands to generate.