/
Aff.purs
86 lines (80 loc) · 2.56 KB
/
Aff.purs
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
-- | This module provides an interface in Aff to Node.ReadLine
-- |
-- | Example usage:
-- |
-- | ```
-- | import Node.ReadLine (close) as RL
-- | import Node.ReadLine.Aff (question, setPrompt, prompt, READLINE, createConsoleInterface, noCompletion)
-- | main :: forall e. Eff (console :: CONSOLE, readline :: READLINE, exception :: EXCEPTION | e) Unit
-- | main = do
-- | interface <- createConsoleInterface noCompletion
-- | runAff_ (either
-- | (\err -> showError err *> RL.close interface)
-- | (const $ RL.close interface))
-- | (loop interface)
-- | where
-- | showError err = error (show err)
-- | loop interface = do
-- | setPrompt "$ " interface
-- | dog <- question "What's your dog's name?\n" interface
-- | liftEff <<< log $ ("Can I pet " <> dog <> "?")
-- | str <- prompt interface
-- | case uncons str of
-- | Just {head: 'y'} -> liftEff $ log "Thanks!"
-- | _ -> (liftEff $ log "C'mon! Be a sport about it!") *> loop interface
-- | ````
module Node.ReadLine.Aff
( close
, prompt
, question
, setPrompt
, module RLExports
) where
import Prelude
import Effect.Aff (makeAff, nonCanceler)
import Effect.Aff.Class (class MonadAff, liftAff)
import Effect.Class (class MonadEffect, liftEffect)
import Data.Either (Either(..))
import Data.String (length)
import Node.ReadLine (output, completer, terminal, historySize, noCompletion, createInterface, createConsoleInterface, Completer, Interface, InterfaceOptions) as RLExports
import Node.ReadLine as RL
-- | Writes a query to the output and returns the response
question
:: forall m
. MonadAff m
=> String
-> RL.Interface
-> m String
question q interface = do
liftAff $ makeAff go
where
go handler = RL.question q (handler <<< Right) interface $> nonCanceler
-- | Set the prompt, this is displayed for future `prompt` calls.
setPrompt
:: forall m
. MonadEffect m
=> String
-> RL.Interface
-> m Unit
setPrompt promptText interface =
liftEffect $ RL.setPrompt promptText (length promptText) interface
-- | Read a single line from input using the current prompt.
prompt
:: forall m
. MonadAff m
=> RL.Interface
-> m String
prompt interface = do
liftAff $ makeAff go
where
go handler = do
RL.setLineHandler interface (handler <<< Right)
RL.prompt interface
pure nonCanceler
-- | Close the specified Interface. This should upon error, or when you're done reading input.
close
:: forall m
. MonadEffect m
=> RL.Interface
-> m Unit
close interface = liftEffect (RL.close interface)