89 lines
2.2 KiB
Rust
89 lines
2.2 KiB
Rust
use core::error;
|
|
use crossterm::{
|
|
cursor::Show,
|
|
event::{self, Event, KeyCode},
|
|
terminal::{disable_raw_mode, enable_raw_mode},
|
|
};
|
|
use std::io::{self, Write, stdout};
|
|
|
|
use crate::input;
|
|
|
|
pub enum ComandEvent {
|
|
//who is this
|
|
None,
|
|
Exit,
|
|
Key,
|
|
}
|
|
pub fn Comand_pars() -> Result<ComandEvent, String> {
|
|
let mut input = String::new();
|
|
io::stdin()
|
|
.read_line(&mut input)
|
|
.expect("error input scan\n");
|
|
let mut words: Vec<&str> = input.trim().split(" ").collect();
|
|
if words.len() == 0 {
|
|
return Err(String::from("Zerro lenth comand\n"));
|
|
}
|
|
|
|
match words[0] {
|
|
"q" => {
|
|
return Ok(ComandEvent::Exit);
|
|
}
|
|
"key" => {
|
|
_ = enable_raw_mode();
|
|
let mut out = stdout();
|
|
_ = crossterm::execute!(out, crossterm::cursor::Hide);
|
|
return Ok(ComandEvent::Key);
|
|
}
|
|
_ => {}
|
|
}
|
|
|
|
Ok(ComandEvent::None)
|
|
}
|
|
|
|
pub enum KeyEvent {
|
|
//KeuEvent for your input
|
|
None,
|
|
Exit,
|
|
Comand,
|
|
A,
|
|
W,
|
|
S,
|
|
D,
|
|
P,
|
|
}
|
|
pub fn Key_pars() -> Result<Option<KeyEvent>, std::io::Error> {
|
|
if let Event::Key(key) = event::read()? {
|
|
match key.code {
|
|
KeyCode::Char('q') => {
|
|
_ = disable_raw_mode();
|
|
let mut out = stdout();
|
|
_ = crossterm::execute!(out, crossterm::cursor::Show);
|
|
return Ok(Some(KeyEvent::Exit));
|
|
}
|
|
KeyCode::Char('c') => {
|
|
_ = disable_raw_mode();
|
|
let mut out = stdout();
|
|
_ = crossterm::execute!(out, crossterm::cursor::Show);
|
|
return Ok(Some(KeyEvent::Comand));
|
|
}
|
|
KeyCode::Char('a') => {
|
|
return Ok(Some(KeyEvent::A));
|
|
}
|
|
KeyCode::Char('w') => {
|
|
return Ok(Some(KeyEvent::W));
|
|
}
|
|
KeyCode::Char('s') => {
|
|
return Ok(Some(KeyEvent::S));
|
|
}
|
|
KeyCode::Char('d') => {
|
|
return Ok(Some(KeyEvent::D));
|
|
}
|
|
KeyCode::Char('p') => {
|
|
return Ok(Some(KeyEvent::P));
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
Ok(Some(KeyEvent::None))
|
|
}
|