Comparing version 2.0.1 to 2.1.0
/// <reference types="node" /> | ||
import getopt from './getopt'; | ||
import read from './read'; | ||
import readLine from './readLine'; | ||
import ProgressBar from './ProgressBar'; | ||
@@ -9,2 +10,3 @@ import ask from './ask'; | ||
read: (lineHandler: import("./read").LineHandler, input?: NodeJS.ReadableStream) => Promise<void>; | ||
readLine: typeof readLine; | ||
ask: typeof ask; | ||
@@ -14,2 +16,2 @@ ProgressBar: typeof ProgressBar; | ||
export default stdio; | ||
export { getopt, read, ask, ProgressBar }; | ||
export { getopt, read, readLine, ask, ProgressBar }; |
@@ -7,2 +7,4 @@ "use strict"; | ||
exports.read = read_1.default; | ||
const readLine_1 = require("./readLine"); | ||
exports.readLine = readLine_1.default; | ||
const ProgressBar_1 = require("./ProgressBar"); | ||
@@ -15,2 +17,3 @@ exports.ProgressBar = ProgressBar_1.default; | ||
read: read_1.default, | ||
readLine: readLine_1.default, | ||
ask: ask_1.default, | ||
@@ -17,0 +20,0 @@ ProgressBar: ProgressBar_1.default, |
{ | ||
"name": "stdio", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"description": "Standard input/output manager for Node.js", | ||
@@ -5,0 +5,0 @@ "files": [ |
@@ -21,2 +21,3 @@ ![logo](https://user-images.githubusercontent.com/675812/61961326-88346a80-afc7-11e9-9853-f4ef66ce686c.png) | ||
- [read()](#read) | ||
- [readLine()](#readline) | ||
- [ask()](#ask) | ||
@@ -49,2 +50,3 @@ - [ProgressBar](#progressbar) | ||
- `read()`: an async function to read the standard input (or huge files) by lines, without having to worry about system resources. | ||
- `readLine()`: an async function to read a single line from the standard input. | ||
- `ask()`: an async function to ask questions in a terminal and wait for a user's response. | ||
@@ -189,3 +191,3 @@ | ||
This function reads standard input by lines, waiting for a line to be processed successfully before reading the next one. This is perfect for huge files as lines are read only as you process them, so you don't have to worry about system resources: | ||
This function reads the whole standard input by lines, waiting for a line to be processed successfully before reading the next one. This is perfect for huge files as lines are read only as you process them, so you don't have to worry about system resources. | ||
@@ -259,2 +261,52 @@ ```javascript | ||
## readLine() | ||
This function reads a single line from standard input. This is perfect for interactive terminal-based programs or just to read standard input on demand. | ||
```javascript | ||
import { readLine } from 'stdio'; | ||
(async () => { | ||
... | ||
const line = await readLine(<options>); | ||
... | ||
})() | ||
``` | ||
Where `<options>` is an optional object with the following properties: | ||
- `stream` (`Readable`): An object implementing `NodeJS.Readable` interface, like a stream. By default, `process.stdin` is used. | ||
- `close` (`boolean`): An optional flag to close the reader after returning the line. This is useful if you want to stop listening before finishing your program execution. | ||
<details> | ||
<summary>Example</summary> | ||
<p> | ||
The following simple program lets the user introduce basic instructions and responds interactively: | ||
```javascript | ||
import { readLine } from 'stdio'; | ||
(async () => { | ||
let command; | ||
do { | ||
command = await readLine(); | ||
if (command === 'SAY_A') { | ||
console.log('A'); | ||
} else if (command === 'SAY_B') { | ||
console.log('B'); | ||
} else if (command === 'EXIT') { | ||
console.log('Good bye'); | ||
await readLine({ close: true }); | ||
} | ||
} while (command !== 'EXIT') | ||
})() | ||
``` | ||
Note we're closing the line reader. In this case it could be replaced by a simple `process.exit(0)`, as our program doesn't do anything else. | ||
</p> | ||
</details> | ||
## ask() | ||
@@ -261,0 +313,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
38957
18
688
407