in reply to Re: Failing to get current TTY's rows & columns...
in thread Failing to get current TTY's rows & columns...

So the answer here is that I have to open "/dev/tty" each time I call it rather than reference my already-open "STDOUT"?

Hmmm....I'm disappointed that I need to open another file handle and can't get the rows & columns associated with my already open FH, "STDOUT", directly...

As for using 'Term::ReadKey', I thought of using something along those lines (or a Curses func), but wanted something that was minimal overhead, as this was called every loop, and portability isn't a major consideration, considering the nature of the program this is being used in is showing linux process and mounted-dev I/O.

If I look at /proc/$$/fd/1, I see a softlink to /dev/pts/1 (i.e. a character, tty type device). Indeed, if I use your 2nd prog and substitute in "/dev/pts/1" instead of "/dev/tty", I get the same result (ie. it works).

Shouldn't STDIN, STDOUT map to fd[0], fd1 => /dev/pts/1 on some level?

I.e. Shouldn't I be able to use some mapping function on STDOUT to get a file descriptor that's suitable for ioctl?

  • Comment on Re^2: Failing to get current TTY's rows & columns...

Replies are listed 'Best First'.
Re^3: Failing to get current TTY's rows & columns...
by Anonymous Monk on Apr 14, 2011 at 08:13 UTC
    So the answer here is that I have to open "/dev/tty" each time I call it rather than reference my already-open "STDOUT"?

    I don't have linux, but no, I don't think that is the answer.

    I think part of the answer is to do error checking correctly like the faq entry

    I also think using S4 is part of the answer, not being an expert on pack/unpack and not having a /dev/tty, I can't run any tests.

    Hmmm....I'm disappointed that I need to open another file handle and can't get the rows & columns associated with my already open FH, "STDOUT", directly...

    Sounds premature if you ask me, avoiding abstractions always involves some growing pains :)

    Shouldn't STDIN, STDOUT map to fd[0], fd[1] => /dev/pts/1 on some level?

    They do

    $ perl -le " print fileno($_) for STDIN, STDOUT, STDERR " 0 1 2
    See, fd 0,1,2. Now regarding tty, using operator -t to test if filehandle is opened to a tty, you can see 0,1,2 are connected to a tty
    $ perl -le " print 0+-t $_ for STDIN, STDOUT, STDERR " 1 1 1
    but not when you do redirection, here is STDIN not connected to a tty
    $ perl -le " print 0+-t $_ for STDIN, STDOUT, STDERR " < NUL 0 1 1
    here is STDIN and STDERR not connected to a tty
    $ perl -le " print 0+-t $_ for STDIN, STDOUT, STDERR " < NUL 2> NUL 0 1 0
    here is STDOUT not connected to a tty
    $ perl -le " print 0+-t $_ for STDIN, STDOUT, STDERR " > out.txt $ cat out.txt 1 0 1
    I.e. Shouldn't I be able to use some mapping function on STDOUT to get a file descriptor that's suitable for ioctl?

    ioctl says you don't need to, the faq item doesn't, and neither does Term::Size::Any

      I think part of the answer is to do error checking correctly like the faq entry
      See my answer to previous poster -- that's a red herring as well as the unpack format.

      What you are missing is:

      "I.e. Shouldn't I be able to use some mapping function on STDOUT to get a file descriptor that's suitable for ioctl?
      ioctl says you don't need to, the faq item doesn't, and neither does Term::Size::Any
      The faq item doesn't use STDOUT. It doesn't show how one would use STDOUT to determine the terminal's size. It opens '/dev/tty' -- ANOTHER file handle -- it doesn't show how to determine the size of the terminal attached to STDOUT.

      Of note -- if STDOUT was redirected to a file, then trying to determine its size woudn't return valid values (presumably the ioctl would return an error).

      So how would you determine the rows/columns of whatever STDOUT is pointing to (without opening another character device like "/dev/tty")?

      Even if you aren't on linux, presumably there's a way to do it on your platform, no? I.e. if you have a 'tty' window, how would you determine the size of the window addressed by 'STDOUT'?

        See my answer to previous poster -- that's a red herring as well as the unpack format.

        Same guy, and no, the red herring is you're confusing c function ioctl with perl function ioctl

        What you are missing is:

        No, I didn't miss it. If you redirect STDIN/STDOUT/STDERR, then you need to give ioctl a handle which is connected to tty, so you open your own to /dev/tty, and it works regardless of the state of STDIN/STDOUT/STDERR

        Even if you aren't on linux, presumably there's a way to do it on your platform, no? I.e. if you have a 'tty' window, how would you determine the size of the window addressed by 'STDOUT'?

        I would use an abstraction like Term::Size::Any, I wouldn't mess around with ioctl

        But in all honesty, unless I'm writing some kind of curses program, I would never need to know the size of the terminal