2017-06-12 16:22:05 +02:00
###########################
#### Function Definitions #
###########################
2017-07-14 21:20:20 +02:00
## Selfupdate
function dotfiles_selfupdate
{
2022-12-23 13:38:20 +01:00
( DOTFILES_FORCEUPDATE = 1 source $DOTFILES /extras/check_for_update.zsh || echo "[dotfiles_selfupdate] failed" )
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
{
2020-02-14 16:37:41 +01:00
curl " https://cheat.sh/ $1 "
2018-03-31 02:33:42 +02:00
}
# get cheat sheets for commands matching $1
function cheatall
{
2020-02-14 16:37:41 +01:00
curl " https://cheat.sh/~ $1 "
2017-06-12 16:22:05 +02:00
}
2020-02-14 16:37:50 +01:00
# List all docker tags for an image
function dockertags
{
2019-12-07 22:32:14 +01:00
if [ [ -z $( command -v jq) ] ] ; then
2020-02-14 16:37:50 +01:00
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
}
2020-03-08 19:43:47 +01:00
# watch with grc enabled
function watchgrc
{
watch -n 1 -c grc --colour= on " $@ "
}
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
{
2019-12-07 22:32:14 +01:00
awk " BEGIN { print $* } "
2017-06-12 16:22:05 +02:00
}
2024-01-09 15:42:43 +01:00
function warning {
echo -e " ${ Yellow } Warning: $CLEAR $@ " >& 2
2017-06-12 16:22:05 +02:00
}
2024-01-09 15:42:43 +01:00
function error {
echo -e " ${ Red } Error: $CLEAR $@ " >& 2
2017-06-12 16:22:05 +02:00
}
2024-01-09 15:42:43 +01:00
function info {
echo -e " ${ Green } Info: $CLEAR $@ "
2023-12-21 16:13:48 +01:00
}
2017-06-12 16:22:05 +02:00
2024-01-09 15:42:43 +01:00
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
}
2024-04-05 15:09:39 +02:00
# set an alarm in seconds, minutes, hours
function alarm {
if [ [ -z $1 ] ] ; then
echo "Usage: alarm 5[s]|5m|1h"
return 1
fi
2024-08-02 18:19:56 +02:00
value = $1
2024-04-05 15:09:39 +02:00
local t
2024-08-02 18:19:56 +02:00
if [ [ $value = *"m" ] ] ; then
t = $(( ${ value /m/ } * 60 ))
elif [ [ $value = *"h" ] ] ; then
t = $(( ${ value /h/ } * 3600 ))
elif [ [ $value = *"s" ] ] ; then
t = ${ value /s/ }
2024-04-05 15:09:39 +02:00
else
2024-08-02 18:19:56 +02:00
t = $value
2024-04-05 15:09:39 +02:00
fi
2024-08-02 18:19:56 +02:00
function format( ) {
if [ [ $1 -lt 60 ] ] ; then
printf "00:%02is" $1
return
fi
2024-04-05 15:09:39 +02:00
2024-08-02 18:19:56 +02:00
if [ [ $1 -lt 3600 ] ] ; then
minutes = $(( $1 / 60 ))
seconds = $(( $1 % 60 ))
2024-08-02 18:19:56 +02:00
printf "%dm:%02dm" $minutes $seconds
2024-08-02 18:19:56 +02:00
return
fi
hours = $(( $1 / 3600 ))
remainder = $(( $1 % 3600 ))
printf "%dh:%s" hours " $( format $remainder ) "
}
count = $t
while [ [ $count -ge 0 ] ] ; do
echo -en " Set a timer for $1 │ remaining= $( format $count ) "
count = $(( count-1))
sleep 1
echo -en "\r"
done
echo -e "\nDone!"
beeper
2024-04-05 15:09:39 +02:00
}
2017-06-12 16:22:05 +02:00
## Simple http server for current directory (or path)
function httpserver
{
2019-12-07 22:32:14 +01:00
(
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 &
2021-10-09 00:18:10 +02:00
python -m http.server 8000
2019-12-07 22:32:14 +01:00
)
2017-06-12 16:22:05 +02:00
}
2020-03-11 15:15:44 +01:00
alias webserver = 'httpserver'
2017-06-12 16:22:05 +02:00
2021-01-07 11:25:47 +01:00
## Upload something using the 0x0.st service. Usage: upload [filename|url]
function upload
2018-03-31 02:33:42 +02:00
{
2021-01-07 11:25:47 +01:00
local url
if echo " $@ " | grep -E -s -q "^http[s]://" ; then
url = true
fi
if [ [ $url = = true ] ] ; then
out = " $( curl -F" url= $@ " https://0x0.st) "
else
out = $( curl --progress-bar -F" file=@ $@ " "https://0x0.st/" )
fi
if [ [ $? -ne 0 ] ] ; then
echo -e " Failed uploading $@ :\n $out " >& 2
return
fi
2017-06-12 16:22:05 +02:00
echo -e " Done, file at:\t $out "
2017-07-14 21:20:20 +02:00
if [ [ $( uname) = = "Darwin" ] ] ; then
2021-01-07 11:25:47 +01:00
clipboard = "pbcopy"
2023-07-10 09:12:50 +02:00
elif command -v wl-copy & >/dev/null; then
2021-01-07 11:25:47 +01:00
clipboard = "wl-copy"
2023-07-10 09:12:50 +02:00
elif command -v xclip & >/dev/null ; then
2021-01-07 11:25:47 +01:00
clipboard = "xclip"
2018-03-31 02:33:42 +02:00
else
2021-01-07 11:25:47 +01:00
clipboard = "cat"
2017-07-14 21:20:20 +02:00
fi
2023-07-10 09:12:50 +02:00
echo -en " $out " | $clipboard
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
2019-12-07 22:33:29 +01:00
# All characters excluding backlash
env LC_CTYPE = C tr -dc '[:graph]\' < /dev/urandom | fold -w $strlen | head -n 1
2017-06-12 16:22:05 +02:00
}
2017-07-14 21:20:20 +02:00
## List defined functions in $DOTFILES/functions.sh
function list_functions
{
2022-03-31 19:37:57 +02:00
grep --color= no -A 1 '^##' $DOTFILES /functions/*.zsh| sed -E 's/function (.*)/\1/g'
2017-07-14 21:20:20 +02:00
}
2022-01-24 09:58:26 +01:00
# MacOS only
2017-07-17 01:28:35 +02:00
if [ [ " $( uname) " = = "Darwin" ] ] ; then
2022-01-24 09:58:26 +01:00
## 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
2017-07-14 21:20:20 +02:00
2022-01-24 09:58:26 +01:00
# 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 "
}
2019-12-07 22:32:14 +01:00
fi
2017-06-12 16:22:05 +02:00
2020-03-17 15:03:49 +01:00
# unzip file to directory with the same name of the zip file (without extension)
2020-03-13 21:33:53 +01:00
function unzipd {
zip_file = " $1 "
filename = $( basename -- " $zip_file " )
extension = " ${ filename ##*. } "
name = " ${ filename %.* } "
unzip -d " $name " " $zip_file "
}
2020-04-28 18:18:21 +02:00
fvim( ) {
if [ [ -n " $@ " ] ] ; then
vim ` fzf -q $@ `
else
vim ` fzf`
fi
}
2020-03-17 15:03:49 +01:00
2021-07-14 10:19:21 +02:00
# retry command until it succeeds (waiting one second or $RETRY_INTERVAL)
function retry( ) {
2024-01-26 14:45:42 +01:00
until $@ ; do sleep ${ RETRY_INTERVAL :- 1 } ; done
2021-07-14 10:19:21 +02:00
}
2022-02-07 20:44:54 +01:00
__completion_wrapper( ) {
# Wrapper for completion functions.
# These can be used to force loading of all completions
# for the given command in order to access specialized
# completion functions
local _completion_function = $1
local _completion_base = $2
2023-10-25 11:34:36 +02:00
if ! command -v $_completion_function & >/dev/null; then
2022-02-07 20:44:54 +01:00
$_completion_base
fi
$_completion_function
}
2022-01-24 11:33:53 +01:00
2021-12-08 23:55:41 +01:00
if command -v pacman & >/dev/null ; then
pacbins( ) {
pacman -Ql $1 | sed -n -e 's/.*\/bin\///p' | tail -n +2
}
2022-02-07 20:44:54 +01:00
_pacbins( ) {
# fix completion
__completion_wrapper _pacman_completions_installed_packages _pacman
}
compdef _pacbins pacbins
2021-12-08 23:55:41 +01:00
fi
if command -v rustc & >/dev/null
then
rexplain( ) {
rustc --explain $@ | bat --language= rust -
}
fi
2022-01-24 11:33:53 +01:00
if command -v fzf & >/dev/null ; then
falias( ) {
alias | fzf $@ | cut -d= -f 1
}
2017-06-12 16:22:05 +02:00
fi
2022-01-24 11:33:53 +01:00
make_backup( ) {
name = " $1 "
backup_name = " backup_ $1 _ $( date +%Y-%m-%d) .tar.gz "
echo " Backing up to $backup_name "
tar -czf " $backup_name " " $1 "
echo "done"
}
2022-04-29 11:16:39 +02:00
function find_by_mtime( ) {
find $@ -printf "%T+ %p\n" | sort
2022-01-24 11:33:53 +01:00
}
2023-01-23 20:21:57 +01:00
function mangrep( ) {
if [ [ -z " $@ " ] ] ; then
echo " Usage: $0 <pattern> <command> "
echo "Opens man page for <command> at the first match for <pattern>"
return
fi
pattern = $1
cmd = $2
if [ [ -z " $cmd " ] ] ; then
man " $cmd "
return
fi
MANPAGER = " less -p \" $pattern \" " man " $cmd "
}
2023-07-10 09:11:08 +02:00
2024-08-08 17:36:58 +02:00
# watches the given file/directory and executes the given action whenever anything is changed. Usage: watch_and_run <file|directory> <command>
function watch_and_run( ) {
2024-01-09 15:53:20 +01:00
if ! which inotifywait 2>& 1 & >/dev/null ; then echo " $0 requires inotifywait " ; return 1; fi
2024-08-08 17:36:58 +02:00
[ [ -z " $1 " ] ] && echo " Usage: $0 <file|directory> <action> " && return 1
local to_watch = $1
2023-07-10 09:11:08 +02:00
shift
2024-08-08 17:36:58 +02:00
[ [ -z " $@ " ] ] && echo " Usage: $0 <file|directory> <action> " && return 1
2024-01-09 15:53:20 +01:00
local action = " $@ "
local _bg_job_pid
2023-07-10 09:11:08 +02:00
2024-01-09 15:53:20 +01:00
local TRAPINT( ) {
if [ [ -n ${ _bg_job_pid } ] ] ; then
kill -9 ${ _bg_job_pid }
fi
yellow '\nQuitting.'
break;
}
{
set +m # disable job control messages
while true; do
2024-08-08 17:36:58 +02:00
if [ [ ! -e " $to_watch " ] ] ; then
echo " \" $to_watch \" does not exist " ;
2024-07-28 14:36:31 +02:00
break
fi
2024-01-09 15:53:20 +01:00
zsh -c " $action " &
_bg_job_pid = $!
disown
echo -e " $Cyan ==> Running \" $action \" with pid= ${ _bg_job_pid } $CLEAR "
2024-08-08 17:36:58 +02:00
# block until the file/directory has been written to
inotifywait -e close_write -r " $to_watch " & >/dev/null
2024-01-09 15:53:20 +01:00
# the job might have failed, ignore errors
kill -9 ${ _bg_job_pid } & >/dev/null || true
done
} always {
# remove ctrl-c trap
unset -f TRAPINT
set -m # restore job control messages
}
2023-07-10 09:11:08 +02:00
}
2024-06-18 11:31:12 +02:00
# use watch and force color with grc
function watchc( ) {
2024-08-12 17:39:42 +02:00
watch --differences --interval 1 --color grc --colour= on $@
}
# make a backup copy of the argument adding the .bak suffix
function bak( ) {
cp $1 { ,.bak}
}
# restore a backup copy created using `bak`
function unbak( ) {
mv $1 { .bak,}
2024-06-18 11:31:12 +02:00
}
2024-07-04 11:45:11 +02:00
# highlight functions with when calling which
function which( ) {
if ! ( ( $+commands[ bat] ) ) ; then
builtin which $@
else
builtin which $@ | bat --language= zsh
fi
}