logo

dotfiles

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

.common.sh (6995B)


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