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

tty: Deno.setRaw(rid, mode) to turn on/off raw mode #3958

Merged
merged 22 commits into from
Feb 26, 2020

Conversation

kevinkassimo
Copy link
Contributor

@kevinkassimo kevinkassimo commented Feb 11, 2020

Difference from the first option:

  • Deno.setRaw(rid, mode) to turn on/off raw mode
  • Added Deno.isatty(rid) that can test any resources, not just stdin/stdout/stderr (personally prefer deprecating current Deno.isTTY())
Deno.setRaw(0, true);
Deno.setRaw(0, true); // safe

const buf = new Uint8Array(6);
for (let i = 0; i < 6; i++) {
  const nread = await Deno.stdin.read(buf);
  if (nread === Deno.EOF) {
    break;
  } else {
    console.log(`READ ${nread} bytes:`, new TextDecoder().decode(buf.subarray(0, nread)));
  }
}

Deno.setRaw(0, false);
Deno.setRaw(0, false); // safe

@kevinkassimo kevinkassimo force-pushed the tty/raw_mode_2 branch 3 times, most recently from 7a553ae to 8776116 Compare February 11, 2020 03:52
/** An instance of `File` for stdout. */
export const stdout: File;
/** An instance of `File` for stderr. */
export const stderr: File;
Copy link
Member

@ry ry Feb 13, 2020

Choose a reason for hiding this comment

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

I like the minimality of isatty(rid). Maybe you can do something similar here, instead of defining this new class TTYInput you could just add a new function... something like Deno.setRaw(rid). What do you think?

Additionally can you mark in the JSDOC any new APIs as

/** UNSTABLE: newly added API
  *
  *  Check if a given resource is TTY
  */
export function isatty(rid: number): boolean;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you think the original returning a restoration function after calling Deno.setRaw(rid) works here? (In case I want to store the information for restoring without exposing? This would be a pure TS side change, and similar to what I did in the old PR, but without wrapping in a class)

Copy link
Member

Choose a reason for hiding this comment

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

I don't think storing the restore state is so important. The user can do that.

Copy link
Contributor Author

@kevinkassimo kevinkassimo Feb 13, 2020

Choose a reason for hiding this comment

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

I don't think they can with current proposed API (unless we expose a full representation of <termios> utilities) if we do not store for them? And there is the discrepancy between Unix and Windows here

Node does not provide the API for direct restoration either. I believe with tty.ReadStream::setRaw(false) libuv simply sets back to the saved original mode

Copy link
Member

@ry ry Feb 13, 2020

Choose a reason for hiding this comment

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

libuv simply sets back to the saved original mode

Yes we should do that too... I mean that the JS interface does not need to expose state restoration just boolean on/off is good enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The slight annoyance is that we will need to accompany StreamResource (including FsFile) with such optional data. I'll see where would be the best place for me to place this piece of info.

@kevinkassimo kevinkassimo force-pushed the tty/raw_mode_2 branch 2 times, most recently from b4429f4 to 4999c68 Compare February 14, 2020 00:18
@kevinkassimo kevinkassimo force-pushed the tty/raw_mode_2 branch 2 times, most recently from 01128e5 to dc4cddd Compare February 22, 2020 00:09
@kevinkassimo
Copy link
Contributor Author

kevinkassimo commented Feb 22, 2020

Added a basic test (unix only, I have no access to Windows). Added pty crate as a dev dependency.

permissions::tests::test_permissions_request_read and permissions::tests::test_permission_request_net seems to be badly flaky locally

@kevinkassimo kevinkassimo changed the title [WIP] tty: Deno.stdin.setRaw(), Option 2 tty: Deno.stdin.setRaw(), Option 2 Feb 22, 2020
@kevinkassimo kevinkassimo force-pushed the tty/raw_mode_2 branch 2 times, most recently from 9cc2e05 to ec5e100 Compare February 22, 2020 03:01
@kevinkassimo kevinkassimo requested a review from ry February 22, 2020 06:16
@ry
Copy link
Member

ry commented Feb 23, 2020

@kevinkassimo Looks good but I'm a little confused because there is still Deno.isTTY() which if I'm reading your patch correctly does not call the new Deno.isatty() .. which I would expect it to.

@kevinkassimo
Copy link
Contributor Author

@ry I can probably update isTTY to use isatty. I was originally thinking about deprecating isTTY since the 2 has very similar names while isTTY only handles stdin/stdout/stderr

@kevinkassimo kevinkassimo force-pushed the tty/raw_mode_2 branch 2 times, most recently from b4643e5 to f1fbe6f Compare February 23, 2020 21:14
cli/ops/os.rs Show resolved Hide resolved
cli/js/tty.ts Outdated Show resolved Hide resolved
Copy link
Member

@ry ry left a comment

Choose a reason for hiding this comment

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

LGTM

@kevinkassimo kevinkassimo changed the title tty: Deno.stdin.setRaw(), Option 2 tty: Deno.setRaw(rid, mode) to turn on/off raw mode Feb 24, 2020
@kevinkassimo
Copy link
Contributor Author

Is this PR considered ready? Have been resolving conflicts with master updates for quite a few times 😅

Copy link
Member

@ry ry left a comment

Choose a reason for hiding this comment

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

LGTM - sorry for the delay! Thanks!

@ry ry merged commit 5946808 into denoland:master Feb 26, 2020
Alhadis added a commit to Alhadis/EAL that referenced this pull request Mar 4, 2020
sheepla added a commit to sheepla/era that referenced this pull request Jul 16, 2023
function `Deno.setRaw(rid, mod)` is not implemented on Deno v1.35.1,
rewrite insted of `Deno.stdin.setRaw(boolean b)`
c.f. denoland/deno#3958
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants