logo

dotfiles

My dotfiles, one branch per machine, rebased on base git clone https://hacktivis.me/git/dotfiles.git

.common.sh (7802B)


  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. echo -n "${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. export PS1='$(jobs_pp)$?$(date +w%wT%T)${USER}[${host_ansi}m@$(hostname)$(get_working_directory)$(get_git_branch) '
  82. ;;
  83. *bash*)
  84. 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\] '
  85. ;;
  86. *)
  87. # Don't even try escape codes, and so no powerline-style
  88. export PS1='$? $(date +w%wT%T) $USER@${HOSTNAME:=$(hostname)} $(get_working_directory) > '
  89. ;;
  90. esac
  91. # = helpers =
  92. ssh_add_all() {
  93. # Make ssh-add call ${SSH_ASK_PASS} even outside of X11
  94. export DISPLAY="${DISPLAY:-dummy}"
  95. ssh-add $(find ~/.ssh -name 'id_*' -a \( \! -name '*.pub' \))
  96. }
  97. ssh_start_agent() {
  98. code="$(ssh-add -l; echo $?)"
  99. case "${code}" in
  100. 0) echo 'Managed to list keys, SSH agent appear to be active' ;;
  101. 1) echo 'Failed to list keys' ;;
  102. 2)
  103. rm -fr "$SSH_AUTH_SOCK"
  104. eval $(ssh-agent -a $SSH_AUTH_SOCK)
  105. echo "$SSH_AGENT_PID" > ~/.ssh/agent.pid
  106. return 0
  107. ;;
  108. *) echo 'Unknown return code: '${code} ;;
  109. esac
  110. return 1
  111. }
  112. ssh_tunnel() {
  113. local port=${1:-443}
  114. local server=${2:-hacktivis.me}
  115. local target_port=${3:-22$port}
  116. local target_host=${4:-localhost}
  117. echo "ssh -NL $target_port:$target_host:$port $server"
  118. ssh -NL $target_port:$target_host:$port $server
  119. }
  120. xdg_desktop_menu() {
  121. echo $1 $2 $3
  122. if [$1 = 'install']; then
  123. cp $2 ${XDG_DESKTOP_DIR:-~/Desktop}
  124. fi
  125. }
  126. ssh_socks() {
  127. local server=${1:-hacktivis.me}
  128. local port=${2:-22443}
  129. echo "ssh -ND $port $server"
  130. ssh -ND $port $server
  131. }
  132. pmounts() {
  133. awk 'BEGIN { print "Mountpoint Type Device Options" } { print $2 " " $3 " " $1 " " $4 | "sort"}' /proc/mounts | column -t -s\
  134. }
  135. ucd_grep() {
  136. grep "$1" /usr/share/unicode-data/UnicodeData.txt | awk -F\; '{ system("unicode -t "$1); print FS "U+"$1 FS $2 }' | column -s\; -t
  137. }
  138. ucd_chars() {
  139. unicode -n "$*" | tr 'abcdef' 'ABCDEF' | while read -r char; do ucd_code "$char"; done
  140. }
  141. ucd_code() {
  142. awk -F\; '{ if($1 == "'"${1}"'"){print "U+"$1 FS $2} }' /usr/share/unicode-data/UnicodeData.txt | grep . || echo "$1"
  143. }
  144. log_cmd() {
  145. echo "$(date -u '+%FT%TZ') START $@" >> $HOME/.log_cmd_"$1".log
  146. "$@"
  147. echo "$(date -u '+%FT%TZ') EXITED[$?] $@" >> $HOME/.log_cmd_"$1".log
  148. }
  149. # Wayland wrapper
  150. wstart() {
  151. [ -z "${XDG_RUNTIME_DIR}" ] && ( echo "$0"': Error: XDG_RUNTIME_DIR undefined, exiting…' 1>&2; false )
  152. export GDK_BACKEND=wayland
  153. export QT_QPA_PLATFORM=wayland-egl
  154. export CLUTTER_BACKEND=wayland
  155. export SDL_VIDEODRIVER=wayland
  156. export MY_LOCKER=swaylock
  157. # Default to sway when unspecified
  158. compositor=${1:-sway}
  159. name="$(sed 's/[^a-z]//g' <<<$compositor)"
  160. uptime="$(cut -d' ' -f1 /proc/uptime)"
  161. echo 'wstart_'"$uptime"'_'"$name"'.log' >&2
  162. "${@:-sway}" 2>&1 | tee "${XDG_RUNTIME_DIR}"'/wstart_'"$uptime"'_'"$name"'.log' 2>&1
  163. # Quit the login shell for security
  164. exit
  165. }
  166. mkcd() {
  167. mkdir -p "$1"
  168. cd "$1"
  169. }
  170. h() {
  171. mkcd "${HOME}/tmp/$(date +%Y-%m)"
  172. }
  173. ver_bump() {
  174. if [ $# -ne 2 ]; then
  175. echo "usage: $0 <source ebuild path> <destination ebuild path>"
  176. return
  177. fi
  178. cp -v "${1}" "${2}"
  179. git add "${2}"
  180. ekeyword ~all "${2}"
  181. ebuild "${2}" manifest
  182. git add "${2}/../Manifest"
  183. }
  184. # Unicode-aware variant inspired by https://qntm.org/files/rot13/rot13.html
  185. rot13() {
  186. uconv -f utf-8 -t utf-8 -x '::nfd;' \
  187. | tr \
  188. 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' \
  189. 'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm'
  190. }
  191. gitclone() {
  192. url="$1"
  193. dir=$(echo "$1" | sed -E -e 's;^(https?://|git:);'$HOME'/Sources/git/;' -e 's;.git$;;')
  194. test -e "$dir" || git clone "$1" "$dir"
  195. cd "$dir"
  196. }
  197. # https://team-cymru.com/community-services/ip-asn-mapping/
  198. ip2asn_stdin() {
  199. ( echo begin ; cat - ; echo end ) | nc whois.cymru.com 43 | sort -n
  200. }
  201. ip2asn_arg() {
  202. echo "$@" | nc whois.cymru.com 43
  203. }
  204. if ! command -v finger
  205. then
  206. finger() {
  207. echo "${1//@*}" | dial -e "tcp!${1//*@}!finger"
  208. }
  209. fi
  210. # startup
  211. #stty sane # I can haz urandom
  212. stty -ixon # Disable stop (^S) / start (^Q)
  213. stty -ixoff # Disable sending of start/stop characters
  214. h