tvl-depot/functions/misc_functions.sh
2018-01-08 15:42:01 -05:00

150 lines
3.5 KiB
Bash

# fuzzily-find-branch
function wgfb {
echo $(git branch -a | fzf-tmux)
}
# utility function for swapping filenames, eg init.el and init.el.bak
function swap-file-names {
file_a=$1
file_b=$2
backup=$(mktemp backup.XXX)
mv ${file_a} ${backup}
mv ${file_b} ${file_a}
mv ${backup} ${file_b}
rm ${backup}
echo "Swapped: ${file_a} <-> ${file_b}"
}
# View a directory's contents in a periodically updated fashion.
function periodically-refresh-contents {
clear
lt .
sleep 1
refresh-contents
}
# download files to /tmp directory
function wdownload {
URL="$1"
FILENAME="$(basename $URL)"
wget -O /tmp/"$FILENAME" $URL >/dev/null && open /tmp && echo "Downloaded to: /tmp/$FILENAME" || echo "Error ..."
}
# spell checker
function wspcheck {
if [ $# -ge 1 -a -f "$1" ] && input="$1" || input="-"
cat "$input" | tr '[:upper:]' '[:lower:]' | tr -cd '[:alpha:]_ \n' | tr -s ' ' '\n' | sort | comm -23 - ~/english_words.txt
}
# trims leading and trailing whitespace
function trim {
input="$1"
echo "${input//[[:blank:]]/}"
}
# Extends `codemod` to exclude dirs in .gitignore file
function cm {
extensions="$1"
regex="$2"
replacement="$3"
ignore_dirs=""
if [ -f ./.gitignore ]; then
# Sanitizes .gitignore and converts it to a comma-separated list
ignore_dirs="$(sed 's/^\//.\//g' <./.gitignore | sed -e 's/#.*$//' -e '/^$/d' | tr '\n' ',' | sed 's/,$//')"
fi
codemod -m -d . --extensions ${extensions} --exclude-paths ${ignore_dirs} ${regex} ${replacement}
}
function tt() {
sessionName="${1}"
if ! tmux has-session -t "${sessionName}" 2> /dev/null; then
oldTMUX="${TMUX}"
unset TMUX
tmux new -d -s "${sessionName}"
export TMUX="${oldTMUX}"
unset oldTMUX
fi
if [[ -n "${TMUX}" ]]; then
tmux switch-client -t "${sessionName}"
else
tmux attach -t "${sessionName}"
fi
}
# generates placeholder content for FE work
function lorem {
text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
echo $text
}
# generates lorem and adds to pasteboard
function loremcp {
lorem | pbcopy
}
# searches all PATH directories for a keyword
function wsearchpath {
echo $PATH | tr ':' '\n' | xargs -I {} find {} -type f -perm +111 -maxdepth 1 -name "*${1}*" -print | xargs basename
}
function ff {
# finds files smartly; using rg under-the-hood so blacklisted files are ignored
glob=$1
rg -g "*$glob*" --files
}
# tests an internet connection
function is_online {
wget -q --spider "http://google.com"
if [ $? -eq 0 ]; then
echo "Online"
return 0
else
echo "Offline"
return 1
fi
}
function du-it-live () {
# live updates the du information for the current directory
directory=$1
while true; do
du -hc $directory | tail -n 1 | tr -d '\n' && echo -n ' ' && sleep 0.5
# elipsis
echo -n '.' && sleep 0.5 &&
echo -n '.' && sleep 0.5 &&
echo -n '.' && sleep 0.5 &&
# clear the three-dots
echo -n '\b\b\b' && echo -n ' ' && echo -n '\r'
done
}
# programmatically get the router's IP address
function router {
netstat -nr | grep default | head -n 1 | awk '{ print $2 }'
}