diff --git a/dotfiles/lib/shellenv/functions.sh b/dotfiles/lib/shellenv/functions.sh index b9e2df65..69a81b98 100644 --- a/dotfiles/lib/shellenv/functions.sh +++ b/dotfiles/lib/shellenv/functions.sh @@ -1,19 +1,17 @@ -function get_python_scripts_path { - python -c "import sysconfig; print sysconfig.get_path('scripts')" +function command_exists { + hash "$1" 2>/dev/null 1>/dev/null } -function path_lines { - local python_command - # We need to get a path to the ACTUAL python command because - # pyenv alters PATH before actually executing python, which ends - # up changing PATH in a way that is not desireable. - hash pyenv 2>/dev/null && python_command="$(pyenv which python)" || python_command="$(which python)" - "$python_command" "$HOME/.lib/python/shell_path.py" --path-lines "$@" +function run_if_exists { + command_exists "$@" && "$@" } -function indirect_expand { - eval "value=\"\${$1}\"" - echo $value +function shell_contains () { + local e + for e in "${@:2}"; do + [[ "$1" == *"$e"* ]] && return 0 + done + return 1 } function environment_variable_exists { @@ -21,6 +19,22 @@ function environment_variable_exists { [ ! -z $value ] } +function get_python_scripts_path { + python -c "import sysconfig; print sysconfig.get_path('scripts')" +} + +function path_lines { + IFS=':' read -A ADDR <<< "$PATH" + for one_path in "${ADDR[@]}"; do + echo $one_path + done +} + +function indirect_expand { + eval "value=\"\${$1}\"" + echo $value +} + function exists_in_path_var { target=${2-PATH} local path_contents="$(indirect_expand $target)" @@ -40,7 +54,7 @@ EOF function echo_split { local IFS - IFS="$2" read -rA -- arr < /dev/null 2>&1; then - local arg=-sbh - else - local arg=-sh - fi - if [[ -n "$@" ]]; then - du $arg -- "$@" - else - du $arg .[^.]* * - fi -} - # Start an HTTP server from a directory, optionally specifying the port function server { local port="${1:-8000}" @@ -126,35 +118,6 @@ function shell_stats() { history 0 | awk '{CMD[$2]++;count++;}END { for (a in CMD)print CMD[a] " " CMD[a]/count*100 "% " a;}' | grep -v "./" | column -c3 -s " " -t | sort -nr | nl | head -n20 } -function is_ssh() { - test $SSH_CLIENT -} - -# TODO: Remove this. -alias clipboard='oscopy' - -function oscopy() { - if is_osx; - then - reattach-to-user-namespace pbcopy - else - test -n "$DISPLAY" && xclip -selection c - fi -} - -function ospaste() { - if is_osx; - then - reattach-to-user-namespace pbpaste - else - xclip -o - fi -} - -function git_root() { - cd "$(git root)" -} - function git_diff_replacing() { local original_sha='HEAD~1' local new_sha='HEAD'