Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to move cursor to (x, y) position #11

Merged
merged 1 commit into from Sep 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions examples/cursor_at.rs
@@ -0,0 +1,30 @@
extern crate console;

use std::io;
use std::thread;
use std::time::Duration;

use console::{style, Term};

fn write_chars() -> io::Result<()> {
let term = Term::stdout();
let (heigth, width) = term.size();
for x in 0..width {
for y in 0..heigth {
term.move_cursor_to(x as usize, y as usize)?;
let text = if (x + y) % 2 == 0 {
format!("{}", style(x % 10).black().on_red())
} else {
format!("{}", style(x % 10).red().on_black())
};

term.write_str(&text)?;
thread::sleep(Duration::from_micros(600));
}
}
Ok(())
}

fn main() {
write_chars().unwrap();
}
4 changes: 4 additions & 0 deletions src/common_term.rs
Expand Up @@ -18,6 +18,10 @@ pub fn move_cursor_up(out: &Term, n: usize) -> io::Result<()> {
}
}

pub fn move_cursor_to(out: &Term, x: usize, y: usize) -> io::Result<()> {
out.write_str(&format!("\x1B[{};{}H", y + 1, x + 1))
}

pub fn clear_line(out: &Term) -> io::Result<()> {
out.write_str("\r\x1b[2K")
}
Expand Down
5 changes: 5 additions & 0 deletions src/term.rs
Expand Up @@ -282,6 +282,11 @@ impl Term {
terminal_size()
}

/// Moves the cursor to `x` and `y`
pub fn move_cursor_to(&self, x: usize, y: usize) -> io::Result<()> {
move_cursor_to(self, x, y)
}

/// Moves the cursor up `n` lines
pub fn move_cursor_up(&self, n: usize) -> io::Result<()> {
move_cursor_up(self, n)
Expand Down
30 changes: 17 additions & 13 deletions src/windows_term.rs
Expand Up @@ -86,38 +86,42 @@ pub fn terminal_size() -> Option<(u16, u16)> {
}
}

pub fn move_cursor_up(out: &Term, n: usize) -> io::Result<()> {
pub fn move_cursor_to(out: &Term, x: usize, y: usize) -> io::Result<()> {
if msys_tty_on(out) {
return common_term::move_cursor_up(out, n);
return common_term::move_cursor_to(out, x, y);
}
if let Some((hand, csbi)) = get_console_screen_buffer_info(as_handle(out)) {
unsafe {
SetConsoleCursorPosition(
hand,
COORD {
X: 0,
Y: csbi.dwCursorPosition.Y - n as i16,
X: x as i16,
Y: y as i16,
},
);
}
}
Ok(())
}

pub fn move_cursor_up(out: &Term, n: usize) -> io::Result<()> {
if msys_tty_on(out) {
return common_term::move_cursor_up(out, n);
}

if let Some((hand, csbi)) = get_console_screen_buffer_info(as_handle(out)) {
move_cursor_to(out, 0, csbi.dwCursorPosition.Y as usize - n);
}
Ok(())
}

pub fn move_cursor_down(out: &Term, n: usize) -> io::Result<()> {
if msys_tty_on(out) {
return common_term::move_cursor_down(out, n);
}

if let Some((hand, csbi)) = get_console_screen_buffer_info(as_handle(out)) {
unsafe {
SetConsoleCursorPosition(
hand,
COORD {
X: 0,
Y: csbi.dwCursorPosition.Y + n as i16,
},
);
}
move_cursor_to(out, 0, csbi.dwCursorPosition.Y as usize + n);
}
Ok(())
}
Expand Down