blob: bf50c58dc315e64086046d2fd951be866b80a2b5 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
|