initial commit
This commit is contained in:
83
.config/zsh/lib/alternatives.zsh
Normal file
83
.config/zsh/lib/alternatives.zsh
Normal file
@@ -0,0 +1,83 @@
|
||||
#!/bin/zsh
|
||||
|
||||
## alternatives list is pipe-separated list of commands/binaries
|
||||
|
||||
## find (first) candidate in alternatives
|
||||
## $1 - alternatives list
|
||||
## $2 - arguments to test command (USE WITH CAUTION!)
|
||||
z-alt-find() {
|
||||
local i c r t
|
||||
local -a v a
|
||||
|
||||
v=( ${(@s:|:)1} )
|
||||
[ ${#v} = 0 ] && v=( "$1" )
|
||||
for i ( $v ) ; do
|
||||
a=( ${(@s: :)i} )
|
||||
|
||||
c=$(which "${a[1]}")
|
||||
[ -z "$c" ] && continue
|
||||
|
||||
# r=$(readlink -f "$c" 2>/dev/null)
|
||||
# [ -z "$r" ] && continue
|
||||
# a[1]="$r"
|
||||
|
||||
if [ -n "$2" ] ; then
|
||||
t="$a $2"
|
||||
command ${(@s: :)t} </dev/null &>/dev/null || continue
|
||||
fi
|
||||
|
||||
echo "${a[@]}"
|
||||
return 0
|
||||
done
|
||||
|
||||
return 127
|
||||
}
|
||||
|
||||
## set function alias for alternative (one-time static resolve)
|
||||
## $1 - function name
|
||||
## $2 - alternatives list
|
||||
## $3 - command wrapper
|
||||
## $4 - function prologue
|
||||
## $5 - function epilogue
|
||||
z-alt-set-static() {
|
||||
local n t a r
|
||||
local -a s
|
||||
n="$1" ; t=''
|
||||
if [[ "$n" =~ '\|' ]] ; then
|
||||
t=${n:${MBEGIN}} ; n=${n:0:${MBEGIN}-1}
|
||||
fi
|
||||
a=$(z-alt-find "$2" "$t")
|
||||
if [ -n "$a" ] ; then
|
||||
r=0
|
||||
[ -n "$4" ] && s+=( "$4 ;" )
|
||||
[ -n "$3" ] && s+=( "$3" )
|
||||
s+=( "command $a \"\$@\" || return 127" )
|
||||
[ -n "$5" ] && s+=( "; $5" )
|
||||
else
|
||||
r=127
|
||||
s+=( 'return 127' )
|
||||
fi
|
||||
eval "$n () { ${s[@]} ; } ; typeset -g $n"
|
||||
return $r
|
||||
}
|
||||
|
||||
## set function alias for alternative (dynamic resolve)
|
||||
## $1 - function name
|
||||
## $2 - alternatives list
|
||||
## $3 - command wrapper
|
||||
## $4 - function prologue
|
||||
## $5 - function epilogue
|
||||
z-alt-set-dynamic() {
|
||||
local n t
|
||||
local -a s
|
||||
n="$1" ; t=''
|
||||
if [[ "$n" =~ '\|' ]] ; then
|
||||
t=${n:${MBEGIN}} ; n=${n:0:${MBEGIN}-1}
|
||||
fi
|
||||
[ -n "$4" ] && s+=( "$4 ;" )
|
||||
s+=( 'local a=$(z-alt-find' "${(qq)2}" "${t:+' $t'} ) ;" )
|
||||
[ -n "$3" ] && s+=( "$3" )
|
||||
s+=( 'command ${(@s: :)a} "$@" || return 127' )
|
||||
[ -n "$5" ] && s+=( "; $5" )
|
||||
eval "$n () { ${s[@]} ; } ; typeset -g $n"
|
||||
}
|
52
.config/zsh/lib/cmdtime.zsh
Normal file
52
.config/zsh/lib/cmdtime.zsh
Normal file
@@ -0,0 +1,52 @@
|
||||
#!/bin/zsh
|
||||
|
||||
zmodload -i zsh/datetime
|
||||
|
||||
z-time() {
|
||||
local a b elapsed result
|
||||
a=${EPOCHREALTIME}
|
||||
"$@"
|
||||
result=$?
|
||||
b=$(( ${EPOCHREALTIME} - a ))
|
||||
elapsed=$(z-ts-to-human "$b" 6)
|
||||
echo 1>&2
|
||||
echo "time took: ${elapsed}" 1>&2
|
||||
return ${result}
|
||||
}
|
||||
|
||||
if autoload -Uz add-zsh-hook ; then
|
||||
|
||||
typeset -gA ZSHU_PS
|
||||
ZSHU_PS[cmd_threshold]=3
|
||||
|
||||
__z_cmdtime_precmd() {
|
||||
local t=${EPOCHREALTIME}
|
||||
# local t=${(%):-%D{%s.%9.}}
|
||||
|
||||
ZSHU_PS[elapsed]=''
|
||||
(( ${+ZSHU_PS[cmd_ts]} )) || return
|
||||
|
||||
t=$(( t - ${ZSHU_PS[cmd_ts]} ))
|
||||
unset "ZSHU_PS[cmd_ts]"
|
||||
|
||||
local x=$(( ${ZSHU_PS[cmd_threshold]} + 0 ))
|
||||
[ "$x" = '0' ] && return
|
||||
|
||||
x=$(( t - x ))
|
||||
[ "${x:0:1}" = '-' ] && return
|
||||
|
||||
local elapsed=$(z-ts-to-human "$t")
|
||||
ZSHU_PS[elapsed]=" %f[%B%F{yellow}+${elapsed}%b%f] "
|
||||
}
|
||||
|
||||
__z_cmdtime_preexec() {
|
||||
ZSHU_PS[cmd_ts]=${EPOCHREALTIME}
|
||||
# ZSHU_PS[cmd_ts]=${(%):-%D{%s.%9.}}
|
||||
}
|
||||
|
||||
add-zsh-hook precmd __z_cmdtime_precmd
|
||||
add-zsh-hook preexec __z_cmdtime_preexec
|
||||
|
||||
else
|
||||
echo "cmd time measurement is disabled due to missing hook support" 1>&2
|
||||
fi
|
69
.config/zsh/lib/completion.zsh
Normal file
69
.config/zsh/lib/completion.zsh
Normal file
@@ -0,0 +1,69 @@
|
||||
#!/bin/zsh
|
||||
|
||||
ZSHU[f_compdump]="${ZSHU[d_cache]}/compdump"
|
||||
ZSHU[d_compcache]="${ZSHU[d_cache]}/compcache"
|
||||
[ -d "${ZSHU[d_compcache]}" ] || mkdir -p "${ZSHU[d_compcache]}"
|
||||
|
||||
fpath=( "${ZSHU[d_compcache]}" $fpath )
|
||||
|
||||
__z_compdump_print() { printf '#zshu %s %s\n' "$1" "${(P)1}" ; }
|
||||
|
||||
__z_compdump_invalidate() {
|
||||
command rm -f "${ZSHU[f_compdump]}"
|
||||
find "${ZSHU[d_compcache]}/" -mindepth 1 -type f '!' -name '.keep' -delete
|
||||
ZSHU[compdump_refresh]=1
|
||||
}
|
||||
|
||||
__z_compdump_verify() {
|
||||
unset "ZSHU[compdump_refresh]"
|
||||
ZSHU[compdump_meta]='ZSH_VERSION ZSH_PATCHLEVEL FPATH PATH'
|
||||
local i s
|
||||
for i ( ${(s: :)ZSHU[compdump_meta]} ) ; do
|
||||
s=$(__z_compdump_print "$i")
|
||||
command grep -Fx -e "$s" "${ZSHU[f_compdump]}" &>/dev/null && continue
|
||||
__z_compdump_invalidate
|
||||
break
|
||||
done
|
||||
}
|
||||
|
||||
__z_compdump_finalize() {
|
||||
local i
|
||||
if (( ${+ZSHU[compdump_refresh]} )) ; then
|
||||
{
|
||||
echo
|
||||
for i ( ${(s: :)ZSHU[compdump_meta]} ) ; do
|
||||
__z_compdump_print "$i"
|
||||
done
|
||||
} | tee -a "${ZSHU[f_compdump]}" &>/dev/null
|
||||
unset "ZSHU[compdump_refresh]"
|
||||
fi
|
||||
unset "ZSHU[compdump_meta]"
|
||||
}
|
||||
|
||||
__z_comp_bash() {
|
||||
(( ${+commands[$1]} )) || return 1
|
||||
(( ${+_comps[$1]} )) && return 2
|
||||
(( ${+ZSHU[compdump_bash]} )) || return 3
|
||||
(( ${+2} )) && return 0
|
||||
local f p
|
||||
f=0
|
||||
for p ( /usr/share/bash-completion/completions ) ; do
|
||||
[ -s "$p/$1" ] && f=1 && break
|
||||
[ -s "$p/_$1" ] && f=1 && break
|
||||
done
|
||||
[ "$f" = 0 ] && return 1
|
||||
complete -C "$1" "$1"
|
||||
return 0
|
||||
}
|
||||
|
||||
__z_comp_test() {
|
||||
(( ${+commands[$1]} )) || return 1
|
||||
(( ${+_comps[$1]} )) && return 2
|
||||
[ -s "${ZSHU[d_compcache]}/_$1" ] && return 3
|
||||
return 0
|
||||
}
|
||||
|
||||
__z_comp_write() {
|
||||
cat > "${ZSHU[d_compcache]}/_$1"
|
||||
return 0
|
||||
}
|
14
.config/zsh/lib/curl.zsh
Normal file
14
.config/zsh/lib/curl.zsh
Normal file
@@ -0,0 +1,14 @@
|
||||
#!/bin/zsh
|
||||
|
||||
__z_curl_headers() {
|
||||
command curl -qsI "$@" 2>/dev/null
|
||||
}
|
||||
__z_curl_location() {
|
||||
__z_curl_headers "$1" \
|
||||
| sed -En '/^[Ll]ocation: (.+)$/{s//\1/;p}'
|
||||
}
|
||||
__z_curl_response() {
|
||||
__z_curl_headers -L "$1" \
|
||||
| sed -En '/^HTTP\/[0-9.]+ ([1-5][0-9]{2})( .+)?$/{s//\1/;p}' \
|
||||
| tail -n 1
|
||||
}
|
63
.config/zsh/lib/enclave.zsh.wip
Normal file
63
.config/zsh/lib/enclave.zsh.wip
Normal file
@@ -0,0 +1,63 @@
|
||||
#!/bin/zsh
|
||||
|
||||
if autoload -Uz add-zsh-hook ; then
|
||||
|
||||
ZSHU[f_enclave]="${ZSHU[d_var]}/enclave"
|
||||
[ -f "${ZSHU[f_enclave]}" ] || touch "${ZSHU[f_enclave]}"
|
||||
[ -s "${ZSHU[f_enclave]}" ] && . "${ZSHU[f_enclave]}"
|
||||
|
||||
## <test data>
|
||||
test_enclave_enter() { echo "you are in $1 enclave" 1>&2 ; }
|
||||
test_enclave_leave() { echo "you are left $1 enclave" 1>&2 ; }
|
||||
|
||||
zstyle ":enclave:box1:path:${HOME}/prj/*" ''
|
||||
zstyle ":enclave:box1:path:${HOME}/prj/_stale/*" exclude
|
||||
zstyle ':enclave:box1:env:unset' 'var2'
|
||||
zstyle ':enclave:box1:env:set' 'var1' 1
|
||||
zstyle ':enclave:box1:on_enter' test_enclave_enter
|
||||
zstyle ':enclave:box1:on_leave' test_enclave_leave
|
||||
|
||||
zstyle ":enclave:box2:path:${HOME}/doc/*" ''
|
||||
zstyle ":enclave:box2:path:${HOME}/doc/work/*" exclude
|
||||
zstyle ':enclave:box2:env:unset' 'var1'
|
||||
zstyle ':enclave:box2:env:set' 'var2' 1
|
||||
zstyle ':enclave:box2:on_enter' test_enclave_enter
|
||||
zstyle ':enclave:box2:on_leave' test_enclave_leave
|
||||
|
||||
typeset -g var1=0
|
||||
typeset -g var2=0
|
||||
## </test data>
|
||||
|
||||
__z_enclave_list() {
|
||||
local -aU list=( $(zstyle -L ':enclave:*' \
|
||||
| sed -En "/^zstyle '?:enclave:([^:]+):.*\$/{s//\1/;p;}") )
|
||||
printf '%s' "${(j: :)list}"
|
||||
}
|
||||
|
||||
__z_chpwd_enclave() {
|
||||
[ -z "${PWD}" ] && return
|
||||
## enclave changed? if no - do nothing
|
||||
[ -z "${OLDPWD}" ] && return
|
||||
[ "${PWD}" = "${OLDPWD}" ] && return
|
||||
|
||||
local a
|
||||
zstyle -s ":enclave:*:path:$PWD" '' a && \
|
||||
declare -p a
|
||||
|
||||
## call on_leave()
|
||||
## unset previously set
|
||||
## set previously set
|
||||
|
||||
env > /tmp/env
|
||||
}
|
||||
|
||||
# zstyle -L ':enclave:*'
|
||||
# echo
|
||||
|
||||
|
||||
add-zsh-hook chpwd __z_chpwd_enclave
|
||||
__z_chpwd_enclave
|
||||
|
||||
else
|
||||
echo "enclaves are disabled due to missing hook support" 1>&2
|
||||
fi
|
54
.config/zsh/lib/git.zsh
Normal file
54
.config/zsh/lib/git.zsh
Normal file
@@ -0,0 +1,54 @@
|
||||
#!/bin/zsh
|
||||
|
||||
## fancy and manageable PS1 for git
|
||||
typeset -gA ZSHU_PS
|
||||
ZSHU_PS[git]=0
|
||||
|
||||
_zshu_git_avail() { (( $+commands[git] )) ; }
|
||||
|
||||
_zshu_git() { GIT_OPTIONAL_LOCKS=0 command git "$@"; }
|
||||
|
||||
_zshu_git_is_repo() { _zshu_git rev-parse --git-dir &>/dev/null ; }
|
||||
|
||||
zshu-git-test() {
|
||||
[ "${ZSHU_PS[git]}" = '1' ] || return 1
|
||||
|
||||
_zshu_git_avail || return 2
|
||||
|
||||
_zshu_git_is_repo || return 3
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
_zshu_git_pwd() {
|
||||
zshu-git-test || return
|
||||
local p=${(%):-%~}
|
||||
[[ "$p" =~ '/.+' ]] || return
|
||||
local s pfx last
|
||||
s=$(_zshu_git rev-parse --show-prefix)
|
||||
s="${s%%/}"
|
||||
if [ -n "$s" ] ; then
|
||||
p=${p%%/$s}
|
||||
last="${s:t}"
|
||||
pfx="${s%${last}}"
|
||||
pfx="${pfx%/}"
|
||||
pfx="/${pfx}${pfx:+/}"
|
||||
else
|
||||
last="/"
|
||||
fi
|
||||
ZSHU_PS[pwd]="%F{magenta}$p%F{cyan}${pfx}%B${last}%f%b"
|
||||
}
|
||||
|
||||
zshu-git-enable() { ZSHU_PS[git]=1 ; }
|
||||
zshu-git-disable() { ZSHU_PS[git]=0 ; }
|
||||
|
||||
zshu-git-status() {
|
||||
_zshu_git_avail
|
||||
echo "Git binary: "${(%):-%(?..NOT )}"found in PATH"
|
||||
[ "${ZSHU_PS[git]}" = 1 ]
|
||||
echo "Git prompt: "${(%):-%(?.enabled.disabled)}
|
||||
_zshu_git_is_repo
|
||||
echo "Git repo: "${(%):-%(?..NOT )}"present"
|
||||
}
|
||||
|
||||
ZSHU[pwd_hook]="${ZSHU[pwd_hook]}${ZSHU[pwd_hook]:+ }_zshu_git_pwd"
|
11
.config/zsh/lib/history.zsh
Normal file
11
.config/zsh/lib/history.zsh
Normal file
@@ -0,0 +1,11 @@
|
||||
#!/bin/zsh
|
||||
|
||||
z-history() {
|
||||
local list
|
||||
zparseopts -E l=list
|
||||
if [[ -n "$list" ]]; then
|
||||
builtin fc "$@"
|
||||
else
|
||||
[[ ${@[-1]-} = *[0-9]* ]] && builtin fc -il "$@" || builtin fc -il "$@" 1
|
||||
fi
|
||||
}
|
44
.config/zsh/lib/prompt.zsh
Normal file
44
.config/zsh/lib/prompt.zsh
Normal file
@@ -0,0 +1,44 @@
|
||||
#!/bin/zsh
|
||||
|
||||
typeset -gA ZSHU_PM ZSHU_PS
|
||||
|
||||
ZSHU_PM[rst]='%b%k%u%s%f'
|
||||
ZSHU_PM[crlf]=$'\n'
|
||||
|
||||
ZSHU_PM[status]='▪'
|
||||
ZSHU_PS[lastcmd]="%B%(?.%F{green}.%F{red})${ZSHU_PM[status]}%f%b"
|
||||
|
||||
ZSHU_PS[pwd_std]='%F{cyan}%B%~%f%b'
|
||||
|
||||
ZSHU_PM[cmd_user]='%K{white}$'
|
||||
ZSHU_PM[cmd_root]='%K{red}#'
|
||||
ZSHU_PS[cmd]="%F{black}%(!.${ZSHU_PM[cmd_root]}.${ZSHU_PM[cmd_user]})${ZSHU_PM[rst]} "
|
||||
|
||||
ZSHU_PM[user]='%(!.%F{red}.%F{green})%n%f'
|
||||
ZSHU_PM[host]="%B%F{blue}${ZSHU[host]}%f%b"
|
||||
|
||||
if autoload -Uz add-zsh-hook ; then
|
||||
|
||||
__z_pwd() {
|
||||
local p=${(%):-%~}
|
||||
[[ "$p" =~ '/.+' ]] || return
|
||||
local pfx="${p:h}"
|
||||
pfx="${pfx%%/}"
|
||||
local last="${p:t}"
|
||||
ZSHU_PS[pwd]="%F{cyan}${pfx}/%B${last}%f%b"
|
||||
}
|
||||
|
||||
# ZSHU[pwd_hook]=''
|
||||
__z_pwd_hook() {
|
||||
unset "ZSHU_PS[pwd]"
|
||||
for i ( ${(s: :)ZSHU[pwd_hook]} __z_pwd ) ; do
|
||||
"$i"
|
||||
(( ${+ZSHU_PS[pwd]} )) && return
|
||||
done
|
||||
}
|
||||
|
||||
add-zsh-hook precmd __z_pwd_hook
|
||||
|
||||
else
|
||||
echo "shiny pwd's are disabled due to missing hook support" 1>&2
|
||||
fi
|
42
.config/zsh/lib/pswalk.zsh
Normal file
42
.config/zsh/lib/pswalk.zsh
Normal file
@@ -0,0 +1,42 @@
|
||||
#!/bin/zsh
|
||||
|
||||
typeset -Uga ZSHU_PARENTS_PID
|
||||
typeset -ga ZSHU_PARENTS_NAME
|
||||
|
||||
function {
|
||||
local i cmd
|
||||
|
||||
i=$$ ; while : ; do
|
||||
i=$(ps -o ppid= -p $i 2>/dev/null || : )
|
||||
i=${i//[^0-9]}
|
||||
[[ "$i" =~ '^[1-9][0-9]*$' ]] || break
|
||||
ZSHU_PARENTS_PID+=( $i )
|
||||
done
|
||||
|
||||
for i ( ${ZSHU_PARENTS_PID} ) ; do
|
||||
cmd=$(ps -o comm= -p $i 2>/dev/null || : )
|
||||
[ -n "${cmd}" ] && ZSHU_PARENTS_NAME+=( "${cmd##*/}" )
|
||||
done
|
||||
|
||||
typeset -r ZSHU_PARENTS_PID
|
||||
typeset -r ZSHU_PARENTS_NAME
|
||||
}
|
||||
|
||||
typeset -gA ZSHU_RUN
|
||||
|
||||
z-run-test() {
|
||||
local key i
|
||||
key=$1 ; shift
|
||||
v=0
|
||||
for i ( ${ZSHU_PARENTS_NAME} ) ; do
|
||||
if (( ${+argv[(r)$i]} )) ; then
|
||||
ZSHU_RUN[${key}]=1
|
||||
return
|
||||
fi
|
||||
done
|
||||
ZSHU_RUN[${key}]=0
|
||||
}
|
||||
|
||||
z-run-test gui konsole xterm x-terminal-emulator
|
||||
z-run-test nested screen tmux
|
||||
z-run-test elevated sudo su
|
14
.config/zsh/lib/selfservice.zsh
Normal file
14
.config/zsh/lib/selfservice.zsh
Normal file
@@ -0,0 +1,14 @@
|
||||
#!/bin/zsh
|
||||
|
||||
dotfiles-update() {
|
||||
"${ZSHU[d_zdot]}/.config/dotfiles/install.sh"
|
||||
}
|
||||
|
||||
z-update() {
|
||||
dotfiles-update
|
||||
}
|
||||
|
||||
z-reload() {
|
||||
exec -a "${ZSH_ARGZERO}" "${ZSH_NAME}" "${argv[@]}"
|
||||
echo "unable to reload (something went wrong), code $?" 1>&2
|
||||
}
|
205
.config/zsh/lib/starship.zsh.sample
Normal file
205
.config/zsh/lib/starship.zsh.sample
Normal file
@@ -0,0 +1,205 @@
|
||||
#!/bin/zsh
|
||||
|
||||
## inspired by 'https://starship.rs/install.sh' as of 2021-03-07
|
||||
|
||||
ZSHU[starship_baseurl]='https://github.com/starship/starship/releases'
|
||||
## ZSHU[starship_target] is auto-detected
|
||||
## ZSHU[starship_path] defaults to ZSHU[d_bin] which is in PATH already
|
||||
|
||||
# export STARSHIP_CONFIG="$HOME/.config/starship.toml"
|
||||
# export STARSHIP_CACHE="$HOME/.cache/starship"
|
||||
|
||||
__z_starship_auto_path() {
|
||||
echo "${ZSHU[starship_path]:-${ZSHU[d_bin]}}"
|
||||
}
|
||||
|
||||
__z_starship() {
|
||||
local x=$(__z_starship_auto_path)
|
||||
x="$x/starship"
|
||||
[ -x "$x" ] || x=starship
|
||||
[ -x "$x" ] || return 127
|
||||
"$x" "$@"
|
||||
}
|
||||
|
||||
__z_starship_test() { __z_starship -V &>/dev/null ; }
|
||||
|
||||
## NB: supply TARGET environment variable to call
|
||||
__z_starship_url_latest() {
|
||||
printf '%s/latest/download/starship-%s.tar.gz' \
|
||||
"${ZSHU[starship_baseurl]}" "${TARGET}"
|
||||
}
|
||||
|
||||
## NB: supply TARGET environment variable to call
|
||||
## $1 - version (semver like '0.50.0')
|
||||
__z_starship_url_versioned() {
|
||||
printf '%s/download/v%s/starship-%s.tar.gz' \
|
||||
"${ZSHU[starship_baseurl]}" "$1" "${TARGET}"
|
||||
}
|
||||
|
||||
## NB: install starship somewhere in PATH ;)
|
||||
__z_starship_ver_installed() {
|
||||
__z_starship -V 2>/dev/null \
|
||||
| sed -En '/^starship v?(\S.+)$/{s//\1/;p;}'
|
||||
}
|
||||
|
||||
## NB: supply TARGET environment variable to call
|
||||
__z_starship_ver_latest() {
|
||||
local x=$(__z_starship_url_latest)
|
||||
local y=$(__z_curl_location "$x")
|
||||
## hackish strip, e.g.:
|
||||
## from: https://github.com/starship/starship/releases/download/v0.50.0/starship-x86_64-unknown-linux-musl.tar.gz
|
||||
## to: v0.50.0
|
||||
y=${y:h:t}
|
||||
[ "${y:0:1}" = 'v' ] && y=${y:1}
|
||||
echo "$y"
|
||||
}
|
||||
|
||||
__z_starship_detect_arch() {
|
||||
local arch=${ZSHU[mach]}
|
||||
case "${arch}" in
|
||||
x86_64) [ "$(getconf LONG_BIT)" -eq 32 ] && arch=i686 ;;
|
||||
aarch64) [ "$(getconf LONG_BIT)" -eq 32 ] && arch=arm ;;
|
||||
esac
|
||||
echo "${arch}"
|
||||
}
|
||||
|
||||
__z_starship_detect_platform() {
|
||||
local platform=${ZSHU[uname]}
|
||||
case "${ZSHU[uname]}" in
|
||||
msys_nt*) platform=pc-windows-msvc ;;
|
||||
cygwin_nt*) platform=pc-windows-msvc ;;
|
||||
mingw*) platform=pc-windows-msvc ;;
|
||||
linux) platform=unknown-linux-musl ;; ## static builds
|
||||
darwin) platform=apple-darwin ;;
|
||||
freebsd) platform=unknown-freebsd ;;
|
||||
esac
|
||||
echo "${platform}"
|
||||
}
|
||||
|
||||
## $1 - arch
|
||||
## $2 - platform
|
||||
__z_starship_detect_target() {
|
||||
local target="$1-$2"
|
||||
case "${target}" in
|
||||
arm-unknown-linux-musl) target="${target}eabihf" ;;
|
||||
esac
|
||||
echo "${target}"
|
||||
}
|
||||
|
||||
__z_starship_auto_target() {
|
||||
[ -n "${ZSHU[starship_target]}" ] && echo "${ZSHU[starship_target]}" && return
|
||||
local arch=$(__z_starship_detect_arch)
|
||||
local platform=$(__z_starship_detect_platform)
|
||||
local target=$(__z_starship_detect_target "${arch}" "${platform}")
|
||||
echo "${target}"
|
||||
}
|
||||
|
||||
__z_starship_install() {
|
||||
local ver=${1:-latest}
|
||||
local target url resp
|
||||
target=$(__z_starship_auto_target)
|
||||
if [ "${ver}" = 'latest' ] ; then
|
||||
url=$(TARGET=${target} __z_starship_url_latest)
|
||||
resp=$(__z_curl_response "${url}")
|
||||
resp=${resp:-400}
|
||||
[ ${resp} -ge 400 ] && return 1
|
||||
else
|
||||
url=$(TARGET=${target} __z_starship_url_versioned "${ver}")
|
||||
resp=$(__z_curl_response "${url}")
|
||||
resp=${resp:-400}
|
||||
if [ ${resp} -ge 400 ] ; then
|
||||
## last resort: try messing with version ;D
|
||||
if [ "${ver:0:1}" = 'v' ] ; then
|
||||
ver=${ver:1}
|
||||
else
|
||||
ver="v${ver}"
|
||||
fi
|
||||
url=$(TARGET=${target} __z_starship_url_versioned "${ver}")
|
||||
resp=$(__z_curl_response "${url}")
|
||||
resp=${resp:-400}
|
||||
[ ${resp} -ge 400 ] && return 1
|
||||
fi
|
||||
fi
|
||||
|
||||
local t=$(mktemp -d)
|
||||
local f="$t/starship.tar.gz"
|
||||
command curl -sqL "${url}" > "$f"
|
||||
command tar -C "$t" -xf "$f" starship &>/dev/null
|
||||
if [ $? -ne 0 ] ; then
|
||||
## last resort
|
||||
command tar -C "$t" --strip-components=1 --wildcards -xf "$f" '*/starship' &>/dev/null
|
||||
if [ $? -ne 0 ] ; then
|
||||
rm -rf "$t"
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
local d=$(__z_starship_auto_path)
|
||||
mv "$t/starship" "$d/"
|
||||
local r=$?
|
||||
if [ $r -eq 0 ] ; then
|
||||
[ "${ver:0:1}" = 'v' ] && ver=${ver:1}
|
||||
echo "starship: installed ${ver} version in $d/" 1>&2
|
||||
fi
|
||||
rm -rf "$t"
|
||||
return $r
|
||||
}
|
||||
|
||||
z-starship-target-available() {
|
||||
local target url resp
|
||||
target=$(__z_starship_auto_target)
|
||||
url=$(TARGET=${target} __z_starship_url_latest)
|
||||
resp=$(__z_curl_response "${url}")
|
||||
resp=${resp:-400}
|
||||
if [ ${resp} -lt 400 ] ; then
|
||||
echo "starship: available for ${target}" 1>&2
|
||||
return 0
|
||||
else
|
||||
echo "starship: NOT available for ${target}" 1>&2
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
z-starship-update-available() {
|
||||
local target=$(__z_starship_auto_target)
|
||||
local installed=$(__z_starship_ver_installed)
|
||||
local latest=$(TARGET=${target} __z_starship_ver_latest)
|
||||
if [ -z "${latest}" ] ; then
|
||||
echo "starship: update is NOT available" 1>&2
|
||||
return 1
|
||||
fi
|
||||
if [ -z "${installed}" ] ; then
|
||||
echo "starship: NOT installed, install it 1st" 1>&2
|
||||
return 0
|
||||
fi
|
||||
local tailver=$(printf '%s\n' "${installed}" "${latest}" | sort -Vu | tail -n 1)
|
||||
if [ "${installed}" = "${tailver}" ] ; then
|
||||
if [ "${installed}" = "${latest}" ] ; then
|
||||
echo "starship: local version is up to date" 1>&2
|
||||
else
|
||||
echo "starship: local version is newer! o_O" 1>&2
|
||||
fi
|
||||
return 1
|
||||
else
|
||||
echo "starship: update is available (${installed} -> ${latest})" 1>&2
|
||||
return 0
|
||||
fi
|
||||
}
|
||||
|
||||
z-starship-init() {
|
||||
[ -n "${ZSHU[starship_init]}" ] && return
|
||||
__z_starship_test || return
|
||||
eval "$(__z_starship init zsh)"
|
||||
ZSHU[starship_init]=1
|
||||
}
|
||||
|
||||
z-starship-install() {
|
||||
z-starship-target-available || return
|
||||
__z_starship_install || \
|
||||
echo "starship: unable to install" 1>&2
|
||||
}
|
||||
|
||||
z-starship-update() {
|
||||
z-starship-update-available || return 0
|
||||
__z_starship_install || \
|
||||
echo "starship: unable to update" 1>&2
|
||||
}
|
42
.config/zsh/lib/time.zsh
Normal file
42
.config/zsh/lib/time.zsh
Normal file
@@ -0,0 +1,42 @@
|
||||
#!/bin/zsh
|
||||
|
||||
zmodload -i zsh/datetime
|
||||
zmodload -i zsh/mathfunc
|
||||
|
||||
z-ts-to-human() {
|
||||
local t=$1
|
||||
local s=$(( int(t) ))
|
||||
local ns=$(( int((t - s) * (10**9)) ))
|
||||
t=$s
|
||||
|
||||
local d=0 h=0 m=0
|
||||
if [ $t -ge 86400 ] ; then
|
||||
d=$(( t / 86400 ))
|
||||
t=$(( t % 86400 ))
|
||||
fi
|
||||
if [ $t -ge 3600 ] ; then
|
||||
h=$(( t / 3600 ))
|
||||
t=$(( t % 3600 ))
|
||||
fi
|
||||
if [ $t -ge 60 ] ; then
|
||||
m=$(( t / 60 ))
|
||||
t=$(( t % 60 ))
|
||||
fi
|
||||
|
||||
local f='%s.%6.'
|
||||
f=$(strftime "$f" $t $ns)
|
||||
|
||||
local x=3
|
||||
## keep math in sync with format above
|
||||
case "$2" in
|
||||
0) x=7 ;;
|
||||
[1-5]) x=$(( 6 - $2 )) ;;
|
||||
6) x=0 ;;
|
||||
esac
|
||||
[ $x -gt 0 ] && f="${f:0:-$x}s"
|
||||
|
||||
[ $s -ge 60 ] && f="${m}m:$f"
|
||||
[ $s -ge 3600 ] && f="${h}h:$f"
|
||||
[ $s -ge 86400 ] && f="${d}d:$f"
|
||||
echo "$f"
|
||||
}
|
Reference in New Issue
Block a user