The very personal dotfiles of Levi Olson.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
4.0 KiB

6 years ago
  1. #!/bin/bash
  2. ##################################################
  3. # .functions
  4. # ------------
  5. # Based on the amazing work of Jess Frazelle
  6. #
  7. # :author: Levi Olson
  8. # :date: 1 Feb 2018
  9. # :version: 0.0.1
  10. ##################################################
  11. # Simple calculator
  12. calc() {
  13. local result=""
  14. result="$(printf "scale=10;%s\\n" "$*" | bc --mathlib | tr -d '\\\n')"
  15. # └─ default (when `--mathlib` is used) is 20
  16. if [[ "$result" == *.* ]]; then
  17. # improve the output for decimal numbers
  18. # add "0" for cases like ".5"
  19. # add "0" for cases like "-.5"
  20. # remove trailing zeros
  21. printf "%s" "$result" |
  22. sed -e 's/^\./0./' \
  23. -e 's/^-\./-0./' \
  24. -e 's/0*$//;s/\.$//'
  25. else
  26. printf "%s" "$result"
  27. fi
  28. printf "\\n"
  29. }
  30. # mkdir, cd into it
  31. mkcd() {
  32. mkdir -p "$@"
  33. cd "$1"
  34. }
  35. # save path on cd
  36. cd() {
  37. builtin cd $@
  38. pwd > ~/.last_dir
  39. }
  40. # Syntax-highlight JSON strings or files
  41. # Usage: `json '{"foo":42}'` or `echo '{"foo":42}' | json`
  42. json() {
  43. if [ -t 0 ]; then # argument
  44. python -mjson.tool <<< "$*" | pygmentize -l javascript
  45. else # pipe
  46. python -mjson.tool | pygmentize -l javascript
  47. fi
  48. }
  49. # Call from a local repo to open the repository on github/bitbucket in browser
  50. # Modified version of https://github.com/zeke/ghwd
  51. repo() {
  52. # Figure out github repo base URL
  53. local base_url
  54. base_url=$(git config --get remote.origin.url)
  55. base_url=${base_url%\.git} # remove .git from end of string
  56. # Fix git@github.com: URLs
  57. base_url=${base_url//git@github\.com:/https:\/\/github\.com\/}
  58. # Fix git://github.com URLS
  59. base_url=${base_url//git:\/\/github\.com/https:\/\/github\.com\/}
  60. # Fix git@bitbucket.org: URLs
  61. base_url=${base_url//git@bitbucket.org:/https:\/\/bitbucket\.org\/}
  62. # Fix git@gitlab.com: URLs
  63. base_url=${base_url//git@gitlab\.com:/https:\/\/gitlab\.com\/}
  64. # Validate that this folder is a git folder
  65. if ! git branch 2>/dev/null 1>&2 ; then
  66. echo "Not a git repo!"
  67. exit $?
  68. fi
  69. # Find current directory relative to .git parent
  70. full_path=$(pwd)
  71. git_base_path=$(cd "./$(git rev-parse --show-cdup)" || exit 1; pwd)
  72. relative_path=${full_path#$git_base_path} # remove leading git_base_path from working directory
  73. # If filename argument is present, append it
  74. if [ "$1" ]; then
  75. relative_path="$relative_path/$1"
  76. fi
  77. # Figure out current git branch
  78. # git_where=$(command git symbolic-ref -q HEAD || command git name-rev --name-only --no-undefined --always HEAD) 2>/dev/null
  79. git_where=$(command git name-rev --name-only --no-undefined --always HEAD) 2>/dev/null
  80. # Remove cruft from branchname
  81. branch=${git_where#refs\/heads\/}
  82. [[ $base_url == *bitbucket* ]] && tree="src" || tree="tree"
  83. url="$base_url/$tree/$branch$relative_path"
  84. echo "Calling $(type open) for $url"
  85. open "$url" &> /dev/null || (echo "Using $(type open) to open URL failed." && exit 1);
  86. }
  87. # Get colors in manual pages
  88. man() {
  89. env \
  90. LESS_TERMCAP_mb="$(printf '\e[1;31m')" \
  91. LESS_TERMCAP_md="$(printf '\e[1;31m')" \
  92. LESS_TERMCAP_me="$(printf '\e[0m')" \
  93. LESS_TERMCAP_se="$(printf '\e[0m')" \
  94. LESS_TERMCAP_so="$(printf '\e[1;44;33m')" \
  95. LESS_TERMCAP_ue="$(printf '\e[0m')" \
  96. LESS_TERMCAP_us="$(printf '\e[1;32m')" \
  97. man "$@"
  98. }
  99. # get dbus session
  100. dbs() {
  101. local t=$1
  102. if [[ -z "$t" ]]; then
  103. local t="session"
  104. fi
  105. dbus-send --$t --dest=org.freedesktop.DBus \
  106. --type=method_call --print-reply \
  107. /org/freedesktop/DBus org.freedesktop.DBus.ListNames
  108. }
  109. # get the name of a x window
  110. xname(){
  111. local window_id=$1
  112. if [[ -z $window_id ]]; then
  113. echo "Please specifiy a window id, you find this with 'xwininfo'"
  114. return 1
  115. fi
  116. local match_string='".*"'
  117. local match_qstring='"[^"\\]*(\\.[^"\\]*)*"' # NOTE: Adds 1 backreference
  118. # get the name
  119. xprop -id "$window_id" | \
  120. sed -nr \
  121. -e "s/^WM_CLASS\\(STRING\\) = ($match_qstring), ($match_qstring)$/instance=\\1\\nclass=\\3/p" \
  122. -e "s/^WM_WINDOW_ROLE\\(STRING\\) = ($match_qstring)$/window_role=\\1/p" \
  123. -e "/^WM_NAME\\(STRING\\) = ($match_string)$/{s//title=\\1/; h}" \
  124. -e "/^_NET_WM_NAME\\(UTF8_STRING\\) = ($match_qstring)$/{s//title=\\1/; h}" \
  125. -e "\${g; p}"
  126. }