Merge functions and setup_functions

This commit is contained in:
Ivan Malison 2021-07-30 18:42:27 -06:00
parent c9d89240bc
commit 41f5b4224d
No known key found for this signature in database
GPG Key ID: 62530EFBE99DC2F8

View File

@ -1,19 +1,17 @@
function get_python_scripts_path { function command_exists {
python -c "import sysconfig; print sysconfig.get_path('scripts')" hash "$1" 2>/dev/null 1>/dev/null
} }
function path_lines { function run_if_exists {
local python_command command_exists "$@" && "$@"
# 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 indirect_expand { function shell_contains () {
eval "value=\"\${$1}\"" local e
echo $value for e in "${@:2}"; do
[[ "$1" == *"$e"* ]] && return 0
done
return 1
} }
function environment_variable_exists { function environment_variable_exists {
@ -21,6 +19,22 @@ function environment_variable_exists {
[ ! -z $value ] [ ! -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 { function exists_in_path_var {
target=${2-PATH} target=${2-PATH}
local path_contents="$(indirect_expand $target)" local path_contents="$(indirect_expand $target)"
@ -40,7 +54,7 @@ EOF
function echo_split { function echo_split {
local IFS local IFS
IFS="$2" read -rA -- arr <<EOF IFS="$2" read -rA -- arr <<EOF
$1 $1
EOF EOF
for i in "${arr[@]}"; do for i in "${arr[@]}"; do
@ -56,19 +70,11 @@ function shell_contains {
return 1 return 1
} }
function dotfiles_directory { function current_shell {
echo $(dirname `readlink -f ~/.zshrc | xargs dirname`)
}
function go2dotfiles {
cd $(dotfiles_directory)
}
function current_shell() {
which "$(ps -p $$ | tail -1 | awk '{print $NF}' | sed 's/\-//')" which "$(ps -p $$ | tail -1 | awk '{print $NF}' | sed 's/\-//')"
} }
function is_zsh() { function is_zsh {
[ ! -z ${ZSH_VERSION+x} ] [ ! -z ${ZSH_VERSION+x} ]
} }
@ -94,20 +100,6 @@ function filter_by_column_value {
awk '$'"$1"' == '"$2"' { print $0 }' awk '$'"$1"' == '"$2"' { print $0 }'
} }
# Determine size of a file or total size of a directory
function fs {
if du -b /dev/null > /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 # Start an HTTP server from a directory, optionally specifying the port
function server { function server {
local port="${1:-8000}" 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 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() { function git_diff_replacing() {
local original_sha='HEAD~1' local original_sha='HEAD~1'
local new_sha='HEAD' local new_sha='HEAD'