Code Monkey home page Code Monkey logo

raw_tty.rs's Introduction

raw_tty

This crate can be used for generally interacting with a tty's mode safely, but was created originally to solve the problem of using raw mode with /dev/tty while reading stdin for data.

Usage

Raw Mode

Description from the termion crate:

Managing raw mode.

Raw mode is a particular state a TTY can have. It signifies that:

  1. No line buffering (the input is given byte-by-byte).
  2. The input is not written out, instead it has to be done manually by the programmer.
  3. The output is not canonicalized (for example, \n means "go one line down", not "line break").

It is essential to design terminal programs.

Example

use raw_tty::IntoRawMode;
use std::io::{Write, stdin, stdout};
                                                                                           
fn main() {
    let stdin = stdin().into_raw_mode().unwrap();
    let mut stdout = stdout();
                                                                                           
    write!(stdout, "Hey there.").unwrap();
}

Example with /dev/tty

use raw_tty::IntoRawMode;
use std::io::{self, Read, Write, stdin, stdout};
use std::fs;
                                                                                           
fn main() -> io::Result<()> {
    let mut tty = fs::OpenOptions::new().read(true).write(true).open("/dev/tty")?;
    // Can use the tty_input for keys while also reading stdin for data.
    let mut tty_input = tty.try_clone()?.into_raw_mode();
    let mut buffer = String::new();
    stdin().read_to_string(&mut buffer)?;
                                                                                           
    write!(tty, "Hey there.")
}

General example

use raw_tty::GuardMode;
use std::io::{self, Write, stdin, stdout};
                                                                                           
fn test_into_raw_mode() -> io::Result<()> {
    let mut stdin = stdin().guard_mode()?;
    stdin.set_raw_mode()?;
    let mut out = stdout();
                                                                                           
    out.write_all(b"this is a test, muahhahahah\r\n")?;
                                                                                           
    drop(out);
    Ok(())
}
                                                                                           
fn main() -> io::Result<()> {
    let mut stdout = stdout().guard_mode()?;
    stdout.modify_mode(|ios| /* do stuff with termios here */ ios)?;
                                                                                           
    // Have to use &* since TtyModeGuard only implements
    // deref, unlike RawReader which implements read specifically.
    // Otherwise, it wouldn't be recognized as `Write`able.
    write!(&mut *stdout, "Hey there.")
}
                                                                                           

raw_tty.rs's People

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

luqmana vandry

raw_tty.rs's Issues

Usefulness of IntoRawMode and RawReader

Since you've asked for feedback on the API:

As far as I can see, IntoRawMode does not serve any purpose besides the API similarity to termion, but is not actually the same, as it requires Read instead of Write.

What do you think of implementing Read and/or Write for TtyWithGuard<T> if <T> implements the same? This would avoid the *& (reborrowing?) shenanigans as shown in the general example of the crate and would definitely make RawReader obsolete.

Both of these are minor nitpicks, though! The crate is still very useful as it is, so thank you for that!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.