-
Notifications
You must be signed in to change notification settings - Fork 4
/
postexec_notify
executable file
·54 lines (49 loc) · 2.34 KB
/
postexec_notify
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/bash
# This script it based on
# https://github.com/rcaloras/bash-preexec
#
# if you want to get last version of preexec.sh
# do this:
# curl https://raw.githubusercontent.com/rcaloras/bash-preexec/master/bash-preexec.sh > preexec.sh
# ----------------------------------------------------------------------------------
# Installation:
# source this file in your ~/.bashrc
# ----------------------------------------------------------------------------------
# Get working directory. It is directory where this script is located
# http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in
# https://stackoverflow.com/a/54755784/9157799
# Is a useful one-liner which will give you the full directory name
# of the script no matter where it is being called from
WORKING_DIR="$(dirname ${BASH_SOURCE[0]:-${(%):-%x}})"
# Treshold from which we start to notify about long running commands
# this is define what is "long-running"
# by default it is 5 seconds
NOTIFICATION_SECONDS_THRESHOLD=60
NOTIFICATION_IGNORE_REGEX="^(vim|ssh|screen|jupyter|ranger)"
# check if preexec is available or already downloaded
if [[ ! -f "$WORKING_DIR"/preexec.sh ]];then
printf "preexec.sh is not exist\n"
printf "Trying to download it from github\n"
if ! $(curl -s https://raw.githubusercontent.com/rcaloras/bash-preexec/master/bash-preexec.sh > "$WORKING_DIR"/preexec.sh); then
# clean after curl
if [[ -f "$WORKING_DIR"/preexec.sh ]]; then
rm "$WORKING_DIR"/preexec.sh
fi
printf "===\nDownload failed, check internet connection or github repo https://github.com/rcaloras/bash-preexec\n===\n"
notify-send -i error "Problem with $0" "\nDownload failed, check internet connection or github repo https://github.com/rcaloras/bash-preexec\n"
return 1
fi
fi
# SECONDS bash variable
# this indicates last executed command time
# check 'man bash' for SECONDS variable definition
function preexec() { SECONDS_WHEN_LAST_COMMAND_WAS_EXECUTED=$SECONDS; LAST_EXECUTED_COMMAND=$1; }
function precmd() {
if [[ ! $LAST_EXECUTED_COMMAND =~ $NOTIFICATION_IGNORE_REGEX ]]; then
if [[ $((SECONDS - SECONDS_WHEN_LAST_COMMAND_WAS_EXECUTED)) -gt $NOTIFICATION_SECONDS_THRESHOLD ]]; then
/bin/bash "$WORKING_DIR"/play_sound
notify-send -i terminal "Execution is finished" "$LAST_EXECUTED_COMMAND"
fi
fi
}
. "$WORKING_DIR"/preexec.sh