Skip to content

Commit

Permalink
Add push function & auto notify for long-running job
Browse files Browse the repository at this point in the history
  • Loading branch information
NyaMisty committed Aug 2, 2023
1 parent bb78e27 commit eed09e0
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions zsh_conf/50-push.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# define $MISTYPUSH_CHAN in your .zshrc.local

function pushex() {
body=$1
desc=$2
channel=$3
if [ -z "$MISTYPUSH_SERVER" ]; then
return
fi
curl "${MISTYPUSH_SERVER}/send" -d "text=$body" -d "desp=$desc" -d "chan=$channel"
}

function push() {
body=$1
channel=$2

pushex $body "" $channel
}

zmodload zsh/datetime

prompt_preexec() {
prompt_preexec_cmd="$1"
#echo $prompt_preexec_cmd
prompt_prexec_realtime=${EPOCHREALTIME}
}

prompt_precmd() {
if (( prompt_prexec_realtime )); then
local -rF elapsed_realtime=$(( EPOCHREALTIME - prompt_prexec_realtime ))

if (( elapsed_realtime > 20 )) && \
! [[ "$prompt_preexec_cmd" =~ ^(htop|top|btop|watch).* ]] \
; then
local -rF s=$(( elapsed_realtime%60 ))
local -ri elapsed_s=${elapsed_realtime}
local -ri m=$(( (elapsed_s/60)%60 ))
local -ri h=$(( elapsed_s/3600 ))

if (( h > 0 )); then
printf -v prompt_elapsed_time '%ih%im' ${h} ${m}
elif (( m > 0 )); then
printf -v prompt_elapsed_time '%im%is' ${m} ${s}
elif (( s >= 10 )); then
printf -v prompt_elapsed_time '%.2fs' ${s} # 12.34s
elif (( s >= 1 )); then
printf -v prompt_elapsed_time '%.3fs' ${s} # 1.234s
else
printf -v prompt_elapsed_time '%ims' $(( s*1000 ))
fi

pushex "cmd finish in ${prompt_elapsed_time} on $(uname -n)" "cmd: ${prompt_preexec_cmd}" "$MISTYPUSH_CHAN"
fi
unset prompt_prexec_realtime
else
# Clear previous result when hitting ENTER with no command to execute
unset prompt_elapsed_time
fi
}

autoload -Uz add-zsh-hook
add-zsh-hook preexec prompt_preexec
add-zsh-hook precmd prompt_precmd

0 comments on commit eed09e0

Please sign in to comment.