Skip to content

Update FFI to uncurried #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
64 changes: 18 additions & 46 deletions src/Node/ReadLine.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
41 changes: 22 additions & 19 deletions src/Node/ReadLine.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)