#!/bin/bash ################################################## # .functions # ------------ # Based on the amazing work of Jess Frazelle # # :author: Levi Olson # :date: 1 Feb 2018 # :version: 0.0.1 ################################################## # Simple calculator calc() { local result="" result="$(printf "scale=10;%s\\n" "$*" | bc --mathlib | tr -d '\\\n')" # └─ default (when `--mathlib` is used) is 20 if [[ "$result" == *.* ]]; then # improve the output for decimal numbers # add "0" for cases like ".5" # add "0" for cases like "-.5" # remove trailing zeros printf "%s" "$result" | sed -e 's/^\./0./' \ -e 's/^-\./-0./' \ -e 's/0*$//;s/\.$//' else printf "%s" "$result" fi printf "\\n" } # mkdir, cd into it mkcd() { mkdir -p "$@" cd "$1" } # save path on cd cd() { builtin cd $@ pwd > ~/.last_dir } # Syntax-highlight JSON strings or files # Usage: `json '{"foo":42}'` or `echo '{"foo":42}' | json` json() { if [ -t 0 ]; then # argument python -mjson.tool <<< "$*" | pygmentize -l javascript else # pipe python -mjson.tool | pygmentize -l javascript fi } # Call from a local repo to open the repository on github/bitbucket in browser # Modified version of https://github.com/zeke/ghwd repo() { # Figure out github repo base URL local base_url base_url=$(git config --get remote.origin.url) base_url=${base_url%\.git} # remove .git from end of string # Fix git@github.com: URLs base_url=${base_url//git@github\.com:/https:\/\/github\.com\/} # Fix git://github.com URLS base_url=${base_url//git:\/\/github\.com/https:\/\/github\.com\/} # Fix git@bitbucket.org: URLs base_url=${base_url//git@bitbucket.org:/https:\/\/bitbucket\.org\/} # Fix git@gitlab.com: URLs base_url=${base_url//git@gitlab\.com:/https:\/\/gitlab\.com\/} # Validate that this folder is a git folder if ! git branch 2>/dev/null 1>&2 ; then echo "Not a git repo!" exit $? fi # Find current directory relative to .git parent full_path=$(pwd) git_base_path=$(cd "./$(git rev-parse --show-cdup)" || exit 1; pwd) relative_path=${full_path#$git_base_path} # remove leading git_base_path from working directory # If filename argument is present, append it if [ "$1" ]; then relative_path="$relative_path/$1" fi # Figure out current git branch # git_where=$(command git symbolic-ref -q HEAD || command git name-rev --name-only --no-undefined --always HEAD) 2>/dev/null git_where=$(command git name-rev --name-only --no-undefined --always HEAD) 2>/dev/null # Remove cruft from branchname branch=${git_where#refs\/heads\/} [[ $base_url == *bitbucket* ]] && tree="src" || tree="tree" url="$base_url/$tree/$branch$relative_path" echo "Calling $(type open) for $url" open "$url" &> /dev/null || (echo "Using $(type open) to open URL failed." && exit 1); } # Get colors in manual pages man() { env \ LESS_TERMCAP_mb="$(printf '\e[1;31m')" \ LESS_TERMCAP_md="$(printf '\e[1;31m')" \ LESS_TERMCAP_me="$(printf '\e[0m')" \ LESS_TERMCAP_se="$(printf '\e[0m')" \ LESS_TERMCAP_so="$(printf '\e[1;44;33m')" \ LESS_TERMCAP_ue="$(printf '\e[0m')" \ LESS_TERMCAP_us="$(printf '\e[1;32m')" \ man "$@" } # get dbus session dbs() { local t=$1 if [[ -z "$t" ]]; then local t="session" fi dbus-send --$t --dest=org.freedesktop.DBus \ --type=method_call --print-reply \ /org/freedesktop/DBus org.freedesktop.DBus.ListNames } # get the name of a x window xname(){ local window_id=$1 if [[ -z $window_id ]]; then echo "Please specifiy a window id, you find this with 'xwininfo'" return 1 fi local match_string='".*"' local match_qstring='"[^"\\]*(\\.[^"\\]*)*"' # NOTE: Adds 1 backreference # get the name xprop -id "$window_id" | \ sed -nr \ -e "s/^WM_CLASS\\(STRING\\) = ($match_qstring), ($match_qstring)$/instance=\\1\\nclass=\\3/p" \ -e "s/^WM_WINDOW_ROLE\\(STRING\\) = ($match_qstring)$/window_role=\\1/p" \ -e "/^WM_NAME\\(STRING\\) = ($match_string)$/{s//title=\\1/; h}" \ -e "/^_NET_WM_NAME\\(UTF8_STRING\\) = ($match_qstring)$/{s//title=\\1/; h}" \ -e "\${g; p}" }