mirror of
https://git.decapod.one/brethil/dotfiles
synced 2024-11-21 19:11:30 +01:00
242 lines
6.5 KiB
Bash
Executable File
242 lines
6.5 KiB
Bash
Executable File
###########################
|
|
#### Function Definitions #
|
|
###########################
|
|
## Selfupdate
|
|
function dotfiles_selfupdate
|
|
{
|
|
(. $DOTFILES/check_for_update.sh && _upgrade_dotfiles)
|
|
}
|
|
|
|
## get cheat sheets for commands from cheat.sh. Usage: cheat commandname
|
|
function cheat
|
|
{
|
|
curl "https://cheat.sh/$1"
|
|
}
|
|
# get cheat sheets for commands matching $1
|
|
function cheatall
|
|
{
|
|
curl "https://cheat.sh/~$1"
|
|
}
|
|
|
|
# List all docker tags for an image
|
|
function dockertags
|
|
{
|
|
if [[ -z $(command -v jq) ]]; then
|
|
echo "jq not installed. Please install jq." 1>&2
|
|
return
|
|
fi
|
|
i=0
|
|
while [[ $? == 0 ]]; do
|
|
i=$(($i+1))
|
|
curl "https://registry.hub.docker.com/v2/repositories/library/$1/tags/?page=$i" 2>/dev/null | jq '."results"[]["name"]'
|
|
done
|
|
}
|
|
|
|
## Simple calculator. Usage: calc 1+1, calc 5/7, calc "sqrt(2)"
|
|
function calc
|
|
{
|
|
awk "BEGIN { print $* }"
|
|
}
|
|
|
|
## Make new directory and cd to that directory. Usage: mcd newfolder
|
|
function mcd
|
|
{
|
|
mkdir -p "$1"
|
|
cd $1
|
|
}
|
|
|
|
## Print full path of item in current directory
|
|
function ppath
|
|
{
|
|
echo "$PWD/$1"
|
|
}
|
|
|
|
## Color string with given color. Usage: color $colorname "string", available colors in colors.sh
|
|
function color
|
|
{
|
|
color=$1
|
|
set -x
|
|
shift 1
|
|
echo -e "$"$color"$@${CLEAR}\n"
|
|
set +x
|
|
}
|
|
|
|
|
|
## These functions return a colored version of the input string. Usage: red "string"
|
|
function red
|
|
{
|
|
echo -e "$Red$@$CLEAR"
|
|
}
|
|
function green
|
|
{
|
|
echo -e "$Green$@$CLEAR"
|
|
}
|
|
function blue
|
|
{
|
|
echo -e "$Blue$@$CLEAR"
|
|
}
|
|
|
|
## Flashes the screen until user presses a key
|
|
function flasher
|
|
{
|
|
while true; do printf "\e[?5h\007"; sleep 0.25; printf "\e[?5l"; read -s -n -t1 && break; done;
|
|
}
|
|
|
|
## Beep until user presses a key
|
|
function beeper
|
|
{
|
|
while true; do printf "\e[?5h\007"; sleep 0.25; printf "\e[?5l"; read -s -n -t1 && break; done;
|
|
}
|
|
|
|
## Search youtube for string ($1) and play video (uses mplayer)
|
|
function pyt
|
|
{
|
|
echo 'https://www.youtube.com/results?search_query='"$(sed 's/ /%20/g' <<< $(tr '\' '+' <<< "$@"))"; id=$(curl -s 'https://www.youtube.com/results?search_query='"$(sed 's/ /%20/g' <<< $(tr '\' '+' <<< "$@"))" | grep -om3 '"[[:alnum:]]\{11\}"' | awk NR==3 | tr -d \"); url='https://www.youtube.com/watch?v='"$id"; echo -e "URL:\t$url"; youtube-dl -q $url -o - | mplayer -vo corevideo -really-quiet /dev/fd/3 3<&0 </dev/tty;
|
|
}
|
|
## Same as above, only play music (uses mplayer)
|
|
function pmusic
|
|
{
|
|
echo 'https://www.youtube.com/results?search_query=HD%20'"$(sed 's/ /%20/g' <<< $(tr '\' '+' <<< "$@"))"; id=$(curl -s 'https://www.youtube.com/results?search_query='HD%20"$(sed 's/ /%20/g' <<< $(tr '\' '+' <<< "$@"))" | grep -om3 '"[[:alnum:]]\{11\}"' | awk NR==3 | tr -d \"); url='https://www.youtube.com/watch?v='"$id"; echo -e "URL:\t$url"; youtube-dl -q $url -o - | mplayer -vo corevideo -really-quiet /dev/fd/3 3<&0 </dev/tty;
|
|
}
|
|
|
|
## Simple http server for current directory (or path)
|
|
function httpserver
|
|
{
|
|
(
|
|
if [[ -d $1 ]]; then
|
|
cd "$1" && echo "Starting webserver in $(realpath $1)/" || echo "directory: $1 does not exist" >&2 || exit 1
|
|
else
|
|
echo "Starting webserver in $PWD/"
|
|
fi
|
|
open "http://localhost:8000" &>/dev/null &
|
|
if [[ $(python --version) == "Python 3"* ]]; then
|
|
python -m http.server 8000
|
|
else
|
|
python -m SimpleHTTPServer
|
|
fi
|
|
)
|
|
}
|
|
|
|
|
|
## Upload something using the transfer.sh service. Usage: transfer filename
|
|
function transfer
|
|
{
|
|
basefile=$(basename "$1" | sed -e 's/[^a-zA-Z0-9._-]/-/g')
|
|
out=$(curl --progress-bar --upload-file "$1" "https://transfer.sh/$basefile")
|
|
echo -e "Done, file at:\t$out"
|
|
if [[ $(uname) == "Darwin" ]]; then
|
|
echo "$out" | pbcopy
|
|
else
|
|
echo "$out"
|
|
fi
|
|
}
|
|
|
|
## If connecting through ssh and reverse forwarding port 2222 (ssh -R 2222:localhost:22 ), this function allows to copy the files back to the machine one is connecting from by typing 'mecp filename' (configure the username for "localhost" in ~/.ssh/config or add an username)
|
|
function mecp
|
|
{
|
|
rsync -r -P -e "ssh -p 2222" -R "$@" localhost:~/Desktop/
|
|
}
|
|
|
|
|
|
## generate a password using pwgen, generate_password 20 generates a 20 characters long password
|
|
function generate_password
|
|
{
|
|
pwgen -1sycn $1
|
|
}
|
|
|
|
## Generate a password from dev urandom using only printable characters
|
|
function genpwd
|
|
{
|
|
if [[ $1 ]]; then
|
|
strlen=$1
|
|
else
|
|
strlen=32
|
|
fi
|
|
# All characters excluding backlash
|
|
env LC_CTYPE=C tr -dc '[:graph]\' < /dev/urandom | fold -w $strlen | head -n 1
|
|
}
|
|
|
|
|
|
if [[ "$(uname)" == "Darwin" ]]; then
|
|
## Copy output of previous command to termbin.com (command line pastebin) and put in in clipboard
|
|
function termbin
|
|
{
|
|
nc termbin.com 9999 | pbcopy && echo -n "$(pbpaste) --> in clipboard"
|
|
}
|
|
else
|
|
## Copy output of previous command to termbin.com (command line pastebin)
|
|
function termbin
|
|
{
|
|
nc termbin.com 9999
|
|
}
|
|
fi
|
|
|
|
## List defined functions in $DOTFILES/functions.sh
|
|
function list_functions
|
|
{
|
|
grep --color=no -A 1 '^##' $DOTFILES/functions.sh | sed -E 's/function (.*)/\1/g'
|
|
}
|
|
|
|
|
|
###########################
|
|
## MAC SPECIFIC #
|
|
###########################
|
|
|
|
if [[ "$(uname)" == "Darwin" ]]; then
|
|
## Create a RAM disk. Default size 1GB. If supplied, first argument defines the RAM disk size in GB
|
|
function ramdisk
|
|
{
|
|
if [[ -e $1 ]]; then
|
|
sizeingb=$1
|
|
else
|
|
sizeingb=1
|
|
fi
|
|
|
|
# Numsectors is size in bytes / 512 (sector size in bytes)
|
|
name='RAM_disk'
|
|
sizeinbytes=$(($sizeingb*1000**3))
|
|
NUMSECTORS=$(($sizeinbytes/512))
|
|
mydev=$(hdiutil attach -nomount ram://$NUMSECTORS )
|
|
# strip whitespace (hdiutil outputs a lot of spaces/tabs along with the device name)
|
|
mydev=$(echo "$mydev"| xargs echo)
|
|
newfs_hfs "$mydev"
|
|
mkdir -p "/tmp/$name"
|
|
mount -t hfs "$mydev" "/tmp/$name"
|
|
echo "RAM Disk mounted: /tmp/$name"
|
|
echo "To eject (destroy) RAM disk, use:"
|
|
echo " $ diskutil eject $mydev"
|
|
}
|
|
|
|
## Open a pdf version of the man in Preview
|
|
function man2pdf
|
|
{
|
|
man -t "$1" | open -f -a Preview
|
|
}
|
|
|
|
## Open Man in separate (different-looking) window
|
|
function nman
|
|
{
|
|
if [ $# -eq 1 ] ; then
|
|
open "x-man-page://$1"
|
|
elif [ $# -eq 2 ] ; then
|
|
open "x-man-page://$1/$2"
|
|
fi
|
|
}
|
|
fi
|
|
|
|
### end of mac-specific functions
|
|
|
|
function pipupdate(){
|
|
for pip in pip2 pip3; do
|
|
if [ -z $pip ]; then
|
|
continue
|
|
fi
|
|
$pip freeze --local | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 $pip install -U
|
|
done
|
|
}
|
|
|
|
# dotfiles user functions
|
|
if [[ -f $HOME/.dotfiles_functions ]]; then
|
|
source "$HOME/.dotfiles_functions"
|
|
fi
|