fixed raw mode
This commit is contained in:
parent
cc55c190d8
commit
99fffee6b3
@ -19,6 +19,7 @@ use std::io::Write;
|
||||
use std::{thread,time};
|
||||
fn main()
|
||||
{
|
||||
input::keyboard::async_input::read_async_demo();
|
||||
terminal::raw_mode::print_wait_screen_on_alternate_window();
|
||||
thread::sleep(time::Duration::from_millis(2000));
|
||||
}
|
||||
|
@ -13,26 +13,26 @@ fn main() {
|
||||
|
||||
let screen = Screen::new(true);
|
||||
let crossterm = Crossterm::new(&screen);
|
||||
|
||||
let input = crossterm.input();
|
||||
let terminal = crossterm.terminal();
|
||||
let cursor = crossterm.cursor();
|
||||
|
||||
let mut stdin = input.read_async().bytes();
|
||||
cursor.hide();
|
||||
|
||||
let mut input_buf = Arc::new(Mutex::new(String::new()));
|
||||
|
||||
let mut count = 0;
|
||||
|
||||
let threads = log(input_buf.clone());
|
||||
let threads = log(input_buf.clone(),&screen);
|
||||
|
||||
thread::spawn(move || {
|
||||
let t = Crossterm::new(&screen);
|
||||
let input = t.input();
|
||||
let mut stdin = input.read_async().bytes();
|
||||
|
||||
loop
|
||||
{
|
||||
let a = stdin.next();
|
||||
|
||||
match a {
|
||||
Some(Ok(b'\n')) =>
|
||||
Some(Ok(10)) =>
|
||||
{
|
||||
input_buf.lock().unwrap().clear();
|
||||
|
||||
@ -41,7 +41,8 @@ fn main() {
|
||||
}
|
||||
Some(Ok(val)) =>
|
||||
{
|
||||
input_buf.lock().unwrap().push(val as char);
|
||||
println!("{:?}",a);
|
||||
input_buf.lock().unwrap().push(a.unwrap().unwrap() as char);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
@ -49,6 +50,8 @@ fn main() {
|
||||
thread::sleep(time::Duration::from_millis(100));
|
||||
count += 1;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
for thread in threads
|
||||
{
|
||||
@ -58,18 +61,19 @@ fn main() {
|
||||
cursor.show();
|
||||
}
|
||||
|
||||
fn log(input_buf: Arc<Mutex<String>>) -> Vec<thread::JoinHandle<()>>
|
||||
fn log(input_buf: Arc<Mutex<String>>, screen: &Screen) -> Vec<thread::JoinHandle<()>>
|
||||
{
|
||||
let mut threads = Vec::with_capacity(10);
|
||||
let (_, term_height) = terminal(&Screen::default()).terminal_size();
|
||||
|
||||
for i in 0..10
|
||||
let (_, term_height) = terminal(&screen).terminal_size();
|
||||
|
||||
for i in 0..1
|
||||
{
|
||||
let input_buffer = input_buf.clone();
|
||||
let clone_stdout = screen.stdout.clone();
|
||||
|
||||
let join = thread::spawn( move || {
|
||||
|
||||
let crossterm = Crossterm::new(&Screen::new(true));
|
||||
let crossterm = Crossterm::new(&Screen::from(clone_stdout));
|
||||
let cursor = crossterm.cursor();
|
||||
let terminal = crossterm.terminal();
|
||||
|
||||
|
@ -78,7 +78,9 @@ fn main() {
|
||||
map.spawn_food(&free_positions, &screen);
|
||||
}
|
||||
}
|
||||
drop(screen);
|
||||
}
|
||||
|
||||
game_over_screen();
|
||||
}
|
||||
|
||||
|
@ -42,6 +42,6 @@ pub fn print_wait_screen_on_alternate_window() {
|
||||
{
|
||||
print_wait_screen(&mut alternate.screen);
|
||||
}
|
||||
|
||||
drop(screen);
|
||||
println!("Whe are back at the main screen");
|
||||
}
|
||||
|
@ -4,6 +4,9 @@ use super::{ IStateCommand};
|
||||
use kernel::unix_kernel::terminal;
|
||||
use termios::{tcsetattr, Termios, CREAD, ECHO, ICANON, TCSAFLUSH};
|
||||
|
||||
use std::sync::{Once, ONCE_INIT};
|
||||
static TERMINAL_MODE: Once = ONCE_INIT;
|
||||
|
||||
const FD_STDIN: ::std::os::unix::io::RawFd = 1;
|
||||
|
||||
use std::io::{Error, ErrorKind, Result};
|
||||
@ -55,3 +58,25 @@ impl NoncanonicalModeCommand {
|
||||
}
|
||||
}
|
||||
|
||||
// This command is used for enabling and disabling raw mode for the terminal.
|
||||
/// This command is used for enabling and disabling raw mode for the terminal.
|
||||
pub struct RawModeCommand;
|
||||
|
||||
impl RawModeCommand {
|
||||
pub fn new() -> Self {
|
||||
return RawModeCommand {}
|
||||
}
|
||||
|
||||
/// Enables raw mode.
|
||||
pub fn enable(&mut self) -> Result<()> {
|
||||
terminal::into_raw_mode();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Disables raw mode.
|
||||
pub fn disable(&mut self) -> Result<()> {
|
||||
terminal::disable_raw_mode();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -28,7 +28,7 @@ impl RawScreen {
|
||||
pub fn into_raw_mode() -> io::Result<()>
|
||||
{
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
let mut command = unix_command::NoncanonicalModeCommand::new();
|
||||
let mut command = unix_command::RawModeCommand::new();
|
||||
#[cfg(target_os = "windows")]
|
||||
let mut command = win_commands::RawModeCommand::new();
|
||||
|
||||
@ -40,9 +40,9 @@ impl RawScreen {
|
||||
pub fn disable_raw_modes() -> io::Result<()>
|
||||
{
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
let command = unix_command::NoncanonicalModeCommand::new();
|
||||
let mut command = unix_command::RawModeCommand::new();
|
||||
#[cfg(target_os = "windows")]
|
||||
let command = win_commands::RawModeCommand::new();
|
||||
let mut command = win_commands::RawModeCommand::new();
|
||||
|
||||
command.disable()?;
|
||||
return Ok(())
|
||||
|
@ -107,25 +107,52 @@ pub fn make_raw(termios: &mut Termios) {
|
||||
|
||||
static mut ORIGINAL_TERMINAL_MODE: Option<Termios> = None;
|
||||
|
||||
/// Get the current terminal mode.
|
||||
pub fn get_terminal_mode() -> io::Result<Termios> {
|
||||
extern "C" {
|
||||
pub fn tcgetattr(fd: c_int, termptr: *mut Termios) -> c_int;
|
||||
}
|
||||
unsafe {
|
||||
if let Some(original_mode) = ORIGINAL_TERMINAL_MODE
|
||||
pub fn into_raw_mode() -> io::Result<()>
|
||||
{
|
||||
return Ok(original_mode.clone())
|
||||
}
|
||||
else {
|
||||
let mut termios = mem::zeroed();
|
||||
is_true(tcgetattr(0, &mut termios))?;
|
||||
ORIGINAL_TERMINAL_MODE = Some(termios.clone());
|
||||
let tty_f;
|
||||
|
||||
return Ok(termios)
|
||||
let fd = unsafe {
|
||||
if libc::isatty(libc::STDIN_FILENO) == 1 {
|
||||
libc::STDIN_FILENO
|
||||
} else {
|
||||
tty_f = fs::File::open("/dev/tty")?;
|
||||
tty_f.as_raw_fd()
|
||||
}
|
||||
};
|
||||
|
||||
let mut termios = Termios::from_fd(fd)?;
|
||||
let original = termios.clone();
|
||||
|
||||
unsafe {
|
||||
if let None = ORIGINAL_TERMINAL_MODE
|
||||
{
|
||||
ORIGINAL_TERMINAL_MODE = Some(original.clone())
|
||||
}
|
||||
}
|
||||
|
||||
make_raw(&mut termios);
|
||||
tcsetattr(fd, TCSADRAIN, &termios)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn disable_raw_mode() -> io::Result<()>
|
||||
{
|
||||
let tty_f;
|
||||
|
||||
let fd = unsafe {
|
||||
if libc::isatty(libc::STDIN_FILENO) == 1 {
|
||||
libc::STDIN_FILENO
|
||||
} else {
|
||||
tty_f = fs::File::open("/dev/tty")?;
|
||||
tty_f.as_raw_fd()
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(original) = unsafe { ORIGINAL_TERMINAL_MODE }
|
||||
{
|
||||
tcsetattr(fd, TCSADRAIN, &original)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get the TTY device.
|
||||
|
@ -6,7 +6,6 @@ static mut HAS_BEEN_TRIED_TO_ENABLE: bool = false;
|
||||
static mut IS_ANSI_ON_WINDOWS_ENABLED: Option<bool> = None;
|
||||
static mut DOES_WINDOWS_SUPPORT_ANSI: Option<bool> = None;
|
||||
static ENABLE_ANSI: Once = ONCE_INIT;
|
||||
|
||||
use common::commands::win_commands::EnableAnsiCommand;
|
||||
use common::commands::IEnableAnsiCommand;
|
||||
|
||||
|
@ -5,6 +5,7 @@ use super::*;
|
||||
|
||||
/// This struct is an ansi escape code implementation for terminal related actions.
|
||||
pub struct AnsiTerminal;
|
||||
use cursor::TerminalCursor;
|
||||
|
||||
impl AnsiTerminal {
|
||||
pub fn new() -> AnsiTerminal {
|
||||
@ -17,6 +18,7 @@ impl ITerminal for AnsiTerminal {
|
||||
match clear_type {
|
||||
ClearType::All => {
|
||||
stdout.write_str(csi!("2J"));
|
||||
TerminalCursor::new(stdout).goto(0,0);
|
||||
}
|
||||
ClearType::FromCursorDown => {
|
||||
stdout.write_str(csi!("J"));
|
||||
|
Loading…
Reference in New Issue
Block a user