Skip to content

Commit ecc6af4

Browse files
committed
Initial commit
0 parents  commit ecc6af4

File tree

7 files changed

+113
-0
lines changed

7 files changed

+113
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
.DS_Store

.prettierrc

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"singleQuote": true,
3+
"trailingComma": "all",
4+
"printWidth": 120,
5+
"arrowParens": "avoid"
6+
}

LICENSE

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
The MIT License (MIT)
2+
Copyright © 2024 Mimo GmbH
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5+
6+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7+
8+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Whitespace-only changes.

index.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const fs = require('fs');
2+
3+
function readStdinUntilNewline() {
4+
const BUFFER_SIZE = 256;
5+
let totalBuf = Buffer.alloc(0);
6+
7+
let fileDescriptor = process.stdin.fd;
8+
let closeFs = false;
9+
10+
try {
11+
fileDescriptor = process.stdin.isTTY ? fs.openSync('/dev/tty', 'rs') : fs.openSync('/dev/stdin', 'rs');
12+
closeFs = true;
13+
} catch (e) {}
14+
15+
while (true) {
16+
const buffer = Buffer.alloc(BUFFER_SIZE);
17+
const bytesRead = fs.readSync(fileDescriptor, buffer, 0, BUFFER_SIZE, null);
18+
19+
if (bytesRead === 0) {
20+
break;
21+
}
22+
23+
totalBuf = Buffer.concat([totalBuf, buffer.subarray(0, bytesRead)]);
24+
25+
if (buffer.subarray(0, bytesRead).includes('\n')) {
26+
break;
27+
}
28+
}
29+
30+
if (closeFs) {
31+
fs.closeSync(fileDescriptor);
32+
}
33+
34+
return totalBuf;
35+
}
36+
37+
let stdinBuffer = '';
38+
39+
function input(prompt) {
40+
if (prompt) {
41+
process.stdout.write(prompt);
42+
}
43+
44+
if (stdinBuffer.length === 0) {
45+
stdinBuffer = readStdinUntilNewline().toString('utf-8');
46+
}
47+
48+
const newline = stdinBuffer.indexOf('\n');
49+
const line = stdinBuffer.slice(0, newline);
50+
51+
stdinBuffer = stdinBuffer.slice(newline + 1);
52+
return line;
53+
}
54+
55+
module.exports = input;

package.json

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "@mimo-org/input-sync",
3+
"version": "0.0.1",
4+
"description": "A simple, synchronous input function to read from stdin, inspired by Python's input()",
5+
"author": "Dennis Daume <[email protected]>",
6+
"license": "MIT",
7+
"main": "index.js",
8+
"scripts": {
9+
"test": "node test.js"
10+
},
11+
"repository": {
12+
"type": "git",
13+
"url": "git+https://github.com/getmimo/input-sync.git"
14+
},
15+
"keywords": [
16+
"stdin",
17+
"input",
18+
"read",
19+
"readline",
20+
"prompt",
21+
"cli",
22+
"command-line",
23+
"terminal",
24+
"console",
25+
"getline"
26+
]
27+
}

test.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const input = require('./index.js');
2+
3+
const input1 = input("Enter 'input 1': ");
4+
5+
if (input1 != 'input 1') {
6+
throw new Error(`'${input1}' is not equal to 'input 1'`);
7+
}
8+
9+
const input2 = input("Enter 'input 2': ");
10+
11+
if (input2 !== 'input 2') {
12+
throw new Error(`'${input2}' is not equal to 'input 2'`);
13+
}
14+
15+
console.log('All tests passed!');

0 commit comments

Comments
 (0)