b717d306c3
When you want to work with the 'alternate screen' you can call the following functions: terminal::from_screen etc. Which will give you an instance to the back of the module you are calling it in. So instead of: let color = color(Screen::default()); let cursor = cursor(Screen::default()); let input = input(Screen::default()); let terminal = terminal(Screen::default()); You can do: let color = color(); let cursor = cursor(); let input = input(); let terminal = terminal();
61 lines
1.1 KiB
Rust
61 lines
1.1 KiB
Rust
extern crate crossterm;
|
|
|
|
use self::crossterm::terminal::{self, ClearType};
|
|
use self::crossterm::style::{Color, StyledObject, ObjectStyle, style };
|
|
use self::crossterm::cursor;
|
|
use self::crossterm::Screen;
|
|
|
|
use std::fmt::Debug;
|
|
use std::fmt;
|
|
|
|
#[derive(Copy, Clone,Debug)]
|
|
pub enum Direction
|
|
{
|
|
Up = 0,
|
|
Down = 1,
|
|
Left = 2,
|
|
Right = 3
|
|
}
|
|
|
|
#[derive(Copy, Clone, Debug, PartialOrd, PartialEq)]
|
|
pub struct Position
|
|
{
|
|
pub x: usize,
|
|
pub y: usize
|
|
}
|
|
|
|
impl Position
|
|
{
|
|
pub fn new(x: usize, y: usize) -> Position
|
|
{
|
|
Position { x, y }
|
|
}
|
|
|
|
pub fn draw(&self, val: &str, screen: &Screen)
|
|
{
|
|
cursor::from_screen(screen).goto(self.x as u16, self.y as u16);
|
|
style(val).with(Color::Red).paint(&screen);
|
|
screen.stdout.flush();
|
|
}
|
|
|
|
pub fn remove(&self, screen: &Screen)
|
|
{
|
|
cursor::from_screen(screen).goto(self.x as u16, self.y as u16);
|
|
terminal::from_screen(&screen).write(" ");
|
|
}
|
|
}
|
|
|
|
#[derive(Copy, Clone)]
|
|
pub struct Size
|
|
{
|
|
pub width: usize,
|
|
pub height: usize
|
|
}
|
|
|
|
impl Size
|
|
{
|
|
pub fn new(width: usize, height: usize) -> Size
|
|
{
|
|
Size {width,height}
|
|
}
|
|
} |