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

[WIP] tty: Deno.stdin.setRaw() #3947

Closed
wants to merge 6 commits into from

Conversation

kevinkassimo
Copy link
Contributor

Basic implementation of raw mode for stdin. Implementation is tentative (op names unsure), and code is mostly lifted from crossterm or rustyline (without introducing more deps)

Likely won't work properly on Windows since I cannot test it locally.

// Returns a function that when called, restores original mode.
// I do not want to expose other tty flags, so this hides away the flags' existence
let restore = Deno.stdin.setRaw();
restore = Deno.stdin.setRaw(); // Safe to call again before restore.

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)));
  }
}

restore(); // restores old mode. Can be safely called multiple times
restore(); // safe to call multiple times

Sample run (assume imput is is raw):

$ ./target/debug/deno raw_mode.ts
READ 1 bytes: i
READ 1 bytes: s
READ 1 bytes:
READ 1 bytes: r
READ 1 bytes: a
READ 1 bytes: w

@ry
Copy link
Member

ry commented Feb 10, 2020

Looks pretty good! Nice work! I'm adding @piscisaureus to review because I think he has some specific ideas about how TTY APIs should look.

@ry ry requested a review from piscisaureus February 10, 2020 16:45
}

// From https://github.com/kkawakam/rustyline/blob/master/src/tty/windows.rs
// and https://github.com/crossterm-rs/crossterm/blob/e35d4d2c1cc4c919e36d242e014af75f6127ab50/src/terminal/sys/windows.rs
Copy link
Member

@ry ry Feb 10, 2020

Choose a reason for hiding this comment

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

We need to add their copyright header to this file. Something like this

// Copyright (c) 2015 Katsu Kawakami & Rustyline authors. MIT license.

@kevinkassimo
Copy link
Contributor Author

Also #3958 over option 2

@kevinkassimo
Copy link
Contributor Author

Closed in favor of #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