functions (5084B)
1 #!/bin/sh 2 3 nixpkgs_cache_index_update() { 4 filename="index-$(uname -m | sed 's/^arm64$/aarch64/')-$(uname | tr "[:upper:]" "[:lower:]")" 5 mkdir -p ~/.cache/nix-index && cd ~/.cache/nix-index || exit 6 wget -q -N https://github.com/nix-community/nix-index-database/releases/latest/download/"$filename" 7 ln -f "$filename" files 8 } 9 10 nvimrecomp() { 11 nix_fennel_path="$HOME/src/sr.ht/nix/modules/mixins/dotfiles/config/nvim/fnl" 12 config_path="$HOME/.config/nvim" 13 14 mkdir -p "$config_path"/lua/config 15 16 find "$nix_fennel_path" -type f -name "*.fnl" | while read -r fnl_file; do 17 rel_path="${fnl_file#"$nix_fennel_path"/}" 18 19 if [ "$rel_path" = "init.fnl" ]; then 20 lua_file="$config_path/init.lua" 21 else 22 lua_file="$config_path/lua/${rel_path%.fnl}.lua" 23 fi 24 25 fnlfmt --fix "$fnl_file" 26 fennel --compile "$fnl_file" | tee "$lua_file" >/dev/null 27 echo "$lua_file" 28 done 29 } 30 31 bandsnatch() { 32 command bandsnatch run \ 33 -c "$HOME/misc/music/random/bandcamp-cookies.json" \ 34 -f flac \ 35 -o "$HOME/misc/music/backlog/_todo" \ 36 mtmnn && backup git:bandcamp 37 } 38 39 gotosleep() { 40 date 41 sleep "${1?}m" && sudo zzz 42 } 43 44 tracklist() { 45 total=0 46 for ext in mp3 flac aif aiff; do 47 for f in *."$ext"; do 48 [ -f "$f" ] || continue 49 echo "$f" 50 done 51 done | sort -V | while read -r f; do 52 filename="${f%.*}" 53 printf "%s %d:%02d\n" "$filename" $((total / 60)) $((total % 60)) 54 duration=$(ffprobe -v quiet -show_entries format=duration -of csv=p=0 "$f" 2>/dev/null | cut -d. -f1) 55 total=$((total + duration)) 56 done 57 } 58 59 tarstamp() { 60 : "${1?name required}" "${2?path required}" 61 tarsnap -c -f "$1-$(date +%Y-%m-%d_%H:%M:%S)" "$2" 62 } 63 64 targarba() { 65 : "${1?name required}" 66 tarsnap --list-archives | grep "^$1-" | prunef --keep-monthly 6 "$1-%Y-%m-%d_%H:%M:%S" | xargs -I{} tarsnap -d -f {} 67 } 68 69 tempfox() { 70 profile_dir=$(mktemp -d) 71 trap 'rm -rf $profile_dir' EXIT 72 cp ~/src/sr.ht/tools/tempfox/user.js "$profile_dir"/user.js 73 firefox -no-remote -new-instance -profile "$profile_dir" 74 } 75 76 labels() { 77 inputs="${*:-all.txt}" 78 mkdir -p ./labels 79 for input in $inputs; do 80 base=$(basename "$input" .txt) 81 cut -d'/' -f1 "$input" | sort | uniq | tee labels/"${base}_labels.txt" 82 done 83 } 84 85 ytrss() { 86 CHANNEL_URL="$1" 87 if [ -n "$CHANNEL_URL" ]; then 88 page="$(curl -s "$CHANNEL_URL")" 89 channel_id="$(echo "$page" | 90 grep -Po '<link rel="canonical" href="https://www.youtube.com/channel/\K.*?(?=">)')" 91 channel_name="$(echo "$page" | 92 grep -Po '<meta property="og:title" content="\K.*?(?=">)')" 93 echo "https://www.youtube.com/feeds/videos.xml?channel_id=$channel_id $channel_name" | wl-copy 94 fi 95 } 96 97 ferss() { 98 url="${1?}" 99 curl -s "$url" | xmllint --html --xpath "//link[@type='application/rss+xml' or @type='application/atom+xml']/@href" - 2>/dev/null 100 } 101 102 reval() { 103 eval "$(rbenv init - --no-rehash shell)" 104 ruby --version 105 } 106 107 mirror() { 108 wget --mirror --page-requisites --no-parent --convert-links -P ./ "$@" 109 } 110 111 cheat() { 112 curl cht.sh/"${1?}" 113 } 114 115 make() { 116 if just --summary >/dev/null 2>&1; then 117 just "$@" 118 else 119 command make "$@" 120 fi 121 } 122 123 kana() { 124 khal new --alarms "$@" 125 pimsync sync 126 } 127 128 mpas() { 129 mpc add "$(yt-dlp -f bestaudio -g "${1?}")" 130 } 131 132 shi() { 133 xh -b GET "$(pass show shirts/history)" \ 134 "Authorization:Bearer $(pass show shirts)" | 135 jaq -r '.shirts[] | "\u001b[36m\(.code)\u001b[0m https://shirts.mtmn.name/s/\(.code) \u001b[33m\(.url)\u001b[0m \u001b[32m\(.created_at)\u001b[0m"' 136 } 137 138 mpa() { 139 mpc add "${1?}" 140 } 141 142 kan() { 143 khal new "$@" 144 pimsync sync 145 } 146 147 ae() { 148 if tmux has-session -t aerc 2>/dev/null; then 149 if [ -n "$TMUX" ]; then 150 tmux switch-client -t aerc 151 else 152 tmux attach -t aerc 153 fi 154 else 155 tmux new -s aerc aerc 156 fi 157 } 158 159 cd() { 160 builtin cd "$@" && magdalena log-dir "$PWD" 161 } 162 163 t() { 164 window_id="" 165 if [ $# -eq 0 ]; then 166 tab=$(printf '\t') 167 result=$(tmux list-windows -a -F "#{session_name}/#{window_name}${tab}#{window_id}" 2>/dev/null | fzf --print-query \ 168 --delimiter '\t' --with-nth 1 \ 169 --preview 'tmux capture-pane -ep -t {2}' \ 170 --preview-window 'right:60%') 171 fzf_exit=$? 172 [ $fzf_exit -eq 130 ] && return 173 selection=$(printf '%s' "$result" | tail -1) 174 [ -z "$selection" ] && return 175 case "$selection" in 176 *"$tab"*) 177 session=${selection%%/*} 178 window_id=${selection##*"$tab"} 179 ;; 180 *) 181 session=$selection 182 ;; 183 esac 184 else 185 session="$1" 186 fi 187 188 if tmux has-session -t "${session}" 2>/dev/null; then 189 [ -n "$window_id" ] && tmux select-window -t "$window_id" 2>/dev/null 190 if [ -n "$TMUX" ]; then 191 tmux switch-client -t "${session}" 192 else 193 tmux attach -t "${session}" 194 fi 195 else 196 if [ -n "$TMUX" ]; then 197 tmux new-session -d -c "$HOME" -s "${session}" && tmux switch-client -t "${session}" 198 else 199 tmux new -c "$HOME" -s "${session}" 200 fi 201 fi 202 } 203 204 u() { 205 if [ -n "$1" ]; then 206 unzip "$1" -d "${1%.zip}" 207 else 208 for a in *.zip; do 209 unzip -n "$a" -d "${a%.zip}" 210 done 211 fi 212 } 213 214 o() { 215 if [ $# -eq 0 ]; then 216 nvim -c ":Oil $(pwd)" 217 else 218 nvim -c ":Oil $1" 219 fi 220 } 221 222 d() { 223 if [ -n "$1" ]; then 224 cd "$1" || return 225 return 226 fi 227 228 target_dir=$(magdalena look-dir) 229 if [ -n "$target_dir" ]; then 230 cd "$target_dir" && magdalena log-dir "$PWD" 231 fi 232 }