Comments (6)
Oops! Just seen the following lines right now:
if (opts.num) {
throw new Error('read() no longer accepts a char number limit')
}
I'm closing this issue because your thoughts seems obvious. Anyway i'm curious, was this feature removed because of a deficient implementation?
from read.
I'm perfectly happy re-opening this, and I'd accept a pull req to add the feature back, provided it doesn't break anything else.
It was removed because implementing it on top of the builtin readline
module was tricky, and not using the builtin readline
module is just painful. For example, is 12<backspace>3
4 characters or 2? what about ^W
to delete a word, etc? How do you count UTF-8 sequences?
If you look at the builtin readline
module in Node, you'll see that quite a lot of code has gone into making this work as humans expect it to, and there's a lot of unclear tradeoffs involved in getting it right for n-chars reading.
from read.
Actually read
behaves differently when the flag -n nchars
is set. Assume always typing 12<backspace>3<enter>
after the command:
$> read && echo $REPLY
13
13
$> read -n 3 && echo && echo $REPLY
12^?
12<backspace control character>��
$> read -n 4 && echo && echo $REPLY
12^?3
12<backspace control character>�3
It seems that read
will not respond as an human will expect when the -n
flag is active. It simply counts and stores each key stroke, and replies the full sequence.
Regarding the implementation on top of the builtin readline
, the key to implement it is the undocumented keypress
event that readline
adds to its input
stream. That is, rely on readline
's line
event when n
is not present, rely on the input stream keypress
event else case.
I have done it successfully while developing an interactive CLI based on "hit one key and it will do something" as design principle, resulting the following code. Please note this is a quick and dirty implementation, just an example of what I mean.
/**
* reads n characters from stdin
* if !n, it will read until enter is hit
*/
function readn( n, callback ){
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function done( data ){
rl.close();
callback( data );
}
if( !n ){
return rl.on( 'line', done )
}
var count = 0, sequence = '';
process.stdin.on( 'keypress', storeKey );
function storeKey( ch, key ){
sequence += ( key && key.sequence || ch );
count++;
if( count == n ){
this.removeListener( 'keypress', storeKey );
done( sequence );
}
}
}
from read.
Ok, well, if it doesn't break anything else this lib does, and the code isn't too complicated to manage, and it matches the behavior of read(1), then sure, patch welcome.
from read.
Nice!
I would like to start adding some tests before going in the wild. It would be my first time using tap so I will pull first those tests to ask some feedback from you.
My thoughts are taking care of how -n
will interact with other options. I was wondering specially what means the following statement on the README
file.
If silent is true, and the input is a TTY, then read will set raw mode, and read character by character.
It's simply an implementation note?
It seems logic to use num
as the option name as it used to be. Do you agree?
from read.
Yeah, that sounds good.
from read.
Related Issues (20)
- silent: true not working on Windows 8 HOT 4
- Getting some errors using the timeout function HOT 1
- Not able to override terminal to false
- Weird formatting of colored prompt message HOT 1
- Error when running `npm init` on edge node HOT 1
- Allow specific prompt length (ANSI)
- Read with nohup
- Optional output stream gets ended after read()
- Multiple questions HOT 1
- extra new line displayed after silenced password input HOT 1
- Licenses, LICENSE and LICENCE? HOT 2
- Silent still outputs text HOT 6
- please avoid writing to stderr during tests
- Backspace breaks silent: true
- [BUG] fails to read more than one line under certain conditions HOT 1
- Audit vulnerabilities detected in the read project on Tag: v1.0.7 HOT 2
- [QUESTION] How to prevent EAGAIN: resource temporarily unavailable when trying to read from stdin with `fs.readFileSync(0)` after using read?
- [BUG] Types no longer match implementation HOT 1
- [BUG] type definitions are broken HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from read.