.common.sh 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #!/bin/sh
  2. # per-shell exports
  3. export GPG_TTY="$(tty)"
  4. # Aliases
  5. unalias bash; alias bash="SHELL=/bin/bash bash -l"
  6. unalias bc; alias bc="bc ~/Sources/git/hacktivis.me/git/blog/notes/bc.txt"
  7. unalias fuck; alias fuck='TF_ALIAS=fuck eval $(thefuck $(fc -ln -1))'
  8. unalias fuu; alias fuu='su -c "$(fc -ln -1)"'
  9. unalias git-st; alias git-st="git status --short --branch"
  10. #alias git-sel='git checkout $(git branch --format "%(refname:lstrip=2)" | dmenu -i -l 10)'
  11. unalias git-sel; alias git-sel='select branch in $(git branch --format "%(refname:lstrip=2)"); do git checkout $branch; break; done'
  12. unalias git-story; alias git-story="git log --graph --abbrev-commit --decorate --format=format:'%C(cyan)%h%C(reset) %C(green)(%ad)%C(reset) %s %C(red)- %an %C(reset)%C(yellow)%d%C(reset)'"
  13. unalias j; alias j='jobs -l'
  14. unalias ls; alias ls='ls -hFA'
  15. unalias mv; alias mv='mv -i'
  16. unalias pscpu; alias pscpu='ps -eo pid,user,pcpu,pmem,comm --sort -pcpu | head -20'
  17. unalias psmem; alias psmem='ps -eo pid,user,pcpu,pmem,comm --sort -pmem | head -20'
  18. unalias rm; alias rm='rm -ri'
  19. unalias sleep; alias sleep="/bin/sleep" # avoid sleep as a builtin
  20. unalias xstart; alias xstart='exec startx -- $(tty | sed "s/\/dev\/tty/vt/")'
  21. unalias .; alias .='cd .'
  22. unalias ..; alias ..='cd ..'
  23. unalias ...; alias ...='cd ../..'
  24. unalias zzz; alias zzz='locker & (sleep 1 ; memsys)'
  25. unalias ytcp; alias ytcp='yt-dlp --sub-langs all,-live_chat --embed-subs --embed-thumbnail --add-metadata --embed-chapters --compat-options filename --concurrent-fragments 10'
  26. unalias ytcp-autosubs; alias ytcp-autosubs='yt-dlp --sub-langs all,-live_chat --write-subs --write-auto-subs --embed-subs --embed-thumbnail --add-metadata --embed-chapters --compat-options filename --concurrent-fragments 10'
  27. unalias deposix; alias deposix='unset POSIXLY_CORRECT POSIX_ME_HARDER'
  28. # List all possible readings; Convert all to romaji
  29. unalias kks; alias kks='kakasi -p -Ha -Ka -Ja -Ea -ka -i utf8 -o utf8'
  30. # because the latin alphabet is a mess: https://lojban.org/publications/cll/cll_v1.1_xhtml-chapter-chunks/chapter-phonology.html#section-oddball-orthographies
  31. # Character-based: Plan9(Port) tr(1); GNU sed(1) y-command
  32. # Byte-based: POSIX, GNU, BusyBox, … tr(1); BusyBox sed(1) y-command
  33. unalias lojban_cyrillic; alias lojban_cyrillic='9 tr "abcdefgijklmnoprstuvxyzABCDEFGIJKLMNOPRSTUVXYZ" \
  34. "абшдефгижклмнопрстувхъзАБШДЕФГИЖКЛМНОПРСТУВХЪЗ"'
  35. unalias cyrillic_lojban; alias cyrillic_lojban='9 tr "абшдефгижклмнопрстувхъзАБШДЕФГИЖКЛМНОПРСТУВХЪЗ" \
  36. "abcdefgijklmnoprstuvxyzABCDEFGIJKLMNOPRSTUVXYZ"'
  37. unalias cmd; alias cmd=command
  38. unalias wol-cutievault; alias wol-cutievault="wol 74:d4:35:b0:15:3e"
  39. # = functions =
  40. # Note: POSIX only allows [0-9a-z_A-Z] in function names
  41. get_git_branch() {
  42. if branch=$(git rev-parse --abbrev-ref HEAD 2> /dev/null); then
  43. case "$branch" in
  44. "HEAD")
  45. branch='detached*'
  46. ;;
  47. "$(hostname)")
  48. branch='@.'
  49. ;;
  50. esac
  51. if git notes list HEAD >/dev/null 2>&1
  52. then
  53. notes="🖉"
  54. else
  55. notes=""
  56. fi
  57. printf -- ' %s%s' "$notes" "$branch"
  58. else
  59. echo ''
  60. fi
  61. unset branch
  62. }
  63. get_working_directory() {
  64. pwd | sed "s|^$HOME|~|"
  65. }
  66. # == prompt setup ==
  67. case $(hostname) in
  68. NightmareMoon) host_ansi="33" ;;
  69. hp2125) host_ansi="37" ;;
  70. *) host_ansi="31" ;;
  71. esac
  72. jobs_pp() {
  73. job_count="$(jobs | wc -l)"
  74. if [ "${job_count}" != "0" ]; then
  75. printf '%s' "${job_count}⏾"
  76. fi
  77. }
  78. case $SHELL in
  79. *ksh*)
  80. # hack derived from ksh88(1), works in mksh, doesn't works in bash
  81. # shellcheck disable=SC2025
  82. export PS1=' $(jobs_pp)$?$(date +w%wT%T)${USER}[${host_ansi}m@$(hostname)$(get_working_directory)$(get_git_branch) $(meep $?)'
  83. ;;
  84. *bash*)
  85. export PS1='\[\e[47;30m\]$(jobs_pp)$?\[\e[37;100m\]\[\e[100;30m\]$(date +w%wT%T)\[\e[40;90m\]\[\e[40;32m\]\u\[\e[${host_ansi}m\]@\h\]\[\e[30;46m\]$(get_working_directory)\[\e[0;36m\]\[\e[0m\] $(meep $?)'
  86. ;;
  87. *)
  88. # Don't even try escape codes, and so no powerline-style
  89. export PS1='$? $(date +w%wT%T) $USER@${HOSTNAME:=$(hostname)} $(get_working_directory) > $(meep $?)'
  90. ;;
  91. esac
  92. # = helpers =
  93. ssh_add_all() {
  94. # Make ssh-add call ${SSH_ASK_PASS} even outside of X11
  95. export DISPLAY="${DISPLAY:-dummy}"
  96. ssh-add $(find ~/.ssh -name 'id_*' -a \( \! -name '*.pub' \))
  97. }
  98. ssh_start_agent() {
  99. code="$(ssh-add -l; echo $?)"
  100. case "${code}" in
  101. 0) echo 'Managed to list keys, SSH agent appear to be active' ;;
  102. 1) echo 'Failed to list keys' ;;
  103. 2)
  104. rm -fr "$SSH_AUTH_SOCK"
  105. eval $(ssh-agent -a "$SSH_AUTH_SOCK")
  106. echo "$SSH_AGENT_PID" > ~/.ssh/agent.pid
  107. return 0
  108. ;;
  109. *) echo "Unknown return code: ${code}" ;;
  110. esac
  111. return 1
  112. }
  113. ssh_tunnel() {
  114. port=${1:-443}
  115. server=${2:-hacktivis.me}
  116. target_port=${3:-22$port}
  117. target_host=${4:-localhost}
  118. echo "ssh -NL ${target_port}:${target_host}:${port} $server"
  119. ssh -NL "${target_port}:${target_host}:${port}" "$server"
  120. }
  121. xdg_desktop_menu() {
  122. echo "$1 $2 $3"
  123. if [ "$1" = 'install' ]; then
  124. cp "$2" "${XDG_DESKTOP_DIR:-~/Desktop}"
  125. fi
  126. }
  127. ssh_socks() {
  128. server=${1:-hacktivis.me}
  129. port=${2:-22443}
  130. echo "ssh -ND $port $server"
  131. ssh -ND "$port" "$server"
  132. }
  133. pmounts() {
  134. awk 'BEGIN { print "Mountpoint Type Device Options" } { print $2 " " $3 " " $1 " " $4 | "sort"}' /proc/mounts | column -t -s' '
  135. }
  136. ucd_grep() {
  137. grep "$1" /usr/share/unicode-data/UnicodeData.txt | awk -F\; '{ system("unicode -t "$1); print FS "U+"$1 FS $2 }' | column -s\; -t
  138. }
  139. ucd_chars() {
  140. unicode -n "$*" | tr 'abcdef' 'ABCDEF' | while read -r char; do ucd_code "$char"; done
  141. }
  142. ucd_code() {
  143. awk -F\; '{ if($1 == "'"${1}"'"){print "U+"$1 FS $2} }' /usr/share/unicode-data/UnicodeData.txt | grep . || echo "$1"
  144. }
  145. log_cmd() {
  146. echo "$(date -u '+%FT%TZ') START $*" >> "${HOME}/.log_cmd_${1}.log"
  147. "$@"
  148. echo "$(date -u '+%FT%TZ') EXITED[$?] $*" >> "${HOME}/.log_cmd_${1}.log"
  149. }
  150. # Wayland wrapper
  151. wstart() {
  152. [ -z "${XDG_RUNTIME_DIR}" ] && ( echo "$0"': Error: XDG_RUNTIME_DIR undefined, exiting…' 1>&2; false )
  153. export GDK_BACKEND=wayland
  154. export QT_QPA_PLATFORM=wayland-egl
  155. export CLUTTER_BACKEND=wayland
  156. export SDL_VIDEODRIVER=wayland
  157. export MY_LOCKER=swaylock
  158. export TERMINAL=foot
  159. # Default to sway when unspecified
  160. compositor=${1:-sway}
  161. name="$(echo "$compositor" | sed 's/[^a-z]//g')"
  162. uptime="$(cut -d' ' -f1 /proc/uptime)"
  163. echo 'wstart_'"$uptime"'_'"$name"'.log' >&2
  164. "${@:-sway}" 2>&1 | tee "${XDG_RUNTIME_DIR}"'/wstart_'"$uptime"'_'"$name"'.log' 2>&1
  165. # Quit the login shell for security
  166. exit
  167. }
  168. meep() {
  169. if [ "$1" = "0" ]
  170. then
  171. beep -l 50 -f 250
  172. else
  173. beep -l 50 -f 150
  174. fi
  175. }
  176. mkcd() {
  177. mkdir -p "$1"
  178. cd "$1" || return
  179. }
  180. h() {
  181. mkcd "${HOME}/tmp/$(date +%Y-%m)"
  182. }
  183. ver_bump() {
  184. if [ $# -ne 2 ]; then
  185. echo "usage: $0 <source ebuild path> <destination ebuild path>"
  186. return
  187. fi
  188. cp -v "${1}" "${2}"
  189. git add "${2}"
  190. ekeyword ~all "${2}"
  191. ebuild "${2}" manifest
  192. git add "${2}/../Manifest"
  193. }
  194. # Unicode-aware variant inspired by https://qntm.org/files/rot13/rot13.html
  195. rot13() {
  196. uconv -f utf-8 -t utf-8 -x '::nfd;' \
  197. | tr \
  198. 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' \
  199. 'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm'
  200. }
  201. gitclone() {
  202. url="$1"
  203. dir=$(echo "$1" | sed -E -e 's;^(https?://|git:);'$HOME'/Sources/git/;' -e 's;.git$;;')
  204. test -e "$dir" || git clone "$1" "$dir"
  205. cd "$dir"
  206. }
  207. # https://team-cymru.com/community-services/ip-asn-mapping/
  208. ip2asn_stdin() {
  209. ( echo begin ; cat - ; echo end ) | nc whois.cymru.com 43 | sort -n
  210. }
  211. ip2asn_arg() {
  212. echo "$@" | nc whois.cymru.com 43
  213. }
  214. if ! command -v finger
  215. then
  216. finger() {
  217. echo "${1//@*}" | dial -e "tcp!${1//*@}!finger"
  218. }
  219. fi
  220. # startup
  221. #stty sane # I can haz urandom
  222. stty -ixon # Disable stop (^S) / start (^Q)
  223. stty -ixoff # Disable sending of start/stop characters
  224. h