dotfiles/zsh/functions.zsh

79 lines
2.2 KiB
Bash
Raw Normal View History

2018-06-15 13:43:04 +00:00
#!/usr/bin/env zsh
2018-02-23 09:38:24 +00:00
2019-09-24 19:34:15 +00:00
count() { echo "$#"; }
2018-02-23 09:38:24 +00:00
2019-09-28 14:36:00 +00:00
bytecount() { wc -c "$@" | numfmt --to=iec-i; }
mkcd() { mkdir -p "$@" && cd "${@[-1]}"; }
2018-02-23 09:38:24 +00:00
2019-10-27 20:34:27 +00:00
viscd() {
local temp_file chosen_dir
2019-10-28 22:13:40 +00:00
temp_file="$(mktemp -t ranger_cd.XXXXXXXXXX)"
2019-10-27 20:34:27 +00:00
ranger --choosedir="$temp_file" -- "${@:-$PWD}"
2019-10-28 22:13:40 +00:00
if chosen_dir="$(<"$temp_file")" && [[ -n "$chosen_dir" && "$chosen_dir" != "$PWD" ]]; then
2019-10-27 20:34:27 +00:00
cd -- "$chosen_dir"
fi
rm -f -- "$temp_file"
}
2019-09-24 19:34:15 +00:00
is_linux() { [[ "$OSTYPE" == linux* ]]; }
is_macos() { [[ "$OSTYPE" == darwin* ]]; }
is_android() { [[ "$OSTYPE" == linux-android ]]; }
2018-08-23 20:46:00 +00:00
2019-09-24 19:34:15 +00:00
command_exists() { command -v "$1" &>/dev/null; }
2018-11-10 20:03:34 +00:00
lazy_load() {
local command="$1"
local init_command="$2"
2019-01-09 16:55:19 +00:00
eval "$command() {
unfunction $command
$init_command
$command \$@
}"
}
2019-03-08 18:10:30 +00:00
2019-09-24 19:34:15 +00:00
welcome() { "$ZSH_DOTFILES/welcome/main.py"; }
2019-08-28 07:46:52 +00:00
2019-08-28 07:51:30 +00:00
if is_android; then
2019-09-24 19:34:15 +00:00
open_cmd='termux-open'
elif command_exists xdg-open; then
open_cmd='nohup xdg-open &> /dev/null'
else
open_cmd='print >&2 "open: Platform $OSTYPE is not supported"; return 1'
2019-08-28 07:46:52 +00:00
fi
2019-09-24 19:34:15 +00:00
eval "open(){$open_cmd \"\$@\";}"
unset open_cmd
if is_macos; then
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'
copy_cmd='print >&2 "clipcopy: '"$error_msg"'"; return 1'
paste_cmd='print >&2 "clippaste: '"$error_msg"'"; return 1'
unset error_msg
fi
eval "clipcopy(){$copy_cmd;};clippaste(){$paste_cmd;}"
unset copy_cmd paste_cmd
# for compatibility with Oh-My-Zsh plugins
# Source: https://github.com/robbyrussell/oh-my-zsh/blob/5911aea46c71a2bcc6e7c92e5bebebf77b962233/lib/git.zsh#L58-L71
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
echo "${ref#refs/heads/}"
}