diff --git a/CHANGELOG.md b/CHANGELOG.md index ca15b17..510d1d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ Other improvements: - Update CI `node` version to `lts/*` (#31, #32 by @JordanMartinez) - Update CI actions to `v3` (#31, #32 by @JordanMartinez) - Format code via `purs-tidy`; enforce formatting in CI (#31, #32 by @JordanMartinez) +- Update FFI to use uncurried functions (#33 by @JordanMartinez) ## [v7.0.0](https://github.com/purescript-node/purescript-node-readline/releases/tag/v7.0.0) - 2022-04-29 diff --git a/src/Node/ReadLine.js b/src/Node/ReadLine.js index cb70419..b8694c1 100644 --- a/src/Node/ReadLine.js +++ b/src/Node/ReadLine.js @@ -2,50 +2,22 @@ import { createInterface } from "readline"; -export function createInterfaceImpl(options) { - return () => createInterface({ - input: options.input, - output: options.output, - completer: options.completer && (line => { - const res = options.completer(line)(); - return [res.completions, res.matched]; - }), - terminal: options.terminal, - historySize: options.historySize, - }); -} +export const createInterfaceImpl = (options) => createInterface({ + input: options.input, + output: options.output, + completer: options.completer && (line => { + const res = options.completer(line)(); + return [res.completions, res.matched]; + }), + terminal: options.terminal, + historySize: options.historySize, +}); -export function close(readline) { - return () => { - readline.close(); - }; -} - -export function prompt(readline) { - return () => { - readline.prompt(); - }; -} - -export function question(text) { - return callback => readline => () => { - readline.question(text, result => { - callback(result)(); - }); - }; -} - -export function setPrompt(prompt) { - return readline => () => { - readline.setPrompt(prompt); - }; -} - -export function setLineHandler(callback) { - return readline => () => { - readline.removeAllListeners("line"); - readline.on("line", line => { - callback(line)(); - }); - }; -} +export const closeImpl = (readline) => readline.close(); +export const promptImpl = (readline) => readline.prompt(); +export const questionImpl = (readline, text, cb) => readline.question(text, cb); +export const setPromptImpl = (readline, prompt) => readline.setPrompt(prompt); +export const setLineHandlerImpl = (readline, cb) => { + readline.removeAllListeners("line"); + readline.on("line", cb); +}; diff --git a/src/Node/ReadLine.purs b/src/Node/ReadLine.purs index 4444e24..a92d315 100644 --- a/src/Node/ReadLine.purs +++ b/src/Node/ReadLine.purs @@ -21,11 +21,10 @@ module Node.ReadLine import Prelude +import Data.Options (Options, Option, (:=), options, opt) import Effect (Effect) - +import Effect.Uncurried (EffectFn1, EffectFn2, EffectFn3, runEffectFn1, runEffectFn2, runEffectFn3) import Foreign (Foreign) -import Data.Options (Options, Option, (:=), options, opt) - import Node.Process (stdin, stdout) import Node.Stream (Readable, Writable) @@ -85,32 +84,36 @@ noCompletion :: Completer noCompletion s = pure { completions: [], matched: s } -- | Prompt the user for input on the specified `Interface`. -foreign import prompt :: Interface -> Effect Unit +prompt :: Interface -> Effect Unit +prompt iface = runEffectFn1 promptImpl iface + +foreign import promptImpl :: EffectFn1 (Interface) (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 +question :: String -> (String -> Effect Unit) -> Interface -> Effect Unit +question text cb iface = runEffectFn3 questionImpl iface text cb + +foreign import questionImpl :: EffectFn3 (Interface) (String) ((String -> Effect Unit)) Unit -- | Set the prompt. -foreign import setPrompt - :: String - -> Interface - -> Effect Unit +setPrompt :: String -> Interface -> Effect Unit +setPrompt newPrompt iface = runEffectFn2 setPromptImpl iface newPrompt + +foreign import setPromptImpl :: EffectFn2 (Interface) (String) (Unit) -- | Close the specified `Interface`. -foreign import close :: Interface -> Effect Unit +close :: Interface -> Effect Unit +close iface = runEffectFn1 closeImpl iface + +foreign import closeImpl :: EffectFn1 (Interface) (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 +setLineHandler :: forall a. LineHandler a -> Interface -> Effect Unit +setLineHandler cb iface = runEffectFn2 setLineHandlerImpl iface cb + +foreign import setLineHandlerImpl :: forall a. EffectFn2 (Interface) (LineHandler a) (Unit)