logo

dotfiles

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

.common.sh (7889B)


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