diff --git a/pkgs/device-mon/default.nix b/pkgs/device-mon/default.nix new file mode 100644 index 0000000..c44a41c --- /dev/null +++ b/pkgs/device-mon/default.nix @@ -0,0 +1,7 @@ +{ writeShellApplication, libnotify }: + +writeShellApplication { + name = "device-mon"; + runtimeInputs = [ libnotify ]; + text = builtins.readFile ./main.sh; +} diff --git a/pkgs/device-mon/main.sh b/pkgs/device-mon/main.sh new file mode 100644 index 0000000..8731973 --- /dev/null +++ b/pkgs/device-mon/main.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash + +trap "echo Exited!; exit;" SIGINT SIGTERM + +OLD="$(lsusb)" +while true; do + watch --chgexit -n0.2 lsusb >& /dev/null + NEW="$(lsusb)" + + diff <(cat <<<"$OLD") <(cat <<<"$NEW") --unified | grep '^[+-]' | grep -Ev '^(---|\+\+\+)' | + while read line; do + echo "$line" + ID="$( echo "$line" | cut -c2- | cut -d' ' -f-6 )" + LABEL="$(echo "$line" | cut -c2- | cut -d' ' -f7- )" + case "$line" in + \+*) + notify-send --icon=device --expire-time=500 "New device connected" "${LABEL} ($ID)" + ;; + \-*) + notify-send --icon=device --expire-time=500 "Device disconnected" "${LABEL} ($ID)" + ;; + esac + done + + OLD="$NEW" +done