-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
74 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,75 @@ | ||
use crossterm::cursor::MoveTo; | ||
use crossterm::execute; | ||
use crossterm::cursor::{Hide, MoveTo, Show}; | ||
use crossterm::queue; | ||
use crossterm::style::Print; | ||
use crossterm::terminal::{disable_raw_mode, enable_raw_mode, size, Clear, ClearType}; | ||
use std::io::stdout; | ||
use std::io::{stdout, Error, Write}; | ||
|
||
pub struct Terminal {} | ||
#[derive(Copy, Clone)] | ||
pub struct Size { | ||
pub height: u16, | ||
pub width: u16, | ||
Check warning on line 10 in src/editor/terminal.rs
|
||
} | ||
|
||
#[derive(Copy, Clone)] | ||
pub struct Position { | ||
pub x: u16, | ||
pub y: u16, | ||
} | ||
|
||
pub struct Terminal; | ||
|
||
impl Terminal { | ||
pub fn terminate() -> Result<(), std::io::Error> { | ||
pub fn terminate() -> Result<(), Error> { | ||
Self::execute()?; | ||
disable_raw_mode()?; | ||
Ok(()) | ||
} | ||
pub fn initialize() -> Result<(), std::io::Error> { | ||
|
||
pub fn initialize() -> Result<(), Error> { | ||
enable_raw_mode()?; | ||
Self::clear_screen()?; | ||
Self::move_cursor_to(0, 0)?; | ||
Self::move_cursor_to(Position { x: 0, y: 0 })?; | ||
Self::execute()?; | ||
Ok(()) | ||
} | ||
|
||
pub fn clear_screen() -> Result<(), Error> { | ||
queue!(stdout(), Clear(ClearType::All))?; | ||
Ok(()) | ||
} | ||
pub fn clear_screen() -> Result<(), std::io::Error> { | ||
execute!(stdout(), Clear(ClearType::All))?; | ||
|
||
pub fn clear_line() -> Result<(), Error> { | ||
queue!(stdout(), Clear(ClearType::CurrentLine))?; | ||
Ok(()) | ||
} | ||
pub fn move_cursor_to(x: u16, y: u16) -> Result<(), std::io::Error> { | ||
execute!(stdout(), MoveTo(x, y))?; | ||
|
||
pub fn move_cursor_to(position: Position) -> Result<(), Error> { | ||
queue!(stdout(), MoveTo(position.x, position.y))?; | ||
Ok(()) | ||
} | ||
pub fn size() -> Result<(u16, u16), std::io::Error> { | ||
size() | ||
|
||
pub fn hide_cursor() -> Result<(), Error> { | ||
queue!(stdout(), Hide)?; | ||
Ok(()) | ||
} | ||
|
||
pub fn show_cursor() -> Result<(), Error> { | ||
queue!(stdout(), Show)?; | ||
Ok(()) | ||
} | ||
|
||
pub fn print(string: &str) -> Result<(), Error> { | ||
queue!(stdout(), Print(string))?; | ||
Ok(()) | ||
} | ||
|
||
pub fn size() -> Result<Size, Error> { | ||
let (width, height) = size()?; | ||
Ok(Size { height, width }) | ||
} | ||
|
||
pub fn execute() -> Result<(), Error> { | ||
stdout().flush()?; | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters