summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-03-14 11:40:37 +0100
committertv <tv@krebsco.de>2026-03-14 11:40:41 +0100
commit3c7c985982ee3de6b710cca1b75cdd82c531fe42 (patch)
treea1415e2b8d5a98896ab51c0560b7379dc4400f8f
-rw-r--r--lib/Terminal.hs41
-rw-r--r--terminal-utilities.cabal16
2 files changed, 57 insertions, 0 deletions
diff --git a/lib/Terminal.hs b/lib/Terminal.hs
new file mode 100644
index 0000000..607ca23
--- /dev/null
+++ b/lib/Terminal.hs
@@ -0,0 +1,41 @@
+{-# LANGUAGE RecordWildCards #-}
+module Terminal
+ ( Config(..)
+ , setConfig
+ , withConfig
+ )
+ where
+
+import Control.Exception
+import Data.List
+import System.IO
+
+data Config = Config
+ { hin :: Handle
+ , hout :: Handle
+ , hinEcho :: Bool
+ , hinBufferMode :: BufferMode
+ , houtBufferMode :: BufferMode
+ , decsetPm :: [Int]
+ , decrstPm :: [Int]
+ }
+
+setConfig :: Config -> IO Config
+setConfig Config{..} = get <* set where
+ get = Config <$> pure hin
+ <*> pure hout
+ <*> hGetEcho hin
+ <*> hGetBuffering hin
+ <*> hGetBuffering hout
+ <*> pure decrstPm
+ <*> pure decsetPm
+ set = do
+ hSetEcho hin hinEcho
+ hSetBuffering hin hinBufferMode
+ hSetBuffering hout houtBufferMode
+ hPutStr hout $ "\ESC[?" <> intercalate ";" (map show decsetPm) <> "h"
+ hPutStr hout $ "\ESC[?" <> intercalate ";" (map show decrstPm) <> "l"
+ hFlush hout
+
+withConfig :: Config -> IO a -> IO a
+withConfig s = bracket (setConfig s) setConfig . const
diff --git a/terminal-utilities.cabal b/terminal-utilities.cabal
new file mode 100644
index 0000000..0f2e74b
--- /dev/null
+++ b/terminal-utilities.cabal
@@ -0,0 +1,16 @@
+cabal-version: 3.0
+name: terminal-utilities
+version: 1.0.0.0
+license: MIT
+author: tv
+maintainer: tv@krebsco.de
+build-type: Simple
+
+library
+ exposed-modules:
+ Terminal
+ build-depends:
+ base
+ default-language: GHC2024
+ ghc-options: -O2 -Wall -Wextra
+ hs-source-dirs: lib