-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |