https://github.com/sant123/get_stdin
Get stdin as a string or buffer in Deno
https://github.com/sant123/get_stdin
cin console deno konsole read-line readline stdin
Last synced: 3 months ago
JSON representation
Get stdin as a string or buffer in Deno
- Host: GitHub
- URL: https://github.com/sant123/get_stdin
- Owner: sant123
- License: mit
- Created: 2020-08-30T14:11:46.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-10-08T01:10:53.000Z (over 4 years ago)
- Last Synced: 2024-04-14T10:16:07.759Z (about 1 year ago)
- Topics: cin, console, deno, konsole, read-line, readline, stdin
- Language: TypeScript
- Homepage:
- Size: 7.81 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# get_stdin 
> Get [stdin](https://doc.deno.land/builtin/stable#Deno.stdin) as a string or buffer
## Usage
```js
// example.ts
import { getStdin, getStdinBuffer } from 'https://deno.land/x/get_stdin/mod.ts';console.log(await getStdin());
//=> 'unicorns'console.log(await getStdinBuffer());
//=> Uint8Array(8) [
// 117, 110, 105,
// 99, 111, 114,
// 110, 115
// ]
```### There's a sync mode too
```js
// example.ts
import { getStdinSync, getStdinBufferSync } from 'https://deno.land/x/get_stdin/mod.ts';console.log(getStdinSync());
//=> 'unicorns'console.log(await getStdinBufferSync());
//=> Uint8Array(8) [
// 117, 110, 105,
// 99, 111, 114,
// 110, 115
// ]
``````sh
$ echo unicorns | deno run example.ts
unicorns
```### Also you can read like a cli application
```js
// example.ts
import { getStdin } from 'https://deno.land/x/get_stdin/mod.ts';console.log("¿What's your name?");
const guest = await getStdin();console.log(`Hellou ${guest || 'Stranger'}`);
//=> 'Hellou Deno'
```### You can read whole files from stdin too
```ts
// example.ts
import { getStdin } from 'https://deno.land/x/get_stdin/mod.ts';const input = await getStdin({ exitOnEnter: false });
console.log(`Received a bunch of (possibly) multiline text from stdin:\n${input}`);
``````sh
$ echo lots\nof\nstuff\nhere > example.txt
$ cat example.txt | deno run example.ts
lots
of
stuff
here
```## API
### getStdin(options?: GetStdinOptions)
Get `stdin` as a `string`.
### getStdinBuffer(options?: GetStdinOptions)
Get `stdin` as a `Buffer`.
### getStdinSync(options?: GetStdinOptions)
Get `stdin` as a `string` in sync mode.
### getStdinBufferSync(options?: GetStdinOptions)
Get `stdin` as a `Buffer` in sync mode.
### GetStdinOptions
- `exitOnEnter` (optional) - If `true`, stop reading the stdin once a newline char is reached. Defaults to `true`.
## Inspired
- Inspired by [get-stdin](https://github.com/sindresorhus/get-stdin) - Get stdin as a string or buffer