summaryrefslogtreecommitdiff
path: root/src/Terminal.hs
diff options
context:
space:
mode:
authorRoman Smrž <roman.smrz@seznam.cz>2025-01-11 19:33:54 +0100
committerRoman Smrž <roman.smrz@seznam.cz>2025-01-11 21:55:28 +0100
commit17998a5e8d386b58d30d138ea8dbc565955cccc6 (patch)
tree3bad48996590b33c1d64557b31a4fca8221eca18 /src/Terminal.hs
parent61a9e98239cf01e91ca079ef176602efe0077dde (diff)
Concurrently run jobs for multiple commits
Changelog: Concurrently run jobs for multiple commits
Diffstat (limited to 'src/Terminal.hs')
-rw-r--r--src/Terminal.hs45
1 files changed, 45 insertions, 0 deletions
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