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

fix(prompt): fix prompt output of non-ascii characters on windows #8199

Merged
merged 1 commit into from
Nov 3, 2020
Merged
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
12 changes: 6 additions & 6 deletions cli/rt/41_prompt.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
((window) => {
const { stdin, stdout } = window.__bootstrap.files;
const { stdin } = window.__bootstrap.files;
const { isatty } = window.__bootstrap.tty;
const LF = "\n".charCodeAt(0);
const CR = "\r".charCodeAt(0);
const encoder = new TextEncoder();
const decoder = new TextDecoder();
const core = window.Deno.core;

function alert(message = "Alert") {
if (!isatty(stdin.rid)) {
return;
}

stdout.writeSync(encoder.encode(`${message} [Enter] `));
core.print(`${message} [Enter] `, false);

readLineFromStdinSync();
}
Expand All @@ -22,7 +22,7 @@
return false;
}

stdout.writeSync(encoder.encode(`${message} [y/N] `));
core.print(`${message} [y/N] `, false);

const answer = readLineFromStdinSync();

Expand All @@ -36,10 +36,10 @@
return null;
}

stdout.writeSync(encoder.encode(`${message} `));
core.print(`${message} `, false);

if (defaultValue) {
stdout.writeSync(encoder.encode(`[${defaultValue}] `));
core.print(`[${defaultValue}] `, false);
}

return readLineFromStdinSync() || defaultValue;
Expand Down
3 changes: 3 additions & 0 deletions core/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use futures::future::FutureExt;
use rusty_v8 as v8;
use std::cell::Cell;
use std::convert::TryFrom;
use std::io::{stdout, Write};
use std::option::Option;
use url::Url;
use v8::MapFnTo;
Expand Down Expand Up @@ -352,8 +353,10 @@ fn print(
};
if is_err {
eprint!("{}", str_.to_rust_string_lossy(tc_scope));
stdout().flush().unwrap();
} else {
print!("{}", str_.to_rust_string_lossy(tc_scope));
stdout().flush().unwrap();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added flush here because print! macro doesn't seem flushing automatically. rust-lang/rust#23818

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kt3k this is a bit strange - Deno.core.print() works for me without a problem without the flush() and it's one of purposes of Deno.core.print not to include new line by default. Could you explain the reason for this change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bartlomieju print! is line-buffered as discussed in rust-lang/rust#23818. So if we use it like Deno.core.print('hello'), the string doesn't show in stdout until new line is printed or process is finished. (Try the script Deno.core.print('xyz'); setTimeout(() => {}, 4000); for example).

If we don't add flush() here, prompt works like the below:

> prompt('hi!')
hello
hi! "hello"

(The answer and question are reversed here.) I think this isn't what the users expect.

If we add flush(), it works like:

> prompt('hi!')
hi! hello
"hello"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kt3k I can not reproduce that:
image

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In repl, the returned value is printed with line break (such as "1\n") and this line break should trigger flushing of stdout.

Try the following:

deno eval "Deno.core.print('xyz'); setTimeout(() => {}, 4000);"

}
}

Expand Down