Skip to content

Upgrade Stdin from byte to bytes add support for terminal raw mode#54

Merged
rtfeldman merged 5 commits intoroc-lang:mainfrom
lukewilliamboswell:stdin-modes
Jun 18, 2023
Merged

Upgrade Stdin from byte to bytes add support for terminal raw mode#54
rtfeldman merged 5 commits intoroc-lang:mainfrom
lukewilliamboswell:stdin-modes

Conversation

@lukewilliamboswell
Copy link
Collaborator

This PR;

  1. Cross-platform support for entering terminal raw mode using the crossterm library, and
  2. Changes Stdin.byte to Stdin.bytes to read in up to 256 bytes from the input buffer as this is a much more useful function than processing a single byte at a time.

@lukewilliamboswell
Copy link
Collaborator Author

what happens if you switch back and forth to/from raw mode?

@rtfeldman I've conducted a number of experiments and from what I can tell nothing bad. I added the crossterm-rs library which I had used previously for the roc-tui platform, and provides cross-platform support for handling drivers.

src/Stdin.roc Outdated
@@ -17,8 +20,8 @@ line =
## Read a single byte from [standard input](https://en.wikipedia.org/wiki/Standard_streams#Standard_input_(stdin)).
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess the docs for this should be changed? 😄

Also, do we want only one of these two functions, or both?

In other words, is there any situation where I'd want to read a single byte instead of as much as I can from the buffer?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I thought a single byte would be useful, but after playing with stdin and decoding ANSI string escapes more I discovered that a single byte at a time is really unhelpful and difficult to use.

For example the below is how you might decode the Left key and Escape key. Without using multiple bytes from a Task you cannot process the input until you have looked ahead at the next byte, which may not be available (e.g. if there was only one read from stdin).

  • [27, 91, 68, ..] -> Key Left
  • [27, ..] -> Key Escape

Also, you can always read a list of bytes using this Task, and then chomp them one by one if that is required or easier for an algorithm such, i.e. recursively processing bytes.

Therefore I cannot think of a situation where we need a Task to read a single byte.

Copy link
Contributor

Choose a reason for hiding this comment

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

Gotcha, fair enough! 👍

@lukewilliamboswell
Copy link
Collaborator Author

Fixed docs for Stdin.bytes and added some for the Tty functions.

Screen Shot 2023-06-19 at 06 15 51

Copy link
Contributor

@rtfeldman rtfeldman left a comment

Choose a reason for hiding this comment

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

Sweet, thanks @lukewilliamboswell! 🎉

@rtfeldman rtfeldman merged commit 09bb635 into roc-lang:main Jun 18, 2023
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