Skip to content

Surprising behavior of getTerminalSize #178

@Bodigrim

Description

@Bodigrim

The documentation for getTerminalSize says

-- There is no \'ANSI\' control character sequence that reports the terminal
-- size. So, it attempts to set the cursor position beyond the bottom right
-- corner of the terminal and then use 'getCursorPosition' to query the console
-- input stream. It works only on terminals that support each step and if data
-- can be emitted to 'stdin'. (Use 'System.IO.hIsTerminalDevice' to test if

My understanding of this was that if the preconditions do not hold (either terminal does not support cursor movement or stdin is not available), getTerminalSize :: IO (Maybe (Int, Int)) will return Nothing. I was surprised to learn that this is not the case.

Namely, running main = getTerminalSize >>= print with redirected stdin fails with an IO exception:

$ ./GetTerminalSize.hs < /dev/null
GetTerminalSize.hs: Uncaught exception ghc-internal:GHC.Internal.IO.Exception.IOException:

<stdin>: hWaitForInput: end of file

HasCallStack backtrace:
  ioException, called at libraries/ghc-internal/src/GHC/Internal/IO/Handle/Internals.hs:353:11 in ghc-internal:GHC.Internal.IO.Handle.Internals

Is it intended? Not the end of the world, but I feel like getTerminalSize could have caught the exception and return Nothing instead of passing the bucket to a caller.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions