-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathReadLine.purs
More file actions
115 lines (93 loc) · 2.78 KB
/
ReadLine.purs
File metadata and controls
115 lines (93 loc) · 2.78 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
-- | This module provides a binding to the Node `readline` API.
module Node.ReadLine
( Interface
, InterfaceOptions
, Completer
, LineHandler
, createInterface
, createConsoleInterface
, output
, completer
, terminal
, historySize
, noCompletion
, prompt
, setPrompt
, setLineHandler
, close
, question
) where
import Prelude
import Effect (Effect)
import Foreign (Foreign)
import Data.Options (Options, Option, (:=), options, opt)
import Node.Process (stdin, stdout)
import Node.Stream (Readable, Writable)
-- | A handle to a console interface.
-- |
-- | A handle can be created with the `createInterface` function.
foreign import data Interface :: Type
foreign import createInterfaceImpl :: Foreign -> Effect Interface
-- | Options passed to `readline`'s `createInterface`
foreign import data InterfaceOptions :: Type
output :: forall w. Option InterfaceOptions (Writable w)
output = opt "output"
completer :: Option InterfaceOptions Completer
completer = opt "completer"
terminal :: Option InterfaceOptions Boolean
terminal = opt "terminal"
historySize :: Option InterfaceOptions Int
historySize = opt "historySize"
-- | A function which performs tab completion.
-- |
-- | This function takes the partial command as input, and returns a collection of
-- | completions, as well as the matched portion of the input string.
type Completer
= String
-> Effect
{ completions :: Array String
, matched :: String
}
-- | Builds an interface with the specified options.
createInterface
:: forall r
. Readable r
-> Options InterfaceOptions
-> Effect Interface
createInterface input opts = createInterfaceImpl
$ options $ opts
<> opt "input" := input
-- | Create an interface with the specified completion function.
createConsoleInterface :: Completer -> Effect Interface
createConsoleInterface compl =
createInterface stdin
$ output := stdout
<> completer := compl
-- | A completion function which offers no completions.
noCompletion :: Completer
noCompletion s = pure { completions: [], matched: s }
-- | Prompt the user for input on the specified `Interface`.
foreign import prompt :: Interface -> Effect Unit
-- | Writes a query to the output, waits
-- | for user input to be provided on input, then invokes
-- | the callback function
foreign import question
:: String
-> (String -> Effect Unit)
-> Interface
-> Effect Unit
-- | Set the prompt.
foreign import setPrompt
:: String
-> Interface
-> Effect Unit
-- | Close the specified `Interface`.
foreign import close :: Interface -> Effect Unit
-- | A function which handles each line of input.
type LineHandler a = String -> Effect a
-- | Set the current line handler function.
foreign import setLineHandler
:: forall a
. LineHandler a
-> Interface
-> Effect Unit