From fe047338d08c73e7fa513777a792d78237107e71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Ko=C4=8D=C3=AD?= Date: Thu, 2 Aug 2018 13:52:42 +0200 Subject: Add inotify based inrun and insurf --- shellrc.d/common | 24 ++++++++++++++++++++++++ shellrc.d/desktop | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/shellrc.d/common b/shellrc.d/common index 6fbbfc1..8a30f3b 100644 --- a/shellrc.d/common +++ b/shellrc.d/common @@ -103,3 +103,27 @@ ssh-clear() { chroot-bash() { sudo chroot "$1" /bin/bash } + +# Run command with inotifywait +# First argument has to be files then -- is expected and everything else +# is command to be executed when file changes. +inrun () { + ( + set -e + local TMPFS="$(mktemp --tmpdir inrun.XXXXXXXX)" + trap "rm '$TMPFS'; trap '' EXIT; exit 0" EXIT INT QUIT TERM ABRT + while [ $# -gt 0 -a "$1" != "--" ]; do + echo "$1" >> "$TMPFS" + shift + done + if [ $# -le 1 ]; then + echo "Usage: inrun FILE.. -- COMMAND" + return 1 + fi + shift + while true; do + inotifywait -qe close_write --fromfile "$TMPFS" || true + "$@" || true + done + ) +} diff --git a/shellrc.d/desktop b/shellrc.d/desktop index cc1a3b6..f155eed 100644 --- a/shellrc.d/desktop +++ b/shellrc.d/desktop @@ -3,3 +3,44 @@ alias mutt='neomutt' alias sdcv='sdcv -c' + +# Following section is applicable for any desktop but only for graphics########## +# TODO add check for wayland +[ -z "$DISPLAY" ] && return + +# This function should not be called externaly +# It expects PID of surf instace as first argument and all other arguments should +# be command to be called before kill is sent. +__insurf_callback() { + local SPID=$1 + shift + "$@" + echo kill $SPID SIGHUP + kill -s SIGHUP $SPID || exit 1 +} + +# Same as inrun but it opens first argument it founds in surf and then reloads +# that instance automatically. +insurf() { + ( + set -e + # Run surf + surf "$1" & + local SPID=$! + trap "kill $SPID; trap '' EXIT; exit 0" EXIT INT QUIT TERM ABRT + # Insert our callback + local ISFIRST=true + for ARG in "$@"; do + if $ISFIRST; then + shift $# + ISFIRST=false + fi + set "$@" "$ARG" + if [ "$ARG" = "--" ]; then + set "$@" "__insurf_callback" "$SPID" + fi + done + # Run inrun + inrun "$@" + ) +} -- cgit v1.2.3