Hacker News new | ask | show | jobs
by paulfurtado 1648 days ago
On linux (and I think even osx), even when stdin, stdout, and stderr are redirected, you can always get at the current session's tty via "/dev/tty" unless the executing program spawns a new session which would be very unlikely in this case. This is why things like sudo can defeat pipes and prompt you.
1 comments

Oh, you are right! This works by looking at /proc/$ppid/fd/1

    format = """$all\
    ${custom.tty}"""

    [custom.tty]
    description = "tty"
    when = 'true'
    command = """
    tpid=$(ps -o pid= -p "$(($(ps -o sid= -p "$$")))")
    tpid=$(( tpid + 0 ))
    readlink /proc/$tpid/fd/1 | sed -e 's|/dev/||'
    """
    format = ' [$output]($style)'
    shell = ["bash", "--noprofile", "--norc"]
    style = 'blue'