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

Failed to ignore revisions in .git-blame-ignore-revs.

332 lines
8.3 KiB
Bash
Raw Normal View History

2017-06-12 16:22:05 +02:00
###########################
#### Function Definitions #
###########################
2017-07-14 21:20:20 +02:00
## Selfupdate
function dotfiles_selfupdate
{
2021-10-09 00:03:46 +02:00
(. $DOTFILES/check_for_update.zsh && _upgrade_dotfiles)
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
{
curl "https://cheat.sh/$1"
2018-03-31 02:33:42 +02:00
}
# get cheat sheets for commands matching $1
function cheatall
{
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
}
## Print full path of item in current directory
function ppath
{
2019-12-07 22:32:14 +01:00
echo "$PWD/$1"
2017-06-12 16:22:05 +02:00
}
2019-12-07 22:32:14 +01:00
## Color string with given color. Usage: color $colorname "string", available colors in colors.sh
2017-06-12 16:22:05 +02:00
function color
{
color=$1
2019-12-07 22:32:14 +01:00
set -x
2017-06-12 16:22:05 +02:00
shift 1
2019-12-07 22:32:14 +01:00
echo -e "$"$color"$@${CLEAR}\n"
set +x
2017-06-12 16:22:05 +02:00
}
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
}
2020-02-14 16:37:39 +01: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
}
## 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
## Upload something using the 0x0.st service. Usage: upload [filename|url]
function upload
2018-03-31 02:33:42 +02: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
clipboard="pbcopy"
elif command -v wl-copy; then
clipboard="wl-copy"
elif command -v xclip; then
clipboard="xclip"
2018-03-31 02:33:42 +02:00
else
clipboard="cat"
2017-07-14 21:20:20 +02:00
fi
echo -n"$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
# 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
{
2021-10-09 00:18:10 +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
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-11-13 13:26:19 +01:00
pipupdate(){
set -x
if [[ "$1" == "user" ]]; then
user_flags='--user'
shift
fi
if [[ -n "$@" ]]; then
pip="$1"
shift
flags="$*"
echo "pip command is: '$pip $flags'"
if ! command -v "$pip" ; then
echo "Given command ($1) does not exist." 1>&2
return
2019-12-07 21:31:14 +01:00
fi
2020-11-13 13:26:19 +01:00
else
pip='python'
flags='-m pip'
echo "Using default pip: $pip $flags"
fi
for package in $($pip $flags freeze $user_flags --local | grep -v '^\-e' | cut -d = -f 1) ; do
$pip $flags install "$user_flags" -U "$package"
2018-03-31 02:33:42 +02:00
done
2020-11-13 13:26:19 +01:00
set +x
2018-03-31 02:33:42 +02:00
}
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)
function unzipd {
zip_file="$1"
filename=$(basename -- "$zip_file")
extension="${filename##*.}"
name="${filename%.*}"
unzip -d "$name" "$zip_file"
}
2020-03-17 15:03:49 +01:00
# remove local branches which have been merged into master
function git_prune_branches(){
git branch --merged master | grep -v '^[ * ]*master$' | xargs git branch -d
}
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() {
local _retry_interval
if [[ -z "$RETRY_INTERVAL" ]]; then _retry_interval=1; else _retry_interval=$RETRY_INTERVAL; fi
2021-10-08 14:47:16 +02:00
until $@; do sleep $_retry_interval; done
2021-07-14 10:19:21 +02:00
}
# Creats a gitignore for the given argument (e.g. python, cpp, etc)
function gi() { curl -sL https://www.toptal.com/developers/gitignore/api/$@; }
# Runs git log with fzf with preview
compdef _git fgitlog=git-log
function fgitlog() {
git log --oneline $@ | fzf --multi --preview 'git -p show --color=always {+1}' --preview-window=right,60%
}
git-commit-show() {
git log --graph --color=always --format="%C(auto)%h%d %s %C(black)%C(bold)%cr" |
fzf --ansi --no-sort --reverse --tiebreak=index --preview \
'f() {
set -- $(echo -- "$@" | grep -o "[a-f0-9]\{7\}")
[ $# -eq 0 ] || git show --color=always $1
}
f {}' \
--bind "j:down,k:up,alt-j:preview-down,alt-k:preview-up,ctrl-f:preview-page-down,ctrl-b:preview-page-up,q:abort,ctrl-m:execute:
(grep -o '[a-f0-9]\{7\}' | head -1 |
xargs -I % sh -c 'git show --color=always % | less -R') << 'FZF-EOF'
{} FZF-EOF" \
--preview-window=right:60%
}
2021-10-08 23:49:39 +02:00
compdef __git_recent_commits git-fixup
git-fixup() {
git commit --fixup=$1
git -c sequence.editor=true rebase --interactive --autosquash $1^
}
compdef __git_branch_names git-diff-branch
git-diff-branch() {
git diff $@
}
2021-12-08 23:55:41 +01:00
if command -v pacman &>/dev/null ; then
compdef _pacman_completions_installed_packages pacbins
pacbins() {
pacman -Ql $1 | sed -n -e 's/.*\/bin\///p' | tail -n +2
}
fi
if command -v rustc &>/dev/null
then
rexplain() {
rustc --explain $@ | bat --language=rust -
}
fi
if command -v fzf &>/dev/null ; then
compdef _fzf_complete
falias() {
alias | fzf $@ | cut -d= -f 1
}
2017-06-12 16:22:05 +02:00
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"
}
make_venv(){
python -m venv .venv && echo "created venv: .venv"
read -q source_venv
if [[ $source_venv == "y" ]]; then
source .venv/bin/activate && echo "sourced venv"
fi
}
# # dotfiles user functions
# if [[ -f $HOME/.dotfiles_functions ]]; then
# source "$HOME/.dotfiles_functions"
# fi