Make functions.sh style consistent

This commit is contained in:
Ivan Malison 2021-07-28 17:37:40 -06:00
parent 8866d80e75
commit fe59575676
No known key found for this signature in database
GPG Key ID: 62530EFBE99DC2F8
2 changed files with 11 additions and 191 deletions

View File

@ -47,3 +47,4 @@ uber.el
url url
var var
xkcd xkcd
.lsp-session-v1

View File

@ -27,7 +27,7 @@ function exists_in_path_var {
[[ ":$path_contents:" == *":$1:"* ]] [[ ":$path_contents:" == *":$1:"* ]]
} }
function split_into_vars () { function split_into_vars {
local string IFS local string IFS
string="$1" string="$1"
@ -38,7 +38,7 @@ $string
EOF 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
@ -48,7 +48,7 @@ EOF
done done
} }
function shell_contains () { function shell_contains {
local e local e
for e in "${@:2}"; do for e in "${@:2}"; do
[[ "$1" == *"$e"* ]] && return 0 [[ "$1" == *"$e"* ]] && return 0
@ -56,21 +56,14 @@ function shell_contains () {
return 1 return 1
} }
function dotfiles_directory() { function dotfiles_directory {
echo $(dirname `readlink -f ~/.zshrc | xargs dirname`) echo $(dirname `readlink -f ~/.zshrc | xargs dirname`)
} }
function go2dotfiles() { function go2dotfiles {
cd $(dotfiles_directory) cd $(dotfiles_directory)
} }
function update_dotfiles() {
local old_pwd=$(pwd)
go2dotfiles
git ffo
cd $old_pwd
}
function current_shell() { function current_shell() {
which "$(ps -p $$ | tail -1 | awk '{print $NF}' | sed 's/\-//')" which "$(ps -p $$ | tail -1 | awk '{print $NF}' | sed 's/\-//')"
} }
@ -79,24 +72,11 @@ function is_zsh() {
[ ! -z ${ZSH_VERSION+x} ] [ ! -z ${ZSH_VERSION+x} ]
} }
function git_diff_add() { function git_diff_add {
git status --porcelain | awk '{print $2}' | xargs -I filename sh -c "git du filename && git add filename" git status --porcelain | awk '{print $2}' | xargs -I filename sh -c "git du filename && git add filename"
} }
function confirm() { function get_cols {
# call with a prompt string or use a default
read -r -p "$1" response
case $response in
[yY][eE][sS]|[yY])
return 0
;;
*)
return 1
;;
esac
}
function get_cols() {
FS=' ' FS=' '
OPTIND=1 OPTIND=1
while getopts "F:" OPTCHAR; do while getopts "F:" OPTCHAR; do
@ -114,16 +94,8 @@ function filter_by_column_value {
awk '$'"$1"' == '"$2"' { print $0 }' awk '$'"$1"' == '"$2"' { print $0 }'
} }
function find_all_ssh_agent_sockets() {
find /tmp -type s -name agent.\* 2> /dev/null | grep '/tmp/ssh-.*/agent.*'
}
function set_ssh_agent_socket() {
export SSH_AUTH_SOCK=$(find_all_ssh_agent_sockets | tail -n 1 | awk -F: '{print $1}')
}
# Determine size of a file or total size of a directory # Determine size of a file or total size of a directory
function fs() { function fs {
if du -b /dev/null > /dev/null 2>&1; then if du -b /dev/null > /dev/null 2>&1; then
local arg=-sbh local arg=-sbh
else else
@ -137,7 +109,7 @@ function fs() {
} }
# 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}"
sleep 1 && open "http://localhost:${port}/" & sleep 1 && open "http://localhost:${port}/" &
# Set the default Content-Type to `text/plain` instead of `application/octet-stream` # Set the default Content-Type to `text/plain` instead of `application/octet-stream`
@ -146,7 +118,7 @@ function server() {
} }
# All the dig info # All the dig info
function digga() { function digga {
dig +nocmd "$1" any +multiline +noall +answer dig +nocmd "$1" any +multiline +noall +answer
} }
@ -261,59 +233,14 @@ function git_prune_all_history_involving {
--prune-empty --tag-name-filter cat -- --all --prune-empty --tag-name-filter cat -- --all
} }
function set_osx_hostname() {
local new_hostname="${1-imalison}"
sudo scutil --set ComputerName $new_hostname
sudo scutil --set HostName $new_hostname
sudo scutil --set LocalHostName $new_hostname
sudo defaults write /Library/Preferences/SystemConfiguration/com.apple.smb.server NetBIOSName -string $new_hostname
}
function pip_package_location() { function pip_package_location() {
pip show $1 | grep Location | get_cols 2 pip show $1 | grep Location | get_cols 2
} }
function set_modifier_keys_on_all_keyboards() {
for vendor_product_id in $(get_keyboard_vendor_id_product_id_pairs | tr " " "-"); do
set_modifier_keys_for_vendor_product_id $vendor_product_id 0 2; echo $vendor_product_id;
done;
}
function get_keyboard_vendor_id_product_id_pairs() {
ioreg -n IOHIDKeyboard -r | grep -e 'class IOHIDKeyboard' -e VendorID\" -e Product | gawk 'BEGIN { RS = "class IOHIDKeyboard" } match($0, /VendorID. = ([0-9]*)/, arr) { printf arr[1]} match($0, /ProductID. = ([0-9]*)/, arr) { printf " %s\n", arr[1]} '
}
function git_config_string() { function git_config_string() {
git config -f $1 --list | xargs -I kv printf '-c \"%s\" ' kv git config -f $1 --list | xargs -I kv printf '-c \"%s\" ' kv
} }
function talk_dirty_to_me() {
python - <<EOF
from random import randrange
import re
import urllib
def talk_dirty_to_me():
socket = urllib.urlopen("http://www.youporn.com/random/video/")
htmlSource = socket.read()
socket.close()
result = re.findall('<p class="message">((?:.|\\n)*?)</p>', htmlSource)
if len(result):
print result[randrange(len(result))]
else:
talk_dirty_to_me()
talk_dirty_to_me()
EOF
}
function dirty_talk() {
while :
do
talk_dirty_to_me | tee >(cat) | say
done
}
function track_modified { function track_modified {
local timestamp_file="/tmp/__track_modified_timestamp__" local timestamp_file="/tmp/__track_modified_timestamp__"
touch $timestamp_file touch $timestamp_file
@ -328,15 +255,6 @@ function python_module_path {
python -c "import os, $1; print(os.path.dirname($1.__file__))" python -c "import os, $1; print(os.path.dirname($1.__file__))"
} }
function mu4e_directory {
if is_osx; then
echo "$(brew --prefix mu)/share/emacs/site-lisp/mu4e"
else
# TODO: make this cleaner.
echo "/usr/share/emacs/site-lisp/mu4e"
fi
}
function timestamp { function timestamp {
date +%s date +%s
} }
@ -349,106 +267,18 @@ function parse_timestamp2 {
date -d "@$(echo $1 | cut -c -10)" -Iseconds date -d "@$(echo $1 | cut -c -10)" -Iseconds
} }
function clear_path {
export PATH="/usr/local/sbin:/usr/local/bin:/usr/bin"
unset PATH_HELPER_RAN
unset ENVIRONMENT_SETUP_DONE
}
function refresh_config {
clear_path
source ~/.zshenv
source ~/.zshrc
}
function file_ends_with_newline { function file_ends_with_newline {
[[ $(tail -c1 "$1" | wc -l) -gt 0 ]] [[ $(tail -c1 "$1" | wc -l) -gt 0 ]]
} }
function add_authorized_key_to_host {
local command='test -e ~/.ssh/authorized_keys && [[ $(tail -c1 ~/.ssh/authorized_keys | wc -l) -gt 0 ]] || echo "\n" >> ~/.ssh/authorized_keys;'"echo $(cat ~/.ssh/id_rsa.pub) >> ~/.ssh/authorized_keys"
echo "Running:"
echo $command
ssh $1 "$command"
}
function add_ssh_key {
[[ $(tail -c1 ~/.ssh/authorized_keys | wc -l) -gt 0 ]] || echo "\n" >> ~/.ssh/authorized_keys;
echo $1 >> ~/.ssh/authorized_keys;
}
function git_free_ssh_rsync {
echo $1
rsync -avz -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" --progress --exclude=".git/" $(readlink -f "$1") $2:$1
}
function project_sync {
git_free_ssh_rsync '~/Projects/'"$1"'/' $2
}
function android_sdk_directory {
if is_osx; then
brew --prefix android-sdk
fi
}
function pkill_zsh {
ps aux | grep "$1" | grep -v grep | get_cols 2 | xargs kill -9
}
function find_by_size {
find . -type f -size +$1
}
function get_git_project_name {
# "$(basename $(git rev-parse --show-toplevel))"
basename $(git remotes | get_cols 2 | head -n 1)
}
function add_github_remote {
local project_name="$(get_git_project_name)"
git remote add "$1" "git@github.com:$1/$project_name"
}
function define_jump_alias_with_completion {
eval "alias $1='jump_cd $2'"
compdef "_files -g '$2/*'" $1
}
function jump_cd {
if [[ $2 =~ ^/ ]]; then
cd $2
else
cd $1
cd $2
fi
}
function source_if_exists { function source_if_exists {
test -r "$1" && source "$1" test -r "$1" && source "$1"
} }
function python_module_exists {
python_module_path $@ 1>/dev/null 2>/dev/null
}
function set_default_prompt {
python_module_exists powerline && set_powerline_prompt || set_my_prompt
}
function edit_script { function edit_script {
$EDITOR "$(which $1)" $EDITOR "$(which $1)"
} }
function in_git_directory {
[ -d .git ]
}
function process_running {
[[ ! -z "$(pgrep $@)" ]]
}
function which_readlink { function which_readlink {
readlink -f "$(which $1)" readlink -f "$(which $1)"
} }
@ -460,9 +290,6 @@ function localip {
;; ;;
'Linux') 'Linux')
ip -4 addr | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | grep -v 127.0.0.1 | head -n 1 ip -4 addr | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | grep -v 127.0.0.1 | head -n 1
# This was a nicer solution, but it returns ipv6 addresses on
# machines that have them:
# hostname --ip-address
;; ;;
esac esac
} }
@ -480,11 +307,3 @@ function all_after_char {
echo ${line##*$1} echo ${line##*$1}
done; done;
} }
function find_local_ssh_hosts {
nmap -p 22 --open -sV 10.0.0.0/24 | grep -Eo "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
}
function rwhich {
readlink -f $(which $1)
}