more struct building + testing

This commit is contained in:
Maddie 2022-11-05 09:57:41 +00:00
parent 9a9c14bc0f
commit 09d26ec97e
2 changed files with 49 additions and 6 deletions

View File

@ -5,14 +5,14 @@ mod editor;
fn main() {
let lambda = editor::Editor::new();
let _term = terminal::Terminal::new();
let mut term = terminal::Terminal::new().unwrap();
loop {
print!("{esc}[2J{esc}[1;1H", esc = 27 as char);
for line in lambda.buffer.data {
terminal::Terminal::write(format!("{line}"));
mut term.cursor.move_to(0, 1);
};
std::thread::sleep(Duration::from_millis(3000));
std::thread::sleep(Duration::from_secs(3));
break;
};
terminal::Terminal::exit()
terminal::Terminal::exit();
}

View File

@ -1,28 +1,65 @@
use crossterm::terminal;
use crossterm::{execute, ErrorKind};
use crossterm::style::Print;
use crossterm::cursor::{CursorShape, MoveTo};
use std::io::{stdout, Write};
#[derive(Debug)]
pub struct Size {
pub width: usize,
pub height: usize,
}
#[derive(Debug)]
pub struct Coords {
pub x: usize,
pub y: usize,
}
impl Coords {
pub fn from(x: usize, y: usize) -> Self {
Self {
x: 0,
y: 0,
}
}
}
pub struct Cursor {
pub position: Coords,
pub shape: CursorShape,
}
impl Cursor {
pub fn new() -> Result<Self, ErrorKind> {
let cursor = Self {
position: Coords::from(0, 0),
shape: CursorShape::Block,
};
Cursor::move_to(&mut cursor, 0, 0);
Ok(cursor)
}
pub fn move_to(&mut self, x: u16, y: u16) {
self.position = Coords::from(x as usize, y as usize);
execute!(stdout(), MoveTo(x, y)).unwrap()
}
}
pub struct Terminal {
pub size: Size,
pub cursor: Cursor,
}
impl Terminal {
pub fn new() -> Result<Self, ErrorKind> {
let size = terminal::size()?;
Terminal::clear();
Terminal::enter();
Ok(Self {
size: Size {
width: size.0 as usize,
height: size.1 as usize,
},
cursor: Cursor::new().unwrap(),
})
}
@ -39,6 +76,12 @@ impl Terminal {
}
pub fn write(text: String) {
// Writes a line to a current cursor position
execute!(stdout(), Print(text)).unwrap();
}
pub fn clear() {
// Clears the terminal screen
execute!(stdout(), terminal::Clear(terminal::ClearType::All)).unwrap();
}
}