.zshrc.local (12305B)
1## completion 2source ~/.local/share/zsh/site-functions/_trt && compdef _trt trt 3 4## alias 5alias gpg="/usr/bin/gpg2" 6alias ssh="TERM=xterm-256color ssh" 7alias pc-ssh="TERM=xterm-256color proxychains ssh" 8alias bap="bat --style=plain --paging=always" 9alias neofetch="USER=user HOSTNAME=hostname /usr/bin/neofetch" 10alias screen="TERM=screen screen" 11alias diff="diff --color=always" 12alias weather="curl --silent 'https://wttr.in/Amsterdam' | tail -n+2 | head -n-1" 13# Language 14alias speak="/usr/bin/trans --brief --theme=random --speak --no-translate --no-init" 15alias zh-TW="/usr/bin/trans --interactive --brief --theme=random :zh-TW --no-init" 16alias wn="sdict wn" 17# Xorg 18alias xcopy="xclip -selection clipboard" 19# Void Linux 20alias sv="doas /usr/bin/sv" 21alias vsv="doas /usr/bin/vsv" 22alias usv="SVDIR=~/.local/service /usr/bin/sv" 23alias uvsv="SVDIR=~/.local/service /usr/bin/vsv" 24 25## plugins 26#. /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh 27. ~/.local/src/github/fast-syntax-highlighting/fast-syntax-highlighting.plugin.zsh 28. /usr/share/zsh/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh 29. /usr/share/zsh/site-functions/skim-key-bindings.zsh 30#. $HOME/.local/share/grc/grc.zsh 31#. /usr/share/skim/key-bindings.zsh 32. ~/.local/src/github/zsh-z/zsh-z.plugin.zsh 33 34if [ ! -z ${DISPLAY+x} ] ; then 35 . /usr/share/zsh-theme-powerlevel10k/powerlevel10k.zsh-theme 36 . $HOME/.p10k.zsh 37 POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir rbenv vcs) 38 #ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=240' 39 ##ZSH_HIGHLIGHT_STYLES[suffix-alias]='fg=blue,underline' 40 ##ZSH_HIGHLIGHT_STYLES[precommand]='fg=blue,underline' 41 ##ZSH_HIGHLIGHT_STYLES[arg0]='fg=blue' 42 ##ZSH_HIGHLIGHT_STYLES[comment]='fg=239' 43 #ZSH_HIGHLIGHT_STYLES[suffix-alias]=fg=32,underline 44 #ZSH_HIGHLIGHT_STYLES[precommand]=fg=32,underline 45 #ZSH_HIGHLIGHT_STYLES[arg0]=fg=32 # 26 46 #ZSH_HIGHLIGHT_STYLES[single-hyphen-option]='fg=38' 47 #ZSH_HIGHLIGHT_STYLES[double-hyphen-option]='fg=38' 48 #ZSH_HIGHLIGHT_STYLES[default]='fg=38' 49 #ZSH_HIGHLIGHT_STYLES[single-quoted-argument-unclosed]='fg=red' 50 #ZSH_HIGHLIGHT_STYLES[double-quoted-argument-unclosed]='fg=red' 51fi 52 53# SSH Agent 54if ! pgrep -u "$USER" ssh-agent > /dev/null; then 55 ssh-agent > "$HOME/.ssh/ssh-agent.env" # or "$XDG_RUNTIME_DIR/ssh-agent.env" 56fi 57if [[ ! "$SSH_AUTH_SOCK" ]]; then 58 eval "$(<"$HOME/.ssh/ssh-agent.env")" >/dev/null 59fi 60 61## functions 62# speedtest 63speedtest-socks-connection() { 64 local URL="https://www.google.com/generate_204" 65 echo 'http_code\ttime_appconnect\ttime_connect\ttime_namelookup\ttime_total' 66 repeat 12 curl \ 67 -x socks5h://127.0.0.1:1080 --output /dev/null --silent \ 68 --write-out '%{http_code}\t\t%{time_appconnect}\t%{time_connect}\t%{time_namelookup}\t%{time_total}\n' \ 69 $URL 70} 71speedtest-socks-speed() { 72 curl \ 73 -x socks5h://127.0.0.1:1080 --silent \ 74 -o/dev/null --write-out '%{speed_download}' \ 75 http://speedtest-sgp1.digitalocean.com/10mb.test \ 76 | numfmt --to=iec-i --suffix=B/s 77} 78speedtest-connection() { 79 #echo 'http_code\ttime_appconnect\ttime_connect\ttime_namelookup\ttime_total' 80 #repeat 7 curl --output /dev/null --silent --write-out '%{http_code}\t\t%{time_appconnect}\t%{time_connect}\t%{time_namelookup}\t%{time_pretransfer}\t\t%{time_redirect}\t%{time_starttransfer}\t\t%{time_total}\n' https://connectivitycheck.gstatic.com/generate_204 81 local time_seconds=$(curl -o/dev/null -sw '%{time_appconnect}' 'https://connectivitycheck.gstatic.com/generate_204') 82 printf "%.0fms\n" $((time_seconds * 1000)) 83} 84 85cdgit() { 86 REPO_PATH=$(git rev-parse --show-toplevel 2>/dev/null) 87 if [[ "$1" == / ]]; then 88 cd "${REPO_PATH}" 89 return 90 fi 91 if [ ! -z $REPO_PATH ]; then 92 NEW_PATH=$(git ls-tree -d -r --name-only HEAD | fzf --layout=reverse --inline-info --prompt='cd ' --height=20) 93 test -z $NEW_PATH || cd "$NEW_PATH" 94 else 95 echo "not a git repo" 96 return 1 97 fi 98} 99 100dict() { 101 /usr/bin/dict $@ \ 102 | GREP_COLORS='ms=00;30;44' grep --color=always -P '(?<=^From\ ).*(?=\ \[)|$' \ 103 | GREP_COLORS='ms=01;37' grep --color=always -P '(?<= )[0-9]\.|$' 104} 105 106# https://dict.revised.moe.edu.tw/ 107dict_revised() { 108 dict -d dict_revised_2015 $(read -e) \ 109 | GREP_COLORS='ms=00;32' grep --color=always "\[形\]\|\[動\]\|\[名\]\|\[助\]\|\[副\]\|$" 110} 111 112sdict() { 113 local ALL_DICT=$(dict -D | tail -n+2 | cut -d' ' -f2-) 114 local COUNT_DICT=$(echo "$ALL_DICT" | wc -l) 115 if [ -z "$1" ]; then 116 echo "$ALL_DICT" \ 117 | fzf --layout=reverse --info=inline --height="$COUNT_DICT" --prompt='dict > ' \ 118 | read DB DICT || return 0 119 else 120 echo "$ALL_DICT" | while read database dictionary; do 121 if [ "$1" = "$database" ]; then 122 DB="$database" 123 DICT="$dictionary" 124 break 125 fi 126 done 127 [ -z "$DB" ] && { echo "Invalid dict db: $1"; return 1; } 128 fi 129 cut -f1 "/usr/share/dict/${DB}.index" | fzf \ 130 --layout=reverse \ 131 --info=inline \ 132 --prompt="${DICT} > " \ 133 --preview="dict --database=${DB} {}" \ 134 --header="[Enter] View | [CTRL+D] Definition | [CTRL+T] Translation | [CTRL+S] Listen" \ 135 --bind="enter:execute:dict --database=${DB} {} | less -c" \ 136 --bind="ctrl-d:execute:trans --dictionary --theme=random {} | less -c" \ 137 --bind="ctrl-t:execute:trans --theme=random :zh-TW {} | less -c" \ 138 --bind="ctrl-s:execute-silent:trans --speak {}" \ 139 --preview-window=up,border-bottom 140} 141 142# zsh-insulter 143if [ -f /etc/zsh.command-not-found ]; then 144 . /etc/zsh.command-not-found 145fi 146 147# Xorg 148x() { echo "which session?"; read && sx $HOME/.config/sx/sxrc $REPLY; } 149 150# memory usage 151memu() { 152 local CHECK=" $@" 153 [ -z $1 ] || [ "${CHECK#* -}" != "$CHECK" ] && { echo "Usage: memu program [program...]"; return 1; } 154 local PIDS=$(pidof "$@") 155 test -z "$PIDS" && return 0 156 echo "$PIDS" \ 157 | xargs -I{} ps -p "{}" -o size,vsize,rss,cmd \ 158 | awk 'NR==1; NR>1 {print $1"K", $2"Ki", $3"K", $4}' \ 159 | numfmt --header --field 1,3 --from=si --to=si --suffix=B --format %.1f \ 160 | numfmt --header --field 2 --from=iec-i --to=iec-i --suffix=B --format %.1f \ 161 | sort -hk3,3 \ 162 | column -t -R1,2,3 \ 163 | GREP_COLORS='mt=1;94' egrep --color=always '.*SIZE.*VSZ.*RSS.*CMD.*|$' \ 164 | GREP_COLORS='mt=1;32' egrep --color=always "KB|KiB|$" \ 165 | GREP_COLORS='mt=0;33' egrep --color=always 'MB|MiB|$' \ 166 | GREP_COLORS='mt=1;31' egrep --color=always "GB|GiB|$" 167} 168ps_mem() { 169 doas /usr/bin/ps_mem "$@" \ 170 | GREP_COLORS='mt=1;32' egrep --color=auto --color=always "KB|KiB|$" \ 171 | GREP_COLORS='mt=0;33' egrep --color=auto --color=always 'MB|MiB|$' \ 172 | GREP_COLORS='mt=1;31' egrep --color=auto --color=always "GB|GiB|$" 173} 174smem() { 175 doas /usr/bin/smem -k "$@" \ 176 | GREP_COLORS='mt=1;32' egrep --color=auto --color=always "K |$" \ 177 | GREP_COLORS='mt=0;33' egrep --color=auto --color=always 'M |$' \ 178 | GREP_COLORS='mt=1;31' egrep --color=auto --color=always "G |$" 179} 180 181# morse code, base64 182mbt() { 183 echo "morse code base64 enc/dec" 184 local NUM_OUTPUT_COLOR=$(shuf -n1 <(echo 3; seq 5 6)) 185 while true; do 186 read input"?$(tput bold)$(tput setf 1)> $(tput sgr0)" 187 if [ -z $input ]; then 188 local OUTPUT=$(xclip -selection clipboard -o | base64 -d | morse -d) 189 echo "$(tput setf $NUM_OUTPUT_COLOR)${OUTPUT}$(tput sgr0)" 190 else 191 local OUTPUT=$(echo "$input" | morse -e | base64 -w0) 192 echo "$OUTPUT" | xclip -selection clipboard 193 echo "$(tput setf 2)${OUTPUT}$(tput sgr0)" 194 fi 195 done 196} 197mat() { 198 if [ -z "$1" ]; then 199 echo "convert text into audio file with morse code" 200 read input"?> " 201 [ -z "$input" ] \ 202 && { echo "NO INPUT"; return 0; } \ 203 || echo $input \ 204 | cwwav -o/tmp/cw.wav -f650 -w20 2>/dev/null 205 opusenc --quiet /tmp/cw.wav cw_$(date -u +%FT%TZ).opus \ 206 && echo $_ 207 else 208 [ ! -f "$1" ] \ 209 && { echo "\"$1\" not found"; exit 0; } \ 210 || sox $1 -esigned-integer -b16 -r 22050 -t raw - 2>/dev/null \ 211 | multimon-ng -q -t raw -a MORSE_CW - 212 fi 213 rm -f /tmp/cw.wav 214} 215 216# Void Linux, xbps 217#git -C ~/.cache/xlocate.git ls-tree --full-tree -r --name-only HEAD | rev | sed 's/_/ /; s/-/ /' | rev | column -t -R3 | sk --inline-info --layout=reverse-list --preview="git -C \"$HOME/.cache/xlocate.git\" show master:$(echo {1}-{2}_{3})" --preview-window=up:50% -m 218fxls() { 219 local HEADER="[Enter] Print selected [TAB] Toggle selection [ALT-F] View file [ALT-I] Info [ALT+H] Homepage" 220 local REPO="$HOME/.cache/xlocate.git" 221 git -C "$REPO" ls-tree --full-tree -r --name-only HEAD | rev | sed 's/_/ /; s/-/ /' | rev | column -t -R3 \ 222 | sk --inline-info \ 223 --prompt "pkg > " \ 224 --layout=reverse-list \ 225 --preview="git -C \"$REPO\" show master:$(echo {1}-{2}_{3})" \ 226 --preview-window=up:50% \ 227 -m \ 228 --bind "alt-f:execute[ git -C \"$REPO\" show master:$(echo {1}-{2}_{3}) | sk --layout=reverse-list --prompt '{1} > ' ]" \ 229 --bind "alt-i:execute[ xbps-query -v -R {1} | sk --layout=reverse-list --prompt '{1} > ' ]" \ 230 --bind 'alt-h:execute-silent[ xdg-open "$(xbps-query -p homepage -R {1})" & ]' \ 231 ${1:+--query "$@"} 232} 233 234# stagit viewer 235sgview() { 236 [ -z "$1" ] \ 237 && local URL=$(read -e "?URL: ") \ 238 || local URL=$1 239 curl --progress-bar "$URL" \ 240 | sed -n 's|^<a href="#.*" class="line" id=".*">.*</a>||p' \ 241 | recode html..ascii \ 242 | bat -n 243} 244 245# termbin, fiche 246tb() { 247 [ "$1" = "-i" ] && { local DATA=$(bash -i |& tee /dev/tty); echo "$DATA" | ansifilter | tb; return 0; } 248 [ -z "$1" ] || { cat "$1" | tb; return 0; } 249 zmodload zsh/net/tcp; ztcp ezup.dev 9999; cat >&$REPLY; cat <&$REPLY | tee >(xclip -selection clipboard); ztcp -c $REPLY 250} 251 252 253np() { 254 # https://unix.stackexchange.com/a/524139 255 local url="https://paste.c-net.org/" 256 [ -s "$1" ] && { curl --silent --data-binary @"$1" --header "x-random;" --header "X-FileName: ${1##*/}" "$url"; return 0; } 257 curl --silent --header "x-random;" --data-binary @- "$url" 258} 259 260# logpaste.com 261logpaste() { 262 [ -s "$1" ] && { curl -F "_=@$@" http://logpaste.com; return 0; } 263 curl -F '_=<-' http://logpaste.com 264} 265 266# gnuplot for lists of date in format of $(date +%F) 267# sed -n 's/^: //p' ~/.zsh_history | cut -d: -f1 | xargs -I{} date +%F -d @{} | showplot 268# xbps-query -m | xargs -L1 xbps-query -p install-date | awk '{print $1}' | showplot 269# find ~/Downloads -type f 2>/dev/null | xargs -I{} stat -c %y '{}' 2>/dev/null | showplot 270# tail -n+2 dump.csv | awk -F';' '{print $NF}' | showplot -c 271# git log --oneline --pretty='%cs' | showplot 272showplot() { 273 zmodload zsh/datetime 274 local DATA=("${(@f)$(sort /dev/stdin)}") 275 local PFDAY=$(date -u -d "${DATA[1]} -1 day" +%F) # previous day of the first day 276 local NLDAY=$(date -u -d "${DATA[-1]} +1 day" +%F) # next day of the last day 277 local FDATE="${FDATE:-$PFDAY}" 278 local LDATE="${LDATE:-$NLDAY}" 279 local DURATION=$(( ( $(strftime -r %Y-%m-%d $LDATE) - $(strftime -r %Y-%m-%d $FDATE) ) / 86400 )) 280 case "$1" in 281 "-x") local TYPE="set terminal x11" ;; 282 "-p") local TYPE="set terminal png size 960,720" ;; 283 "-d") local TYPE="set terminal png size 960,720; set output '| feh -'" ;; 284 "-c") local TYPE="set terminal png size 960,720; set output '| xclip -selection clipboard -t image/png'" ;; 285 *) local TYPE="set terminal dumb size 150,45; set autoscale" ;; 286 esac 287 printf '%s\n' "${DATA[@]}" | uniq -c | awk '{print $2,$1}' | gnuplot -background white -p -e " 288 $TYPE; 289 set xdata time; 290 set timefmt '%Y-%m-%d'; 291 set xrange ['$FDATE':'$LDATE']; 292 plot '-' using 1:2 with lines title '${#DATA[@]} items | $FDATE + ${DURATION}d = $LDATE' 293 " 294} 295 296# https://unix.stackexchange.com/a/49066 297zlibd() (printf "\x1f\x8b\x08\x00\x00\x00\x00\x00" | cat - "$@" | gzip -dc) 298 299# Gentoo emerge history 300mergelop() { qlop -c -M | sort -hrk2 | sed -r 's/:| average| for| merges| merge//g' | awk '$2 < 86400 { cmd = "date -u +%T -d@" $2; print $1, ((cmd | getline res)>0)? res : $2, $3; close(cmd) }' | column -t | sk --no-sort; } 301 302ocr() { 303 grim -g "$(slurp)" - | tesseract - - 2>/dev/null 304} 305 306day_of_week() { 307 # https://stackoverflow.com/a/36389289/15763223 308 rtc_timestamp=$(</sys/class/rtc/rtc0/since_epoch) 309 rtc_date=$(</sys/class/rtc/rtc0/date) 310 dow=$(((rtc_timestamp/86400+4)%7)) 311 week=(Sunday Monday Tuesday Wednesday Thursday Friday Saturday) 312 echo "${rtc_date} ${week[@]:${dow}:1}" 313} 314 315calculator() { while true; do bc <<<"scale=3; $(read -e)"; done } 316 317slugify() { 318 # https://github.com/Mayeu/slugify/blob/master/slugify#L24 319 if [ "$1" = "-h" ]; then 320 echo "Usage: $0 [-i]" 321 elif test -t 0; then 322 read -e | $0 $@ 323 else 324 sed -e 's/[^[:alnum:]]/-/g' \ 325 | tr -s '-' \ 326 | sed -e 's/-$//' \ 327 | ([ "$1" = "-i" ] && tr A-Z a-z || cat) 328 fi 329}