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

alternate screen (fixes #63) #65

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
21 changes: 21 additions & 0 deletions examples/alternate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use std::io;

use console::Term;

fn main() -> io::Result<()> {
let term = Term::stdout();
let (height, width) = term.size();
term.hide_cursor()?;
term.enter_alternate_screen()?;

let s = "press any key to quit";
let x = (width as usize / 2) - (s.len() / 2);
let y = height as usize / 2;
term.move_cursor_to(x, y)?;
term.write_str(&s)?;
term.read_key()?;

term.exit_alternate_screen()?;
term.show_cursor()?;
Ok(())
}
10 changes: 10 additions & 0 deletions src/common_term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,13 @@ pub fn show_cursor(out: &Term) -> io::Result<()> {
pub fn hide_cursor(out: &Term) -> io::Result<()> {
out.write_str("\x1b[?25l")
}

#[inline]
pub fn enter_alternate_screen(out: &Term) -> io::Result<()> {
out.write_str("\x1b[?1049h")
}

#[inline]
pub fn exit_alternate_screen(out: &Term) -> io::Result<()> {
out.write_str("\x1b[?1049l")
}
12 changes: 12 additions & 0 deletions src/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,18 @@ impl Term {
hide_cursor(self)
}

/// Enter alternate screen
#[inline]
pub fn enter_alternate_screen(&self) -> io::Result<()> {
enter_alternate_screen(self)
}

/// Exit alternate screen
#[inline]
pub fn exit_alternate_screen(&self) -> io::Result<()> {
exit_alternate_screen(self)
}

// helpers

#[cfg(all(windows, feature = "windows-console-colors"))]
Expand Down
14 changes: 14 additions & 0 deletions src/windows_term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,20 @@ pub fn hide_cursor(out: &Term) -> io::Result<()> {
Ok(())
}

pub fn enter_alternate_screen(out: &Term) -> io::Result<()> {
if out.is_msys_tty {
return common_term::enter_alternate_screen(out);
}
Ok(())
}

pub fn exit_alternate_screen(out: &Term) -> io::Result<()> {
if out.is_msys_tty {
return common_term::exit_alternate_screen(out);
}
Ok(())
}

fn get_console_screen_buffer_info(hand: HANDLE) -> Option<(HANDLE, CONSOLE_SCREEN_BUFFER_INFO)> {
let mut csbi: CONSOLE_SCREEN_BUFFER_INFO = unsafe { mem::zeroed() };
match unsafe { GetConsoleScreenBufferInfo(hand, &mut csbi) } {
Expand Down