From 17998a5e8d386b58d30d138ea8dbc565955cccc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Sat, 11 Jan 2025 19:33:54 +0100 Subject: Concurrently run jobs for multiple commits Changelog: Concurrently run jobs for multiple commits --- src/Terminal.hs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/Terminal.hs (limited to 'src/Terminal.hs') diff --git a/src/Terminal.hs b/src/Terminal.hs new file mode 100644 index 0000000..bf50c58 --- /dev/null +++ b/src/Terminal.hs @@ -0,0 +1,45 @@ +module Terminal ( + TerminalOutput, + TerminalLine, + initTerminalOutput, + newLine, + redrawLine, +) where + +import Control.Concurrent + +import Data.Text (Text) +import Data.Text qualified as T +import Data.Text.IO qualified as T + +import System.IO + + +data TerminalOutput = TerminalOutput + { outNumLines :: MVar Int + } + +data TerminalLine = TerminalLine + { lineOutput :: TerminalOutput + , lineNum :: Int + } + +initTerminalOutput :: IO TerminalOutput +initTerminalOutput = do + outNumLines <- newMVar 0 + return TerminalOutput {..} + +newLine :: TerminalOutput -> Text -> IO TerminalLine +newLine lineOutput@TerminalOutput {..} text = do + modifyMVar outNumLines $ \lineNum -> do + T.putStrLn text + hFlush stdout + return ( lineNum + 1, TerminalLine {..} ) + +redrawLine :: TerminalLine -> Text -> IO () +redrawLine TerminalLine {..} text = do + let TerminalOutput {..} = lineOutput + withMVar outNumLines $ \total -> do + let moveBy = total - lineNum + T.putStr $ "\ESC[s\ESC[" <> T.pack (show moveBy) <> "F" <> text <> "\ESC[u" + hFlush stdout -- cgit v1.2.3