• # Intégré à mon prompt

    Posté par . En réponse au journal De l'art de la notification. Évalué à 6. Dernière modification le 24 juillet 2012 à 23:33.

    Et toi cher codeur quel est ta technique pour être notifié de la fin d'une exécution ?

    C ́est intégré à la commande qui gère mon prompt :

    # NOTIF_MIN_RUN: notify command termination if command ran longer than this (sec)
    # NOTIF_OK_ICON: icon to display if $? == 0
    # NOTIF_KO_ICON: icon to display if $? != 0
    # NOTIF_TIMEOUT: delay to display notifications (sec)
    # NOTIF_PRIO : notification priority
    # NOTIF_CMDS : array of grep-expressions of commands to notify for
    NOTIF_MIN_RUN=10
    NOTIF_OK_ICON='/usr/share/icons/gnome/48x48/emblems/emblem-default.png'
    NOTIF_KO_ICON='/usr/share/icons/gnome/48x48/status/dialog-warning.png'
    NOTIF_TIMEOUT=10
    NOTIF_PRIO=low
    NOTIF_CMDS=(
     "\</?configure'? .*"
     "\</?q?make'? .*"
    )
    do_notify() {
     local ret="${1}"; shift
     local cmd="${*}"
     local pwd="$(pwd)"
     local msg icon
     if [ ${ret} -eq 0 ]; then
     title="Command finished successfully"
     icon="${NOTIF_OK_ICON}"
     else
     title="Command terminated in error"
     icon="${NOTIF_KO_ICON}"
     fi
     case "${pwd}" in
     "${HOME}"/?*) pwd="~${pwd#${HOME}}";;
     esac
     msg="$( printf "CWD: %s\nCMD: %s" "${pwd}" "${cmd}" )"
     notify-send -t $((1000*NOTIF_TIMEOUT)) \
     -u ${NOTIF_PRIO} \
     -i "${icon}" \
     "${title}" \
     "${msg}"
    }
    do_prompt() {
    # [...]
     # Was the last command running for long enough that it warrants a notification?
     last_run="$( history |tail -n 1 \
     |sed -r -e |sed -r -e 's/^[''[:space:]'']*[''[:digit:]'']+[''[:space:]'']+(.*)$/1円/;' \
     )"
     last_run_cmd="${last_run#* }"
     last_run_epoch="$( date -d "$( sed -r -e 's/(....)(..)(..)\.(..)(..)(..)/1円-2円-3円 4円:5円:6円/;' <<<"${last_run%% *}" )" '+%s' )"
     cur_epoch="$( date '+%s' )"
     if [ $((cur_epoch-last_run_epoch)) -ge ${NOTIF_MIN_RUN} -a ${prompt_first} -eq 0 ]; then
     for c in "${NOTIF_CMDS[@]}"; do
     if grep -E "${c}" <<<"${last_run_cmd}" >/dev/null; then
     do_notify ${RET} "${last_run_cmd}"
     break
     fi
     done
     fi
    # [...]
     prompt_first=0
    }
    export prompt_first=1
    export -f doPrompt
    export PROMPT_COMMAND=doPrompt
    
    

    Note: j ́ai été obligé de rajouter des simples quotes dans l ́expression sed, sinon ça fait des liens Wikipedia… :-(

    Le bidouillage avec history, c ́est parce que bash ne donne pas le temps d ́exécution de la dernière commande. Utiliser history n ́est pas très robuste, mais ça marchouille dans le cas droit.

    J ́ai très peu de commandes notifiées, mais en ajouter est trivial.

    Dans le futur, je vais peut-être ajouter un son (Tadaa! pour succès, Blong-patatra pour une erreur), mais je n ́aime pas trop les notifications sonores… Ceci dit, le rajouter dans le code ci-dessus sera trivial.

    Hop,
    Moi.