2017-06-12 16:22:05 +02:00
###########################
#### Function Definitions #
###########################
2017-07-14 21:20:20 +02:00
## Selfupdate
function dotfiles_selfupdate
{
( cd $DOTFILES ; git pull)
2017-06-12 16:22:05 +02:00
}
2017-07-14 21:20:20 +02:00
## get cheat sheets for commands from cheat.sh. Usage: cheat commandname
function cheat
2017-06-12 16:22:05 +02:00
{
2017-07-14 21:20:20 +02:00
curl cheat.sh/$1
2017-06-12 16:22:05 +02:00
}
2017-07-14 21:20:20 +02:00
## Simple calculator. Usage: calc 1+1, calc 5/7, calc "sqrt(2)"
function calc
2017-06-12 16:22:05 +02:00
{
2017-07-14 21:20:20 +02:00
awk "BEGIN { print " $* " }"
2017-06-12 16:22:05 +02:00
}
2017-07-14 21:20:20 +02:00
## Make new directory and cd to that directory. Usage: mcd newfolder
2017-06-12 16:22:05 +02:00
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
shift 1
printf " $color $@ $CLEAR "
}
2017-07-14 21:20:20 +02:00
## These functions return a colored version of the input string. Usage: red "string"
2017-06-12 16:22:05 +02:00
function red
{
2017-07-14 21:20:20 +02:00
echo -e " $Red $@ $CLEAR "
2017-06-12 16:22:05 +02:00
}
2017-07-14 21:20:20 +02:00
function green
{
echo -e " $Green $@ $CLEAR "
2017-06-12 16:22:05 +02:00
}
2017-07-14 21:20:20 +02:00
function blue
{
echo -e " $Blue @ $CLEAR "
2017-06-12 16:22:05 +02:00
}
## Flashes the screen until user presses a key
function flasher
{
2017-07-14 21:20:20 +02:00
while true; do printf "\e[?5h\007" ; sleep 0.25; printf "\e[?5l" ; read -s -n -t1 && break; done ;
2017-06-12 16:22:05 +02:00
}
## Beep until user presses a key
function beeper
{
2017-07-14 21:20:20 +02:00
while true; do printf "\e[?5h\007" ; sleep 0.25; printf "\e[?5l" ; read -s -n -t1 && break; done ;
2017-06-12 16:22:05 +02:00
}
2017-07-14 21:20:20 +02:00
## Search youtube for string ($1) and play video (uses mplayer)
2017-06-12 16:22:05 +02:00
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;
}
2017-07-14 21:20:20 +02:00
## Same as above, only play music (uses mplayer)
2017-06-12 16:22:05 +02:00
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
{
cwd = " $pwd "
if [ [ -e $1 ] ] ;
then
dir = " $1 "
else
dir = " $pwd "
fi
cd " $dir "
sleep 0.1 && open "http://localhost:8000" & >/dev/null &
python -m SimpleHTTPServer
}
2017-07-14 21:20:20 +02:00
## Upload something using the transfer.sh service. Usage: transfer filename
2017-06-12 16:22:05 +02:00
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 "
2017-07-14 21:20:20 +02:00
if [ [ $( uname) = = "Darwin" ] ] ; then
echo " $out " | pbcopy
else
echo " $out "
fi
2017-06-12 16:22:05 +02:00
}
2017-07-14 21:20:20 +02:00
## 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)
2017-06-12 16:22:05 +02:00
function mecp
{
2017-07-14 21:20:20 +02:00
rsync -r -P -e "ssh -p 2222" -R " $@ " localhost:~/Desktop/
2017-06-12 16:22:05 +02:00
}
2017-07-14 21:20:20 +02:00
## generate a password using pwgen, generate_password 20 generates a 20 characters long password
2017-06-12 16:22:05 +02:00
function generate_password
{
2017-07-14 21:20:20 +02:00
pwgen -1sycn $1
2017-06-12 16:22:05 +02:00
}
2017-07-14 21:20:20 +02:00
## Generate a password from dev urandom using only printable characters
function genpwd
{
if [ [ $1 ] ] ; then
2017-06-12 16:22:05 +02:00
strlen = $1
else
strlen = 32
fi
cat /dev/urandom | env LC_CTYPE = C tr -dc '[:graph:]' | fold -w $strlen | head -n 1
}
if [ [ " $( uname) " = = "Darwin" ] ] ; then
2017-07-14 21:20:20 +02:00
## Copy output of previous command to termbin.com (command line pastebin) and put in in clipboard
function termbin
{
2017-06-12 16:22:05 +02:00
nc termbin.com 9999 | pbcopy && echo -n " $( pbpaste) --> in clipboard "
}
else
2017-07-14 21:20:20 +02:00
## Copy output of previous command to termbin.com (command line pastebin)
function termbin
{
2017-06-12 16:22:05 +02:00
nc termbin 9999
}
fi
2017-07-14 21:20:20 +02:00
## List defined functions in $DOTFILES/functions.sh
function list_functions
{
cat $DOTFILES /functions.sh | grep --color= no -A 1 '^##' | sed -E 's/function (.*)/\1/g'
}
2017-06-12 16:22:05 +02:00
###########################
## MAC SPECIFIC #
###########################
2017-07-17 01:28:35 +02:00
if [ [ " $( uname) " = = "Darwin" ] ] ; then
2017-07-14 21:20:20 +02:00
## Create a RAM disk. Default size 1GB. If supplied, first argument defines the RAM disk size in GB
function ramdisk
{
2017-07-17 01:28:35 +02:00
if [ [ -e $1 ] ] ; then
2017-07-14 21:20:20 +02:00
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 "
}
## Anybar support https://github.com/tonsky/AnyBar
function anybar
{
echo -n $1 | nc -4u -w0 localhost ${ 2 :- 1738 } ;
}
## 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
{
2017-07-17 01:28:35 +02:00
if [ $# -eq 1 ] ; then
open x-man-page://$1 ;
elif [ $# -eq 2 ] ; then
open x-man-page://$1 /$2 ;
2017-07-14 21:20:20 +02:00
fi
}
fi # end of mac-specific functions
2017-06-12 16:22:05 +02:00
2017-07-17 01:28:35 +02:00
if [ [ -f ~/.dotfiles_functions ] ] ; then
2017-07-14 21:20:20 +02:00
source ~/.dotfiles_functions
2017-06-12 16:22:05 +02:00
fi