2018-06-15 13:43:04 +00:00
|
|
|
#!/usr/bin/env zsh
|
2018-02-23 09:38:24 +00:00
|
|
|
|
2021-03-30 12:15:33 +00:00
|
|
|
count() { print -r -- "$#"; }
|
2018-02-23 09:38:24 +00:00
|
|
|
|
2021-05-15 19:00:13 +00:00
|
|
|
bytecount() { wc -c "$@" | bytefmt2; }
|
2019-09-28 14:36:00 +00:00
|
|
|
|
|
|
|
mkcd() { mkdir -p "$@" && cd "${@[-1]}"; }
|
2018-02-23 09:38:24 +00:00
|
|
|
|
2021-05-10 18:38:37 +00:00
|
|
|
# Re-added from:
|
|
|
|
# https://github.com/dmitmel/dotfiles/blob/16f0a1cf32ec97355da2e17de1c4bb458431767b/zsh/functions.zsh#L19
|
|
|
|
source_if_exists() { [[ -f "$1" ]] && source "$1" }
|
|
|
|
|
2021-05-10 15:20:40 +00:00
|
|
|
silence() { $1 &>/dev/null }
|
|
|
|
|
2019-10-27 20:34:27 +00:00
|
|
|
viscd() {
|
2020-09-05 15:09:29 +00:00
|
|
|
setopt local_options err_return
|
2019-10-27 20:34:27 +00:00
|
|
|
local temp_file chosen_dir
|
2019-10-28 22:13:40 +00:00
|
|
|
temp_file="$(mktemp -t ranger_cd.XXXXXXXXXX)"
|
2020-09-05 15:09:29 +00:00
|
|
|
{
|
2020-10-21 11:27:42 +00:00
|
|
|
ranger --choosedir="$temp_file" -- "${@:-$PWD}"
|
2020-09-05 15:09:29 +00:00
|
|
|
if chosen_dir="$(<"$temp_file")" && [[ -n "$chosen_dir" && "$chosen_dir" != "$PWD" ]]; then
|
|
|
|
cd -- "$chosen_dir"
|
|
|
|
fi
|
|
|
|
} always {
|
|
|
|
rm -f -- "$temp_file"
|
|
|
|
}
|
2019-10-27 20:34:27 +00:00
|
|
|
}
|
|
|
|
|
2019-09-24 19:34:15 +00:00
|
|
|
command_exists() { command -v "$1" &>/dev/null; }
|
2018-06-15 13:50:24 +00:00
|
|
|
|
2018-11-10 20:03:34 +00:00
|
|
|
lazy_load() {
|
2018-06-15 13:50:24 +00:00
|
|
|
local command="$1"
|
|
|
|
local init_command="$2"
|
|
|
|
|
2019-01-09 16:55:19 +00:00
|
|
|
eval "$command() {
|
|
|
|
unfunction $command
|
|
|
|
$init_command
|
|
|
|
$command \$@
|
|
|
|
}"
|
2018-06-15 13:50:24 +00:00
|
|
|
}
|
2019-03-08 18:10:30 +00:00
|
|
|
|
2020-01-05 11:58:58 +00:00
|
|
|
if (( ! _is_macos )); then
|
|
|
|
if (( _is_android )); then
|
2019-11-16 13:04:10 +00:00
|
|
|
open_cmd='termux-open'
|
|
|
|
elif command_exists xdg-open; then
|
|
|
|
open_cmd='nohup xdg-open &> /dev/null'
|
|
|
|
else
|
2021-03-30 12:15:33 +00:00
|
|
|
open_cmd='print >&2 -r -- "open: Platform $OSTYPE is not supported"; return 1'
|
2019-11-16 13:04:10 +00:00
|
|
|
fi
|
2019-12-23 09:52:49 +00:00
|
|
|
eval "open(){local f; for f in \"\$@\"; do $open_cmd \"\$f\"; done;}"
|
2019-11-16 13:04:10 +00:00
|
|
|
unset open_cmd
|
2019-08-28 07:46:52 +00:00
|
|
|
fi
|
2019-09-24 19:34:15 +00:00
|
|
|
|
2020-01-05 11:58:58 +00:00
|
|
|
if (( _is_macos )); then
|
2019-09-24 19:34:15 +00:00
|
|
|
copy_cmd='pbcopy' paste_cmd='pbpaste'
|
|
|
|
elif command_exists xclip; then
|
|
|
|
copy_cmd='xclip -in -selection clipboard' paste_cmd='xclip -out -selection clipboard'
|
|
|
|
elif command_exists xsel; then
|
|
|
|
copy_cmd='xsel --clipboard --input' paste_cmd='xsel --clipboard --output'
|
|
|
|
elif command_exists termux-clipboard-set && command_exists termux-clipboard-get; then
|
|
|
|
copy_cmd='termux-clipboard-set' paste_cmd='termux-clipboard-get'
|
|
|
|
else
|
|
|
|
error_msg='Platform $OSTYPE is not supported'
|
2021-03-30 12:15:33 +00:00
|
|
|
copy_cmd='print >&2 -r -- "clipcopy: '"$error_msg"'"; return 1'
|
|
|
|
paste_cmd='print >&2 -r -- "clippaste: '"$error_msg"'"; return 1'
|
2019-09-24 19:34:15 +00:00
|
|
|
unset error_msg
|
|
|
|
fi
|
2020-09-05 15:09:29 +00:00
|
|
|
eval "clipcopy() { $copy_cmd; }; clippaste() { $paste_cmd; }"
|
2019-09-24 19:34:15 +00:00
|
|
|
unset copy_cmd paste_cmd
|
2019-10-30 16:32:43 +00:00
|
|
|
|
2019-12-21 19:19:24 +00:00
|
|
|
# for compatibility with Oh My Zsh plugins
|
|
|
|
# Source: https://github.com/ohmyzsh/ohmyzsh/blob/5911aea46c71a2bcc6e7c92e5bebebf77b962233/lib/git.zsh#L58-L71
|
2019-10-30 16:32:43 +00:00
|
|
|
git_current_branch() {
|
|
|
|
if [[ "$(command git rev-parse --is-inside-work-tree)" != true ]]; then
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
local ref
|
|
|
|
ref="$(
|
|
|
|
command git symbolic-ref --quiet HEAD 2> /dev/null ||
|
|
|
|
command git rev-parse --short HEAD 2> /dev/null
|
|
|
|
)" || return
|
2021-03-30 12:15:33 +00:00
|
|
|
print -r -- "${ref#refs/heads/}"
|
2019-10-30 16:32:43 +00:00
|
|
|
}
|
2020-07-18 19:48:25 +00:00
|
|
|
|
2020-09-04 19:56:14 +00:00
|
|
|
declare -A date_formats=(
|
|
|
|
iso '%Y-%m-%dT%H:%M:%SZ'
|
|
|
|
normal '%Y-%m-%d %H:%M:%S'
|
|
|
|
compact '%Y%m%d%H%M%S'
|
|
|
|
only-date '%Y-%m-%d'
|
|
|
|
only-time '%H:%M:%S'
|
2021-02-05 09:13:25 +00:00
|
|
|
timestamp '%s'
|
2020-09-04 19:56:14 +00:00
|
|
|
)
|
2020-07-18 19:48:25 +00:00
|
|
|
|
2020-09-04 19:56:14 +00:00
|
|
|
for format_name format in "${(kv)date_formats[@]}"; do
|
|
|
|
eval "date-fmt-${format_name}() { date +${(q)format} \"\$@\"; }"
|
|
|
|
done; unset format_name format
|
|
|
|
|
|
|
|
unset date_formats
|
2020-10-04 16:26:44 +00:00
|
|
|
|
|
|
|
if (( _is_linux )) && command_exists swapoff && command_exists swapon; then
|
|
|
|
deswap() { sudo sh -c 'swapoff --all && swapon --all'; }
|
|
|
|
fi
|
2021-05-09 07:55:05 +00:00
|
|
|
|
|
|
|
# Taken from <https://vi.stackexchange.com/a/7810/34615>
|
|
|
|
sudoedit() {
|
|
|
|
SUDO_COMMAND="sudoedit $@" command sudoedit "$@"
|
|
|
|
}
|
|
|
|
alias sudoe="sudoedit"
|
|
|
|
alias sue="sudoedit"
|
2021-05-10 14:05:55 +00:00
|
|
|
|
|
|
|
# gpg-crypt {{{
|
|
|
|
# Encrypt the given file or directory to a given recipient
|
|
|
|
function gpg-encrypt() {
|
|
|
|
if [ "$#" -ne 2 ]; then
|
|
|
|
echo "Usage: $0 FILE/DIRECTORY RECIPIENT" >&2
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
tar -c `basename $1` | gpg --encrypt --recipient $2 -o `basename $1`.tar.gpg
|
|
|
|
}
|
|
|
|
|
|
|
|
# Decrypt the given tar.gpg file
|
|
|
|
function gpg-decrypt() {
|
|
|
|
if [ "$#" -ne 1 ] || [[ "$1" != *.tar.gpg ]]; then
|
|
|
|
echo "Usage: $0 FILE.tar.gpg" >&2
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
gpg --quiet --decrypt $1 | tar -x
|
|
|
|
}
|
|
|
|
# }}}
|