Skip to content

Commit

Permalink
And more caps
Browse files Browse the repository at this point in the history
  • Loading branch information
nc6 committed Jan 10, 2014
1 parent 4c988b9 commit 0c49ee1
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Tabula/TTY.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module Tabula.TTY where
import Control.Exception.Base (bracket_)

import System.Posix.IO (openFd, OpenMode(ReadWrite), defaultFileFlags)
import System.Posix.Terminal
import System.Posix.Types (Fd(..))

getControllingTerminal :: IO Fd
getControllingTerminal = getControllingTerminalName >>=
\a -> openFd a ReadWrite Nothing defaultFileFlags

bracketChattr :: Fd -> (TerminalAttributes -> TerminalAttributes) -> IO a -> IO a
bracketChattr fd chattr action = do
oldAttrs <- getTerminalAttributes fd
bracket_ (initialise oldAttrs)
(finalise oldAttrs)
action
where
initialise oldAttrs = do
let newAttrs = chattr oldAttrs
setTerminalAttributes fd newAttrs Immediately
finalise oldAttrs = setTerminalAttributes fd oldAttrs Immediately

setRaw :: (TerminalAttributes -> TerminalAttributes)
setRaw = withoutModes rawModes where
rawModes = [ProcessInput, KeyboardInterrupts, ExtendedFunctions,
EnableEcho, InterruptOnBreak, MapCRtoLF, IgnoreBreak,
IgnoreCR, MapLFtoCR, CheckParity, StripHighBit,
StartStopOutput, MarkParityErrors, ProcessOutput]
withoutModes modes tty = foldl withoutMode tty modes

cloneAttr :: Fd -> Fd -> IO ()
cloneAttr from to = getTerminalAttributes from >>=
\a -> setTerminalAttributes to a Immediately

0 comments on commit 0c49ee1

Please sign in to comment.