Compare commits
2
Commits
render
..
b09fa9e2e6
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b09fa9e2e6 | ||
|
|
88f5f7f61a |
No files matched your search
@@ -4,3 +4,4 @@ version = "0.1.0"
|
|||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
crossterm = "0.29"
|
||||||
@@ -1,71 +0,0 @@
|
|||||||
use std::{
|
|
||||||
error,
|
|
||||||
io::{Stdout, Write, stdout},
|
|
||||||
process::Output,
|
|
||||||
};
|
|
||||||
|
|
||||||
use crossterm::{
|
|
||||||
cursor::MoveTo,
|
|
||||||
execute, queue,
|
|
||||||
style::{Color, Print, ResetColor, SetBackgroundColor, SetForegroundColor},
|
|
||||||
terminal::{Clear, ClearType},
|
|
||||||
};
|
|
||||||
|
|
||||||
pub struct Render {
|
|
||||||
out: Stdout,
|
|
||||||
rez: (usize, usize),
|
|
||||||
pos: (usize, usize),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
Render {
|
|
||||||
out: stdout(),
|
|
||||||
rez: (10, 10),
|
|
||||||
pos: (0, 0),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn set_rez(&mut self, a: usize, b: usize) -> Result<(), String> {
|
|
||||||
if a == 0 || b == 0 {
|
|
||||||
return Err("Invalid resolution\n".to_string());
|
|
||||||
}
|
|
||||||
self.rez = (a, b);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
pub fn set_pos(&mut self, a: usize, b: usize) {
|
|
||||||
self.pos = (a, b);
|
|
||||||
}
|
|
||||||
pub fn init(&mut self) {
|
|
||||||
execute!(self.out, Clear(ClearType::All), MoveTo(0, 0));
|
|
||||||
}
|
|
||||||
pub fn print(&mut self, out_str: &str) {
|
|
||||||
execute!(self.out, Print(out_str));
|
|
||||||
}
|
|
||||||
pub fn print_err(&mut self, out_err: &str) {
|
|
||||||
execute!(
|
|
||||||
self.out,
|
|
||||||
SetForegroundColor(Color::Red),
|
|
||||||
Print(out_err),
|
|
||||||
Print("\n"),
|
|
||||||
ResetColor
|
|
||||||
);
|
|
||||||
}
|
|
||||||
pub fn frame(&mut self) {
|
|
||||||
for _i in 0..self.rez.0 {
|
|
||||||
for _j in 0..self.rez.1 {
|
|
||||||
queue!(
|
|
||||||
self.out,
|
|
||||||
SetBackgroundColor(Color::Rgb {
|
|
||||||
r: 100,
|
|
||||||
g: 30,
|
|
||||||
b: 90
|
|
||||||
}),
|
|
||||||
Print(" "),
|
|
||||||
ResetColor
|
|
||||||
);
|
|
||||||
}
|
|
||||||
queue!(self.out, Print("\r\n"));
|
|
||||||
}
|
|
||||||
self.out.flush();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
use core::error;
|
||||||
|
use crossterm::{
|
||||||
|
event::{self, Event, KeyCode},
|
||||||
|
terminal::{disable_raw_mode, enable_raw_mode},
|
||||||
|
};
|
||||||
|
use std::io::{self, Write};
|
||||||
|
|
||||||
|
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();
|
||||||
|
return Ok(ComandEvent::Key);
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(ComandEvent::None)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum KeyEvent {
|
||||||
|
//KeuEvent for your input
|
||||||
|
None,
|
||||||
|
Exit,
|
||||||
|
Comand,
|
||||||
|
}
|
||||||
|
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();
|
||||||
|
return Ok(Some(KeyEvent::Exit));
|
||||||
|
}
|
||||||
|
KeyCode::Char('c') => {
|
||||||
|
_ = disable_raw_mode();
|
||||||
|
return Ok(Some(KeyEvent::Comand));
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(Some(KeyEvent::None))
|
||||||
|
}
|
||||||
+50
@@ -1,3 +1,53 @@
|
|||||||
|
mod input;
|
||||||
|
use input::*;
|
||||||
|
enum Input_type {
|
||||||
|
comand,
|
||||||
|
keybard,
|
||||||
|
}
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Hello, world!");
|
println!("Hello, world!");
|
||||||
|
let mut it = Input_type::comand;
|
||||||
|
loop {
|
||||||
|
match it {
|
||||||
|
Input_type::comand => {
|
||||||
|
println!("input your comand: ");
|
||||||
|
let input = Comand_pars();
|
||||||
|
if input.is_err() {
|
||||||
|
println!("ERROR: {}", input.err().unwrap());
|
||||||
|
} else {
|
||||||
|
match input.unwrap() {
|
||||||
|
ComandEvent::Exit => {
|
||||||
|
println!("progarm is end");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
ComandEvent::Key => {
|
||||||
|
println!("jamp to keybard");
|
||||||
|
it = Input_type::keybard
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Input_type::keybard => {
|
||||||
|
let input = Key_pars();
|
||||||
|
if input.is_err() {
|
||||||
|
println!("ERROR: {}", input.err().unwrap())
|
||||||
|
} else {
|
||||||
|
if let Some(k) = input.unwrap() {
|
||||||
|
match k {
|
||||||
|
KeyEvent::Exit => {
|
||||||
|
println!("progarm is end");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
KeyEvent::Comand => {
|
||||||
|
println!("jamp to comand");
|
||||||
|
it = Input_type::comand
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user