Skip to content

Instantly share code, notes, and snippets.

@raghavauppuluri13
Created October 13, 2025 23:59
Show Gist options
  • Select an option

  • Save raghavauppuluri13/e67927d54d62f02e9f6bea12c7bec17b to your computer and use it in GitHub Desktop.

Select an option

Save raghavauppuluri13/e67927d54d62f02e9f6bea12c7bec17b to your computer and use it in GitHub Desktop.

Revisions

  1. raghavauppuluri13 created this gist Oct 13, 2025.
    76 changes: 76 additions & 0 deletions enter-tio.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,76 @@
    #!/usr/bin/env bash
    # tio-diff-plug: prompt user to unplug, then wait for a new USB serial TTY and run tio on it.
    set -euo pipefail

    # --- Check tio availability ---
    if ! command -v tio >/dev/null 2>&1; then
    echo "[!] 'tio' not found."
    if command -v brew >/dev/null 2>&1; then
    echo "[i] Installing via Homebrew..."
    brew install tio
    else
    echo "[!] Homebrew not found; please install 'tio' manually:"
    echo " https://tio.github.io/ or use your package manager."
    exit 1
    fi
    fi

    # Make unmatched globs expand to nothing
    shopt -s nullglob 2>/dev/null || true

    PATTERNS=(
    "/dev/tty.usbmodem*"
    "/dev/ttyACM*"
    "/dev/ttyUSB*"
    )

    list_ttys() {
    local cs=() p f
    for p in "${PATTERNS[@]}"; do
    for f in $p; do [[ -e "$f" ]] && cs+=("$f"); done
    done
    ((${#cs[@]})) && printf '%s\n' "${cs[@]}" | sort -u || :
    }

    to_tmpfile() {
    local content="${1-}" f
    f="$(mktemp)"
    printf "%s\n" "$content" >"$f"
    echo "$f"
    }

    echo "[tio-diff] Please unplug your USB device(s) now."
    echo "[tio-diff] Press ENTER once you’ve confirmed they’re unplugged..."
    read -r _

    echo "[tio-diff] Capturing baseline (no devices connected)…"
    BASE="$(list_ttys || true)"
    BASE_F="$(to_tmpfile "${BASE-}")"

    use_udev=0
    if [[ "$(uname -s)" == "Linux" ]] && command -v udevadm >/dev/null 2>&1; then
    use_udev=1
    fi

    echo "[tio-diff] Now plug in your device. Waiting for it to appear…"
    while :; do
    if (( use_udev )); then
    udevadm monitor --udev --subsystem-match=tty --property --kernel --environment \
    | awk '/(add|remove)/ { exit }' >/dev/null 2>&1 || true
    else
    sleep 0.4
    fi

    NOW="$(list_ttys || true)"
    NOW_F="$(to_tmpfile "${NOW-}")"
    NEW="$(comm -13 "$BASE_F" "$NOW_F" || true)"
    rm -f "$NOW_F"

    if [[ -n "${NEW-}" ]]; then
    pick="$(ls -t -- $NEW 2>/dev/null | head -n1 || true)"
    if [[ -n "$pick" && -e "$pick" ]]; then
    echo "[tio-diff] New device: $pick"
    exec tio "$pick" "$@"
    fi
    fi
    done