Skip to content
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

Add support for reading non-ASCII characters on Windows #64

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ Takes `config` option with the following possible properties

`history`: Takes an object that supplies a "history interface", see [prompt-sync-history](http://npm.im/prompt-sync-history) for an example.

`encoding`: Default is `utf-8`. On Windows used for decoding of non-ASCII characters. For Cyrillic specify `windows-1251` for example.

## `prompt(ask, value, opts)`

`ask` is the label of the prompt, `value` is the default value
Expand Down
10 changes: 7 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ function create(config) {
config = config || {};
var sigint = config.sigint;
var eot = config.eot;
var encoding = config.encoding || 'utf-8';
const decoder = new TextDecoder(encoding);
var autocomplete = config.autocomplete =
config.autocomplete || function(){return []};
var history = config.history;
Expand Down Expand Up @@ -140,7 +142,9 @@ function create(config) {

// if it is not a control character seq, assume only one character is read
character = buf[read-1];

const codePoint = process.platform == 'win32'
? decoder.decode(buf.subarray(0, read))
: String.fromCharCode(character);
// catch a ^C and return null
if (character == 3){
process.stdout.write('^C\n');
Expand Down Expand Up @@ -200,9 +204,9 @@ function create(config) {
insert--;
process.stdout.write('\u001b[2D');
} else {
if ((character < 32 ) || (character > 126))
if (character < 32)
continue;
str = str.slice(0, insert) + String.fromCharCode(character) + str.slice(insert);
str = str.slice(0, insert) + codePoint + str.slice(insert);
insert++;
};

Expand Down