diff options
author | Roman Smrž <roman.smrz@seznam.cz> | 2025-03-15 22:50:07 +0100 |
---|---|---|
committer | Roman Smrž <roman.smrz@seznam.cz> | 2025-03-16 20:48:35 +0100 |
commit | 4896804069ea2b098f5dda2d0135d667778a4741 (patch) | |
tree | 2f1ac178c81d683191d0fb6407a89b91d96a29df /src/Terminal.hs | |
parent | 52dca5dc0e60d4d84aa5ecf280a45b24f1111dda (diff) |
Footnotes for errors in terminal output
Diffstat (limited to 'src/Terminal.hs')
-rw-r--r-- | src/Terminal.hs | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/Terminal.hs b/src/Terminal.hs index 84dfb91..aa7335c 100644 --- a/src/Terminal.hs +++ b/src/Terminal.hs @@ -1,9 +1,11 @@ module Terminal ( TerminalOutput, TerminalLine, + TerminalFootnote(..), initTerminalOutput, newLine, redrawLine, + newFootnote, terminalBlinkStatus, ) where @@ -11,6 +13,7 @@ import Control.Concurrent import Control.Concurrent.STM import Control.Monad +import Data.Function import Data.Text (Text) import Data.Text qualified as T import Data.Text.IO qualified as T @@ -20,17 +23,30 @@ import System.IO data TerminalOutput = TerminalOutput { outNumLines :: MVar Int + , outNextFootnote :: MVar Int , outBlinkVar :: TVar Bool } +instance Eq TerminalOutput where + (==) = (==) `on` outNumLines + data TerminalLine = TerminalLine { lineOutput :: TerminalOutput , lineNum :: Int } + deriving (Eq) + +data TerminalFootnote = TerminalFootnote + { footnoteLine :: TerminalLine + , footnoteNumber :: Int + , footnoteText :: Text + } + deriving (Eq) initTerminalOutput :: IO TerminalOutput initTerminalOutput = do outNumLines <- newMVar 0 + outNextFootnote <- newMVar 1 outBlinkVar <- newTVarIO False void $ forkIO $ forever $ do threadDelay 500000 @@ -52,5 +68,12 @@ redrawLine TerminalLine {..} text = do T.putStr $ "\ESC[s\ESC[" <> T.pack (show moveBy) <> "F" <> text <> "\ESC[u" hFlush stdout +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 {..} ) + terminalBlinkStatus :: TerminalOutput -> STM Bool terminalBlinkStatus TerminalOutput {..} = readTVar outBlinkVar |