echo-0.1.3: A cross-platform, cross-console way to handle echoing terminal input

Copyright(C) 2016-2017 Ryan Scott
LicenseBSD-style (see the file LICENSE)
MaintainerRyan Scott
StabilityProvisional
PortabilityPortable
Safe HaskellSafe
LanguageHaskell2010

System.IO.Echo.Internal

Contents

Description

Exports functions that handle whether or not terminal input is handled in a way that should be portable across different platforms and consoles.

Unlike System.IO.Echo, this module exports internal functionality which, if used improperly, can lead to runtime errors. Make sure to read the documentation beforehand!

Synopsis

Safe public interface

withoutInputEcho :: IO a -> IO a #

Perform a computation with the terminal's input echoing disabled. Before running the computation, the terminal's input EchoState is saved, and the saved EchoState is restored after the computation finishes.

withoutInputEcho action = bracketInputEcho (setInputEchoState echoOff >> action)

bracketInputEcho :: IO a -> IO a #

Save the terminal's current input EchoState, perform a computation, restore the saved EchoState, and then return the result of the computation.

bracketInputEcho action = bracket getInputEchoState setInputEchoState (const action)

getInputEchoState :: IO EchoState #

Return the terminal's current input EchoState.

setInputEchoState :: EchoState -> IO () #

Set the terminal's input EchoState.

echoOff :: EchoState #

Indicates that the terminal's input echoing is (or should be) off.

echoOn :: EchoState #

Indicates that the terminal's input echoing is (or should be) on.

Alternative (safe) interface

getInputEcho :: IO Bool #

Return whether the terminal's echoing is on (True) or off (False).

Note that while this works on MinTTY, it is not as efficient as getInputEchoState, as it involves a somewhat expensive substring computation.

setInputEcho :: Bool -> IO () #

Set the terminal's echoing on (True) or off (False).

Unsafe STTY internals

data EchoState #

A representation of the terminal input's current echoing state. Example values include echoOff and echoOn.

Constructors

MinTTY STTYSettings

The argument to (or value returned from) an invocation of the stty command-line utility. Most POSIX-like shells have stty, including MinTTY on Windows. Since neither hGetEcho nor hSetEcho work on MinTTY, when getInputEchoState runs on MinTTY, it returns a value built with this constructor.

However, native Windows consoles like cmd.exe or PowerShell do not have stty, so if you construct an EchoState with this constructor manually, take care not to use it with a native Windows console.

DefaultTTY Bool

A simple on (True) or off (False) toggle. This is returned by hGetEcho and given as an argument to hSetEcho, which work on most consoles, with the notable exception of MinTTY on Windows. If you construct an EchoState with this constructor manually, take care not to use it with MinTTY.

type STTYSettings = String #

Settings used to configure the stty command-line utility.

getInputEchoSTTY :: IO STTYSettings #

Return all of stty's current settings in a non-human-readable format.

This function is not very useful on its own. Its greater purpose is to provide a compact STTYSettings that can be fed back into setInputEchoState.

setInputEchoSTTY :: STTYSettings -> IO () #

Create an stty process and wait for it to complete. This is useful for changing stty's settings, after which stty does not output anything.

setInputEchoSTTY = void . sttyRaw

sttyRaw :: String -> IO STTYSettings #

Create an stty process, wait for it to complete, and return its output.

MinTTY

minTTY :: Bool #

Is the current process attached to a MinTTY console (e.g., Cygwin or MSYS)?