Skip to content
This repository has been archived by the owner on Oct 22, 2019. It is now read-only.

Commit

Permalink
Attempt to sync raw modes crossplatform (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimonPost authored Oct 18, 2019
1 parent 929fb44 commit 1ee2cf8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 56 deletions.
52 changes: 17 additions & 35 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,46 +33,28 @@
//!
//! ### Raw Mode
//!
//! To understand the concept of a 'raw mode' let's look at the following points:
//!
//! **No line buffering.**
//!
//! Normally the terminals use line buffering. This means that the input will be sent to the terminal
//! line by line. With raw mode, the input will send one byte at a time.
//!
//! **Input**
//!
//! All input has to be written to the screen buffer manually by the programmer.
//!
//! **Characters**
//!
//! The characters are not processed by the terminal driver. Also, special character has no meaning.
//! For example, backspace will not be interpreted as backspace but instead will be sent directly to
//! the terminal.
//!
//! **Escape Characters**
//!
//! Note that in raw mode `\n` `\r` will move the cursor to a new line but it will be at the
//! same position as it was on the previous line.
//!
//! Example:
//!
//! ```text
//! some text
//! some text
//!```
//!
//! To start at the beginning of the next line, use `\n\r`.
#[doc(no_inline)]
pub use crossterm_utils::{
execute, queue, Command, ErrorKind, ExecutableCommand, QueueableCommand, Result,
};
//! By default, the terminal behaves in a certain way.
//! You can think of going to a new line if the input is at the end of the current line, or interpreting backspace
//! to remove letters. Sometimes it can be useful to disable these modes because this is undesirable.
//! This may be undesirable if your application wants to read the input without it being shown on the screen.
//! Raw modes are the modes to create this possibility.
//
//! Those modes will be set when enabling raw modes:
//!
//! - Input will not be forwarded to screen
//! - Input will not be processed on enter press
//! - Input will not be line buffered (input sent byte-by-byte to input buffer)
//! - Special keys like backspace and CTL+C will not be processed by terminal driver
//! - New line character will not be processed therefore `println!` can't be used, use `write!` instead
// This brings the trait into scope, so we're able to call enter()/leave(),
// but it it's false positive for unused_imports check
#[allow(unused_imports)]
use alternate::AlternateScreen as _;
#[doc(no_inline)]
pub use crossterm_utils::{
execute, queue, Command, ErrorKind, ExecutableCommand, QueueableCommand, Result,
};

pub use self::raw::{IntoRawMode, RawScreen};

Expand Down
16 changes: 0 additions & 16 deletions src/raw.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
//! This module is used for enabling and disabling raw mode for the terminal.
//!
//! What exactly is raw state:
//! - No line buffering.
//! Normally the terminals uses line buffering. This means that the input will be send to the terminal line by line.
//! With raw mode the input will be send one byte at a time.
//! - Input
//! All input has to be written manually by the programmer.
//! - Characters
//! The characters are not processed by the terminal driver, but are sent straight through.
//! Special character have no meaning, like backspace will not be interpret as backspace but instead will be directly send to the terminal.
//! - Escape characters
//! Note that in raw modes `\n` `\r` will move to the new line but the cursor will be at the same position as before on the new line therefor use `\n\r` to start at the new line at the first cell.
//!
//! With these modes you can easier design the terminal screen.
use std::io::{Stdout, Write};

use crossterm_utils::Result;
Expand Down
10 changes: 5 additions & 5 deletions src/sys/winapi.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crossterm_utils::Result;
use crossterm_winapi::ConsoleMode;
use crossterm_winapi::{ConsoleMode, Handle};
use winapi::shared::minwindef::DWORD;
use winapi::um::wincon;

use self::wincon::{ENABLE_LINE_INPUT, ENABLE_WRAP_AT_EOL_OUTPUT};
use self::wincon::{ENABLE_ECHO_INPUT, ENABLE_LINE_INPUT, ENABLE_PROCESSED_INPUT};

/// This command is used for enabling and disabling raw mode for Windows systems.
/// For more info check: https://docs.microsoft.com/en-us/windows/console/high-level-console-modes.
Expand All @@ -15,15 +15,15 @@ pub struct RawModeCommand {
impl RawModeCommand {
pub fn new() -> Self {
RawModeCommand {
mask: ENABLE_WRAP_AT_EOL_OUTPUT | ENABLE_LINE_INPUT,
mask: ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT | ENABLE_PROCESSED_INPUT,
}
}
}

impl RawModeCommand {
/// Enables raw mode.
pub fn enable(&mut self) -> Result<()> {
let console_mode = ConsoleMode::new()?;
let console_mode = ConsoleMode::from(Handle::input_handle()?);

let dw_mode = console_mode.mode()?;

Expand All @@ -36,7 +36,7 @@ impl RawModeCommand {

/// Disables raw mode.
pub fn disable(&self) -> Result<()> {
let console_mode = ConsoleMode::new()?;
let console_mode = ConsoleMode::from(Handle::input_handle()?);

let dw_mode = console_mode.mode()?;

Expand Down

0 comments on commit 1ee2cf8

Please sign in to comment.