summaryrefslogtreecommitdiff
path: root/src/Terminal.hs
diff options
context:
space:
mode:
authorRoman Smrž <roman.smrz@seznam.cz>2025-04-13 10:57:56 +0200
committerRoman Smrž <roman.smrz@seznam.cz>2025-04-15 19:51:22 +0200
commit30e91608555839e3cb0113cdbd670e76d2d35508 (patch)
tree7d5050c075dd60534ccb381fbfaa406e7db23cfb /src/Terminal.hs
parentd0ade87f13dec39eb3b62cac34c3fe31135a14f8 (diff)
Output style options
Changelog: Added `--terminal-output` and `--log-output` options to set output style
Diffstat (limited to 'src/Terminal.hs')
-rw-r--r--src/Terminal.hs27
1 files changed, 16 insertions, 11 deletions
diff --git a/src/Terminal.hs b/src/Terminal.hs
index aa7335c..1e71559 100644
--- a/src/Terminal.hs
+++ b/src/Terminal.hs
@@ -6,6 +6,7 @@ module Terminal (
newLine,
redrawLine,
newFootnote,
+ terminalHandle,
terminalBlinkStatus,
) where
@@ -22,7 +23,8 @@ import System.IO
data TerminalOutput = TerminalOutput
- { outNumLines :: MVar Int
+ { outHandle :: Handle
+ , outNumLines :: MVar Int
, outNextFootnote :: MVar Int
, outBlinkVar :: TVar Bool
}
@@ -37,14 +39,14 @@ data TerminalLine = TerminalLine
deriving (Eq)
data TerminalFootnote = TerminalFootnote
- { footnoteLine :: TerminalLine
- , footnoteNumber :: Int
- , footnoteText :: Text
+ { tfLine :: TerminalLine
+ , tfNumber :: Int
}
deriving (Eq)
initTerminalOutput :: IO TerminalOutput
initTerminalOutput = do
+ outHandle <- return stdout
outNumLines <- newMVar 0
outNextFootnote <- newMVar 1
outBlinkVar <- newTVarIO False
@@ -57,7 +59,7 @@ newLine :: TerminalOutput -> Text -> IO TerminalLine
newLine lineOutput@TerminalOutput {..} text = do
modifyMVar outNumLines $ \lineNum -> do
T.putStrLn text
- hFlush stdout
+ hFlush outHandle
return ( lineNum + 1, TerminalLine {..} )
redrawLine :: TerminalLine -> Text -> IO ()
@@ -66,14 +68,17 @@ redrawLine TerminalLine {..} text = do
withMVar outNumLines $ \total -> do
let moveBy = total - lineNum
T.putStr $ "\ESC[s\ESC[" <> T.pack (show moveBy) <> "F" <> text <> "\ESC[u"
- hFlush stdout
+ hFlush outHandle
newFootnote :: TerminalOutput -> Text -> IO TerminalFootnote
-newFootnote tout@TerminalOutput {..} footnoteText = do
- modifyMVar outNextFootnote $ \footnoteNumber -> do
- footnoteLine <- newLine tout $ "[" <> T.pack (show footnoteNumber) <> "] " <> footnoteText
- hFlush stdout
- return ( footnoteNumber + 1, TerminalFootnote {..} )
+newFootnote tout@TerminalOutput {..} text = do
+ modifyMVar outNextFootnote $ \tfNumber -> do
+ tfLine <- newLine tout $ "[" <> T.pack (show tfNumber) <> "] " <> text
+ hFlush outHandle
+ return ( tfNumber + 1, TerminalFootnote {..} )
+
+terminalHandle :: TerminalOutput -> Handle
+terminalHandle = outHandle
terminalBlinkStatus :: TerminalOutput -> STM Bool
terminalBlinkStatus TerminalOutput {..} = readTVar outBlinkVar