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