zbell.plugin.zsh
2814 bytes
1#!/usr/bin/env zsh
2
3# This script prints a bell character when a command finishes
4# if it has been running for longer than $zbell_duration seconds.
5# If there are programs that you know run long that you don't
6# want to bell after, then add them to $zbell_ignore.
7#
8# This script uses only zsh builtins so its fast, there's no needless
9# forking, and its only dependency is zsh and its standard modules
10#
11# Written by Jean-Philippe Ouellet <jpo@vt.edu>
12# Made available under the ISC license.
13
14# only do this if we're in an interactive shell
15[[ -o interactive ]] || return
16
17# get $EPOCHSECONDS. builtins are faster than date(1)
18zmodload zsh/datetime || return
19
20# make sure we can register hooks
21autoload -Uz add-zsh-hook || return
22
23# make sure we can do regexp replace
24autoload -Uz regexp-replace || return
25
26# initialize zbell_duration if not set
27(( ${+zbell_duration} )) || zbell_duration=15
28
29# initialize zbell_ignore if not set
30(( ${+zbell_ignore} )) || zbell_ignore=($EDITOR $PAGER)
31
32# initialize zbell_use_notify_send if not set
33(( ${+zbell_use_notify_send} )) || zbell_use_notify_send=true
34
35# initialize it because otherwise we compare a date and an empty string
36# the first time we see the prompt. it's fine to have lastcmd empty on the
37# initial run because it evaluates to an empty string, and splitting an
38# empty string just results in an empty array.
39zbell_timestamp=$EPOCHSECONDS
40
41# UI notification function
42# $1: command
43# $2: duration in seconds
44zbell_ui_notify() {
45 [[ $zbell_use_notify_send != "true" ]] && return
46
47 if type notify-send > /dev/null; then
48 notify-send -i terminal "Command completed in ${2}s:" $1
49 fi
50}
51
52# default notification function
53# $1: command
54# $2: duration in seconds
55zbell_notify() {
56 zbell_ui_notify "${@}"
57 print -n "\a"
58}
59
60# right before we begin to execute something, store the time it started at
61zbell_begin() {
62 zbell_timestamp=$EPOCHSECONDS
63 zbell_lastcmd=$1
64}
65
66# when it finishes, if it's been running longer than $zbell_duration,
67# and we dont have an ignored command in the line, then print a bell.
68zbell_end() {
69 local cmd_duration=$(( $EPOCHSECONDS - $zbell_timestamp ))
70 local ran_long=$(( $cmd_duration >= $zbell_duration ))
71
72 local zbell_lastcmd_tmp="$zbell_lastcmd"
73 regexp-replace zbell_lastcmd_tmp '^sudo ' ''
74
75 [[ $zbell_last_timestamp == $zbell_timestamp ]] && return
76
77 [[ $zbell_lastcmd_tmp == "" ]] && return
78
79 zbell_last_timestamp=$zbell_timestamp
80
81 local has_ignored_cmd=0
82 for cmd in ${(s:;:)zbell_lastcmd_tmp//|/;}; do
83 words=(${(z)cmd})
84 util=${words[1]}
85 if (( ${zbell_ignore[(i)$util]} <= ${#zbell_ignore} )); then
86 has_ignored_cmd=1
87 break
88 fi
89 done
90
91 (( ! $has_ignored_cmd && ran_long )) && zbell_notify $zbell_lastcmd $cmd_duration
92}
93
94# register the functions as hooks
95add-zsh-hook preexec zbell_begin
96add-zsh-hook precmd zbell_end