Clear until nextline and currentline windows functionality added

This commit is contained in:
T 2018-01-07 15:26:20 +01:00
parent 3b4b6fb11a
commit 37670c7fbb
9 changed files with 96 additions and 54 deletions

View File

@ -21,21 +21,11 @@ fn main() {
let mut curs = cursor::get();
{
curs.goto(10, 10);
curs.print("@");
curs.move_up(1);
curs.print("1");
curs.goto(4, 1).print("@");
curs.move_right(1);
curs.print("2");
curs.move_down(1);
curs.print("3");
curs.move_left(2);
curs.print("4");
curs.goto(0, 30);
println!("{:?}", curs.pos());
}
terminal::get().clear(ClearType::UntilNewLine);
cursor::get().goto(0,30);
}

View File

@ -13,20 +13,20 @@ pub enum CursorDirection {
}
/// Set the cursor position to an coordinate (x,y).
pub fn set(x: u16, y: u16) {
pub fn set(x: i16, y: i16) {
set_cursor_pos(x as i16, y as i16);
}
/// Get the current cursor x position.
pub fn xpos() -> u16 {
pub fn xpos() -> i16 {
let csbi = kernel::get_console_screen_buffer_info();
csbi.dwCursorPosition.X as u16
csbi.dwCursorPosition.X
}
/// Get the current cursor y position.
pub fn ypos() -> u16 {
pub fn ypos() -> i16 {
let csbi = kernel::get_console_screen_buffer_info();
csbi.dwCursorPosition.Y as u16
csbi.dwCursorPosition.Y
}
pub fn move_down(count: u16) {

View File

@ -44,7 +44,7 @@ pub fn clear_after_cursor() {
let mut y = cursor::ypos() as i16;
// if cursor position is at the outer right position
if x > csbi.srWindow.Right
if x > csbi.dwSize.X
{
y += 1;
x = 0;
@ -52,22 +52,29 @@ pub fn clear_after_cursor() {
// location where to start clearing
let start_loaction = winapi::COORD { X: x, Y: y };
clear(output_handle, csbi, start_loaction);
// get sum cells before cursor
let cells_to_write = csbi.dwSize.X as u32 * csbi.dwSize.Y as u32;
clear(output_handle, csbi, start_loaction,cells_to_write);
}
// pub fn before_after_cursor() {
// let output_handle = handle::get_output_handle();
// let csbi = kernel::get_console_screen_buffer_info();
pub fn clear_before_cursor() {
let output_handle = handle::get_output_handle();
let csbi = kernel::get_console_screen_buffer_info();
// // one cell after cursor position
// let x = cursor::xpos() as i16 - 1;
// // one at row of cursor position
// let y = cursor::ypos() as i16;
// one cell after cursor position
let x = 0;
// one at row of cursor position
let y = 0;
// // location where to start clearing
// let start_loaction = winapi::COORD { X: x, Y: y };
// clear(output_handle, csbi, start_loaction);
// }
// location where to start clearing
let start_loaction = winapi::COORD { X: x, Y: y };
// get sum cells before cursor
let cells_to_write = (csbi.dwSize.X as u32 * cursor::ypos() as u32) + (cursor::xpos() -1) as u32;
// println!("{:?}", (csbi.dwSize.X as u32 * (cursor::ypos() - 1) as u32));
clear(output_handle, csbi, start_loaction, cells_to_write);
}
pub fn clear_entire_screen() {
let output_handle = handle::get_output_handle();
@ -80,28 +87,74 @@ pub fn clear_entire_screen() {
// location where to start clearing
let start_loaction = winapi::COORD { X: x, Y: y };
// get sum cells before cursor
clear(output_handle, csbi, start_loaction);
let cells_to_write = csbi.dwSize.X as u32 * csbi.dwSize.Y as u32;
clear(output_handle, csbi, start_loaction, cells_to_write);
// put the cursor back at (0, 0)
cursor::set(0, 0);
}
pub fn clear_current_line()
{
let output_handle = handle::get_output_handle();
let csbi = kernel::get_console_screen_buffer_info();
// position x at start
let x = 0;
// position y at start
let y = cursor::ypos();
// location where to start clearing
let start_loaction = winapi::COORD { X: x, Y: y };
// get sum cells before cursor
let cells_to_write = csbi.dwSize.X as u32;
clear(output_handle, csbi, start_loaction, cells_to_write);
// put the cursor back at (0, 0)
cursor::set(x, y);
}
pub fn clear_until_line()
{
let output_handle = handle::get_output_handle();
let csbi = kernel::get_console_screen_buffer_info();
// position x at start
let x = cursor::xpos();
// position y at start
let y = cursor::ypos();
// location where to start clearing
let start_loaction = winapi::COORD { X: x -1, Y: y };
// get sum cells before cursor
let cells_to_write = (csbi.dwSize.X - x) as u32 - 1;
clear(output_handle, csbi, start_loaction, cells_to_write);
print!("{:?}",start_loaction);
// put the cursor back at (0, 0)
cursor::set(x, y);
}
fn clear(
handle: winapi::HANDLE,
csbi: winapi::CONSOLE_SCREEN_BUFFER_INFO,
start_loaction: winapi::COORD,
cells_to_write: u32
) {
let console_size = (csbi.dwSize.X as u32 * csbi.dwSize.Y as u32) as u32;
let mut cells_written = 0;
let mut success;
unsafe {
// fill the entire screen with blanks
// fill the cetain cells in console with blanks
success = kernel32::FillConsoleOutputCharacterA(
handle,
' ' as i8,
console_size,
cells_to_write,
start_loaction,
&mut cells_written,
);
@ -117,7 +170,7 @@ fn clear(
success = kernel32::FillConsoleOutputAttribute(
handle,
csbi.wAttributes,
console_size,
cells_to_write,
start_loaction,
&mut cells_written,
);

View File

@ -18,10 +18,9 @@ impl ITerminal for WinApiTerminal {
{
ClearType::All => terminal::clear_entire_screen(),
ClearType::AfterCursor => terminal::clear_after_cursor(),
_ => print!("")
// ClearType::BeforeCursor => format!(csi!("1J")),
// ClearType::CurrentLine => format!(csi!("2K")),
// ClearType::UntilNewLine => format!(csi!("K")),
ClearType::BeforeCursor => terminal::clear_before_cursor(),
ClearType::CurrentLine => terminal::clear_current_line(),
ClearType::UntilNewLine => terminal::clear_until_line(),
};
}

View File

@ -15,7 +15,7 @@ impl ITerminalCursor for AnsiCursor {
format!(csi!("{};{}H"), x, y);
}
fn pos(&self) -> (u16, u16) {
fn pos(&self) -> (i16, i16) {
(0, 0)
}

View File

@ -9,7 +9,7 @@
pub trait ITerminalCursor {
/// Goto some location (x,y) in the terminal.
fn goto(&self, x: u16, y: u16);
fn pos(&self) -> (u16, u16);
fn pos(&self) -> (i16, i16);
fn move_up(&self, count: u16);
fn move_right(&self, count: u16);
fn move_down(&self, count: u16);

View File

@ -42,7 +42,7 @@ impl TerminalCursor {
self
}
pub fn pos(mut self) -> (u16, u16) {
pub fn pos(mut self) -> (i16, i16) {
&self.init();
if let Some(ref terminal_cursor) = self.terminal_cursor {
terminal_cursor.pos()

View File

@ -14,7 +14,7 @@ impl Construct for NoCursor {
impl ITerminalCursor for NoCursor {
fn goto(&self, x: u16, y: u16) {}
fn pos(&self) -> (u16, u16) {
fn pos(&self) -> (i16, i16) {
(0, 0)
}

View File

@ -15,10 +15,10 @@ impl Construct for WinApiCursor {
impl ITerminalCursor for WinApiCursor {
fn goto(&self, x: u16, y: u16) {
cursor::set(x, y);
cursor::set(x as i16, y as i16);
}
fn pos(&self) -> (u16, u16) {
fn pos(&self) -> (i16, i16) {
(cursor::xpos(), cursor::ypos())
}
@ -26,27 +26,27 @@ impl ITerminalCursor for WinApiCursor {
let xpos = cursor::xpos();
let ypos = cursor::ypos();
cursor::set(xpos, ypos - 1);
cursor::set(xpos, ypos - count as i16);
}
fn move_right(&self, count: u16) {
let xpos = cursor::xpos();
let ypos = cursor::ypos();
cursor::set(xpos + 1, ypos);
cursor::set(xpos + count as i16, ypos);
}
fn move_down(&self, count: u16) {
let xpos = cursor::xpos();
let ypos = cursor::ypos();
cursor::set(xpos, ypos + count);
cursor::set(xpos, ypos + count as i16);
}
fn move_left(&self, count: u16) {
let xpos = cursor::xpos();
let ypos = cursor::ypos();
cursor::set(xpos - 1, ypos);
cursor::set(xpos - count as i16, ypos);
}
}