1
0
mirror of https://git.decapod.one/brethil/dotfiles synced 2024-11-06 04:11:31 +01:00
dotfiles/functions/misc.zsh

302 lines
7.3 KiB
Bash
Executable File

###########################
#### Function Definitions #
###########################
## Selfupdate
function dotfiles_selfupdate
{
(DOTFILES_FORCEUPDATE=1 source $DOTFILES/extras/check_for_update.zsh || echo "[dotfiles_selfupdate] failed")
}
## 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
}
# watch with grc enabled
function watchgrc
{
watch -n 1 -c grc --colour=on "$@"
}
## Simple calculator. Usage: calc 1+1, calc 5/7, calc "sqrt(2)"
function calc
{
awk "BEGIN { print $* }"
}
## Print full path of item in current directory
function ppath
{
echo "$PWD/$1"
}
## Color string with given color. Usage: `color $NAME "string"`, available colors in `colors.sh`
function color
{
local color=$1
shift 1
echo -e "${color}$@${CLEAR}"
}
## 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;
}
## 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 &
python -m http.server 8000
)
}
alias webserver='httpserver'
## Upload something using the 0x0.st service. Usage: upload [filename|url]
function upload
{
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
echo -e "Done, file at:\t$out"
if [[ $(uname) == "Darwin" ]]; then
clipboard="pbcopy"
elif command -v wl-copy &>/dev/null; then
clipboard="wl-copy"
elif command -v xclip &>/dev/null ; then
clipboard="xclip"
else
clipboard="cat"
fi
echo -en "$out" | $clipboard
}
## 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
}
## List defined functions in $DOTFILES/functions.sh
function list_functions
{
grep --color=no -A 1 '^##' $DOTFILES/functions/*.zsh| sed -E 's/function (.*)/\1/g'
}
# MacOS only
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"
}
fi
# unzip file to directory with the same name of the zip file (without extension)
function unzipd {
zip_file="$1"
filename=$(basename -- "$zip_file")
extension="${filename##*.}"
name="${filename%.*}"
unzip -d "$name" "$zip_file"
}
fvim() {
if [[ -n "$@" ]]; then
vim `fzf -q $@`
else
vim `fzf`
fi
}
# retry command until it succeeds (waiting one second or $RETRY_INTERVAL)
function retry() {
local _retry_interval
if [[ -z "$RETRY_INTERVAL" ]]; then _retry_interval=1; else _retry_interval=$RETRY_INTERVAL; fi
until $@; do sleep $_retry_interval; done
}
__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
if ! command -v $_completion_function; then
$_completion_base
fi
$_completion_function
}
if command -v pacman &>/dev/null ; then
pacbins() {
pacman -Ql $1 | sed -n -e 's/.*\/bin\///p' | tail -n +2
}
_pacbins(){
# fix completion
__completion_wrapper _pacman_completions_installed_packages _pacman
}
compdef _pacbins pacbins
fi
if command -v rustc &>/dev/null
then
rexplain() {
rustc --explain $@ | bat --language=rust -
}
fi
if command -v fzf &>/dev/null ; then
falias() {
alias | fzf $@ | cut -d= -f 1
}
fi
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"
}
function find_by_mtime() {
find $@ -printf "%T+ %p\n" | sort
}
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"
}
# watches the given file and executes the given action whenever the file is changed. Usage: watch_file <file> <command>
function watch_file() {
if ! which inotifywait &>/dev/null ; then echo "$0 requires inotifywait"; return 1; fi
[[ -z "$1" ]] && "Usage: $0 <file> <action>" && return 1
local file
file="$1"
shift
[[ -z "$@" ]] && "Usage: $0 <file> <action>" && return 1
local action
action="$@"
while inotifywait -e close_write "$file"; do zsh -c "$action"; done
}