never executed always true always false
1 module PureClaw.Channels.CLI
2 ( -- * CLI channel
3 mkCLIChannelHandle
4 ) where
5
6 import Control.Exception (bracket_)
7 import Data.Text qualified as T
8 import Data.Text.IO qualified as TIO
9 import System.IO
10
11 import PureClaw.Core.Types
12 import PureClaw.Handles.Channel
13
14 -- | Create a channel handle that reads from stdin and writes to stdout.
15 -- Receive prints a @>@ prompt and reads a line. EOF (Ctrl-D) causes
16 -- an 'IOError' which the agent loop catches to exit cleanly.
17 mkCLIChannelHandle :: ChannelHandle
18 mkCLIChannelHandle = ChannelHandle
19 { _ch_receive = do
20 putStr "> "
21 hFlush stdout
22 line <- TIO.getLine
23 pure IncomingMessage
24 { _im_userId = UserId "cli-user"
25 , _im_content = line
26 }
27 , _ch_send = \msg -> do
28 TIO.putStrLn ""
29 TIO.putStrLn (_om_content msg)
30 TIO.putStrLn ""
31 , _ch_sendError = \err ->
32 TIO.hPutStrLn stderr $ "Error: " <> T.pack (show err)
33 , _ch_sendChunk = \case
34 ChunkText t -> do
35 TIO.putStr t
36 hFlush stdout
37 ChunkDone -> TIO.putStrLn ""
38 , _ch_readSecret = bracket_
39 (hSetEcho stdin False)
40 (hSetEcho stdin True)
41 TIO.getLine
42 , _ch_prompt = \promptText -> do
43 TIO.putStr promptText
44 hFlush stdout
45 TIO.getLine
46 , _ch_promptSecret = \promptText -> do
47 TIO.putStr promptText
48 hFlush stdout
49 bracket_ (hSetEcho stdin False) (hSetEcho stdin True) $ do
50 line <- TIO.getLine
51 TIO.putStrLn "" -- newline after hidden input
52 pure line
53 }