fixed examples 0.3.0

This commit is contained in:
TimonPost 2018-07-18 22:32:17 +02:00
parent 66f2d04d5c
commit f4d2ab4feb
11 changed files with 83 additions and 59 deletions

View File

@ -10,12 +10,15 @@
//! ``` //! ```
//! //!
//! - Run program with: `cargo run` //! - Run program with: `cargo run`
extern crate crossterm; //extern crate crossterm;
mod color; // mod terminal;
mod cursor; // mod color;
mod terminal; // mod cursor;
// mod crossterm_type;
fn main() { fn main()
// some code to try out the examples. {
} }

View File

@ -11,7 +11,7 @@ use self::crossterm::Context;
/// print some red font | demonstration. /// print some red font | demonstration.
pub fn paint_foreground() { pub fn paint_foreground() {
let context = Context::new(); let context = Context::new();
let terminal = terminal::terminal(context.clone()); let terminal = terminal::terminal(&context);
// Pass an string to the `paint()` method with you want to paint. // Pass an string to the `paint()` method with you want to paint.
// This will give you an object back wits can be styled and displayed. // This will give you an object back wits can be styled and displayed.
@ -28,7 +28,7 @@ pub fn paint_foreground() {
/// print some font on red background | demonstration. /// print some font on red background | demonstration.
pub fn paint_background() { pub fn paint_background() {
let context = Context::new(); let context = Context::new();
let terminal = terminal::terminal(context.clone()); let terminal = terminal::terminal(&context);
// Pass an string to the `paint()` method with you want to paint. // Pass an string to the `paint()` method with you want to paint.
// This will give you an object back wits can be styled and displayed. // This will give you an object back wits can be styled and displayed.
@ -45,7 +45,7 @@ pub fn paint_background() {
/// print font with fore- background color | demonstration. /// print font with fore- background color | demonstration.
pub fn paint_foreground_and_background() { pub fn paint_foreground_and_background() {
let context = Context::new(); let context = Context::new();
let terminal = terminal::terminal(context.clone()); let terminal = terminal::terminal(&context);
// Pass an string to the `paint()` method with you want to paint. // Pass an string to the `paint()` method with you want to paint.
// This will give you an object back wits can be styled and displayed. // This will give you an object back wits can be styled and displayed.
@ -76,7 +76,7 @@ pub fn paint_foreground_and_background() {
/// Print all available foreground colors | demonstration. /// Print all available foreground colors | demonstration.
pub fn print_all_foreground_colors() { pub fn print_all_foreground_colors() {
let context = Context::new(); let context = Context::new();
let terminal = terminal::terminal(context.clone()); let terminal = terminal::terminal(&context);
println!("Black : \t {}", terminal.paint("").with(Color::Black)); println!("Black : \t {}", terminal.paint("").with(Color::Black));
println!("Red : \t\t {}", terminal.paint("").with(Color::Red)); println!("Red : \t\t {}", terminal.paint("").with(Color::Red));
@ -119,7 +119,7 @@ pub fn print_all_foreground_colors() {
/// Print all available foreground colors | demonstration. /// Print all available foreground colors | demonstration.
pub fn print_all_background_colors() { pub fn print_all_background_colors() {
let context = Context::new(); let context = Context::new();
let terminal = terminal::terminal(context.clone()); let terminal = terminal::terminal(&context);
println!("Black : \t {}", terminal.paint(" ").on(Color::Black)); println!("Black : \t {}", terminal.paint(" ").on(Color::Black));
println!("Red : \t\t {}", terminal.paint(" ").on(Color::Red)); println!("Red : \t\t {}", terminal.paint(" ").on(Color::Red));
@ -171,7 +171,7 @@ pub fn print_all_background_colors() {
#[cfg(unix)] #[cfg(unix)]
pub fn print_font_with_attributes() { pub fn print_font_with_attributes() {
let context = Context::new(); let context = Context::new();
let terminal = terminal::terminal(context.clone()); let terminal = terminal::terminal(&context);
println!("{}", terminal.paint("Normal text")); println!("{}", terminal.paint("Normal text"));
println!("{}", terminal.paint("Bold text").bold()); println!("{}", terminal.paint("Bold text").bold());
@ -189,9 +189,9 @@ pub fn print_font_with_attributes() {
#[cfg(unix)] #[cfg(unix)]
pub fn print_supported_colors() { pub fn print_supported_colors() {
let context = Context::new(); let context = Context::new();
let terminal = terminal::terminal(context.clone()); let terminal = terminal::terminal(&context);
let count = crossterm::style::color(context.clone()) let count = crossterm::style::color(&context)
.get_available_color_count() .get_available_color_count()
.unwrap(); .unwrap();

View File

@ -11,7 +11,7 @@ pub fn goto() {
let context = Context::new(); let context = Context::new();
// Get the cursor // Get the cursor
let mut cursor = cursor(context.clone()); let mut cursor = cursor(&context);
// Set the cursor to position X: 10, Y: 5 in the terminal // Set the cursor to position X: 10, Y: 5 in the terminal
cursor.goto(10, 5); cursor.goto(10, 5);
} }
@ -21,7 +21,7 @@ pub fn pos() {
let context = Context::new(); let context = Context::new();
// Get the cursor // Get the cursor
let mut cursor = cursor(context.clone()); let mut cursor = cursor(&context);
// get the cursor position. // get the cursor position.
let (x, y) = cursor.pos(); let (x, y) = cursor.pos();
} }
@ -31,9 +31,10 @@ pub fn move_up() {
let context = Context::new(); let context = Context::new();
// Get the cursor // Get the cursor
let mut cursor = cursor(context.clone()); let mut cursor = cursor(&context);
// Move the cursor to position 3 times to the up in the terminal // Move the cursor to position 3 times to the up in the terminal
cursor.move_up(3); cursor.move_up(10);
} }
/// Move the cursor 3 to the right | demonstration. /// Move the cursor 3 to the right | demonstration.
@ -41,7 +42,7 @@ pub fn move_right() {
let context = Context::new(); let context = Context::new();
// Get the cursor // Get the cursor
let mut cursor = cursor(context.clone()); let mut cursor = cursor(&context);
// Move the cursor to position 3 times to the right in the terminal // Move the cursor to position 3 times to the right in the terminal
cursor.move_right(3); cursor.move_right(3);
} }
@ -51,7 +52,7 @@ pub fn move_down() {
let context = Context::new(); let context = Context::new();
// Get the cursor // Get the cursor
let mut cursor = cursor(context.clone()); let mut cursor = cursor(&context);
// Move the cursor to position 3 times to the down in the terminal // Move the cursor to position 3 times to the down in the terminal
cursor.move_down(3); cursor.move_down(3);
} }
@ -61,7 +62,7 @@ pub fn move_left() {
let context = Context::new(); let context = Context::new();
// Get the cursor // Get the cursor
let mut cursor = cursor(context.clone()); let mut cursor = cursor(&context);
// Move the cursor to position 3 times to the left in the terminal // Move the cursor to position 3 times to the left in the terminal
cursor.move_left(3); cursor.move_left(3);
} }
@ -73,7 +74,7 @@ pub fn print() {
// To print an some displayable content on an certain position. // To print an some displayable content on an certain position.
// Get the cursor // Get the cursor
let mut cursor = cursor(context.clone()); let mut cursor = cursor(&context);
// Set the cursor to position X: 10, Y: 5 in the terminal // Set the cursor to position X: 10, Y: 5 in the terminal
cursor.goto(10, 5); cursor.goto(10, 5);
// Print the @ symbol at position X: 10, Y: 5 in the terminal // Print the @ symbol at position X: 10, Y: 5 in the terminal
@ -96,7 +97,7 @@ pub fn print() {
pub fn safe_and_reset_position() { pub fn safe_and_reset_position() {
let context = Context::new(); let context = Context::new();
let mut cursor = cursor(context.clone()); let mut cursor = cursor(&context);
// Goto X: 5 Y: 5 // Goto X: 5 Y: 5
cursor.goto(5, 5); cursor.goto(5, 5);
@ -118,7 +119,7 @@ pub fn safe_and_reset_position() {
pub fn hide_cursor() { pub fn hide_cursor() {
let context = Context::new(); let context = Context::new();
let cursor = cursor(context.clone()); let cursor = cursor(&context);
cursor.hide(); cursor.hide();
} }
@ -126,7 +127,7 @@ pub fn hide_cursor() {
pub fn show_cursor() { pub fn show_cursor() {
let context = Context::new(); let context = Context::new();
let cursor = cursor(context.clone()); let cursor = cursor(&context);
cursor.show(); cursor.show();
} }
@ -134,7 +135,7 @@ pub fn show_cursor() {
pub fn blink_cursor() { pub fn blink_cursor() {
let context = Context::new(); let context = Context::new();
let cursor = cursor(context.clone()); let cursor = cursor(&context);
cursor.blink(false); cursor.blink(false);
cursor.blink(false); cursor.blink(false);
} }

View File

@ -11,10 +11,10 @@ use std::rc::Rc;
use std::{thread, time}; use std::{thread, time};
fn print_wait_screen(context: Rc<Context>) { fn print_wait_screen(context: Rc<Context>) {
let mut terminal = terminal::terminal(context.clone()); let mut terminal = terminal::terminal(&context);
terminal.clear(ClearType::All); terminal.clear(ClearType::All);
let mut cursor = cursor(context.clone()); let mut cursor = cursor(&context);
cursor.goto(0, 0); cursor.goto(0, 0);
cursor.hide(); cursor.hide();
@ -56,7 +56,7 @@ pub fn print_wait_screen_on_alternate_window(context: Rc<Context>) {
/// some stress test switch from and to alternate screen. /// some stress test switch from and to alternate screen.
pub fn switch_between_main_and_alternate_screen() { pub fn switch_between_main_and_alternate_screen() {
let context = Context::new(); let context = Context::new();
let mut cursor = cursor(context.clone()); let mut cursor = cursor(&context);
{ {
// create new alternate screen instance and switch to the alternate screen. // create new alternate screen instance and switch to the alternate screen.

View File

@ -1,10 +1,8 @@
/// Examples of actions that could be performed on the alternatescreen. /// Examples of actions that could be performed on the alternatescreen.
/// !! Note that alternate screen only works on Unix and windows 10 systems. I am working on windows 7 support. !!
pub mod alternate_screen; pub mod alternate_screen;
/// Examples of actions that could be performed on the terminal. /// Examples of actions that could be performed on the terminal.
pub mod terminal; pub mod terminal;
/// Alternate screen is only supported for unix systems. Windows support will come later :). // Raw screen
#[cfg(target_os = "unix")]
pub mod raw_mode; pub mod raw_mode;

View File

@ -13,9 +13,9 @@ use crossterm::raw::IntoRawMode;
// raw screen is not working correctly currently // raw screen is not working correctly currently
fn print_wait_screen(context: Rc<Context>) { fn print_wait_screen(context: Rc<Context>) {
terminal::terminal(context.clone()).clear(ClearType::All); terminal::terminal(&context).clear(ClearType::All);
let mut cursor = cursor(context.clone()); let mut cursor = cursor(&context);
cursor.goto(0, 0).print("Welcome to the wait screen."); cursor.goto(0, 0).print("Welcome to the wait screen.");
cursor cursor
.goto(0, 1) .goto(0, 1)
@ -44,7 +44,7 @@ pub fn print_wait_screen_on_alternate_window() {
// then convert the output of the program to raw mode. // then convert the output of the program to raw mode.
// then print the wait screen on the alternate screen in raw mode. // then print the wait screen on the alternate screen in raw mode.
let mut screen = AlternateScreen::from(context.clone()); let mut screen = AlternateScreen::from(context.clone());
let alternate_screen = screen.into_raw_mode(context.clone()); let raw_screen = screen.into_raw_mode(context.clone());
// Print the wait screen. // Print the wait screen.
print_wait_screen(context.clone()); print_wait_screen(context.clone());

View File

@ -19,7 +19,7 @@ pub fn clear_all_lines() {
let context = Context::new(); let context = Context::new();
// Get terminal // Get terminal
let mut terminal = terminal(context); let mut terminal = terminal(&context);
print_test_data(); print_test_data();
@ -32,12 +32,12 @@ pub fn clear_from_cursor_down() {
let context = Context::new(); let context = Context::new();
// Get terminal // Get terminal
let mut terminal = terminal(context.clone()); let mut terminal = terminal(&context);
print_test_data(); print_test_data();
// Set terminal cursor position (see example for more info). // Set terminal cursor position (see example for more info).
cursor::cursor(context.clone()).goto(4, 8); cursor::cursor(&context).goto(4, 8);
// Clear all cells from current cursor position down. // Clear all cells from current cursor position down.
terminal.clear(ClearType::FromCursorDown); terminal.clear(ClearType::FromCursorDown);
@ -48,12 +48,12 @@ pub fn clear_from_cursor_up() {
let context = Context::new(); let context = Context::new();
// Get terminal // Get terminal
let mut terminal = terminal(context.clone()); let mut terminal = terminal(&context);
print_test_data(); print_test_data();
// Set terminal cursor position (see example for more info). // Set terminal cursor position (see example for more info).
cursor::cursor(context.clone()).goto(4, 4); cursor::cursor(&context).goto(4, 4);
// Clear all cells from current cursor position down. // Clear all cells from current cursor position down.
terminal.clear(ClearType::FromCursorUp); terminal.clear(ClearType::FromCursorUp);
@ -64,12 +64,12 @@ pub fn clear_current_line() {
let context = Context::new(); let context = Context::new();
// Get terminal // Get terminal
let mut terminal = terminal(context.clone()); let mut terminal = terminal(&context);
print_test_data(); print_test_data();
// Set terminal cursor position (see example for more info). // Set terminal cursor position (see example for more info).
cursor::cursor(context.clone()).goto(4, 4); cursor::cursor(&context).goto(4, 4);
// Clear current line cells. // Clear current line cells.
terminal.clear(ClearType::CurrentLine); terminal.clear(ClearType::CurrentLine);
@ -80,12 +80,12 @@ pub fn clear_until_new_line() {
let context = Context::new(); let context = Context::new();
// Get terminal // Get terminal
let mut terminal = terminal(context.clone()); let mut terminal = terminal(&context);
print_test_data(); print_test_data();
// Set terminal cursor position (see example for more info). // Set terminal cursor position (see example for more info).
cursor::cursor(context.clone()).goto(4, 20); cursor::cursor(&context).goto(4, 20);
// Clear all the cells until next line. // Clear all the cells until next line.
terminal.clear(ClearType::UntilNewLine); terminal.clear(ClearType::UntilNewLine);
@ -96,7 +96,7 @@ pub fn print_terminal_size() {
let context = Context::new(); let context = Context::new();
// Get terminal // Get terminal
let mut terminal = terminal(context.clone()); let mut terminal = terminal(&context);
// Get terminal size // Get terminal size
let terminal_size = terminal.terminal_size(); let terminal_size = terminal.terminal_size();
// Print results // Print results
@ -107,7 +107,7 @@ pub fn print_terminal_size() {
pub fn set_terminal_size() { pub fn set_terminal_size() {
let context = Context::new(); let context = Context::new();
let mut terminal = terminal(context); let mut terminal = terminal(&context);
terminal.set_size(10, 10); terminal.set_size(10, 10);
} }
@ -117,8 +117,11 @@ pub fn scroll_down() {
let context = Context::new(); let context = Context::new();
print_test_data(); print_test_data();
// Get terminal // Get terminal
let mut terminal = terminal(context.clone()); let mut terminal = terminal(&context);
// Scroll down 10 lines. // Scroll down 10 lines.
terminal.scroll_down(10); terminal.scroll_down(10);
} }
@ -130,9 +133,9 @@ pub fn scroll_up() {
print_test_data(); print_test_data();
// Get terminal // Get terminal
let mut terminal = terminal(context.clone()); let mut terminal = terminal(&context);
// Scroll up 10 lines. // Scroll up 10 lines.
terminal.scroll_up(10); terminal.scroll_up(5);
} }
/// Resize the terminal to X: 10, Y: 10 | demonstration. /// Resize the terminal to X: 10, Y: 10 | demonstration.
@ -140,7 +143,8 @@ pub fn resize_terminal() {
let context = Context::new(); let context = Context::new();
// Get terminal // Get terminal
let mut terminal = terminal(context.clone()); let mut terminal = terminal(&context);
// Get terminal size // Get terminal size
terminal.set_size(10, 10); terminal.set_size(10, 10);
} }
@ -150,7 +154,7 @@ pub fn exit() {
let context = Context::new(); let context = Context::new();
// Get terminal // Get terminal
let mut terminal = terminal(context.clone()); let mut terminal = terminal(&context);
// Get terminal size // Get terminal size
terminal.exit(); terminal.exit();
} }

View File

@ -104,7 +104,7 @@ pub fn get_console_screen_buffer_info(
let mut csbi = CONSOLE_SCREEN_BUFFER_INFO::empty(); let mut csbi = CONSOLE_SCREEN_BUFFER_INFO::empty();
let success; let success;
unsafe { success = GetConsoleScreenBufferInfo(get_output_handle(), &mut csbi) } unsafe { success = GetConsoleScreenBufferInfo(get_current_handle(screen_manager), &mut csbi) }
if success == 0 { if success == 0 {
panic!("Cannot get console screen buffer info"); panic!("Cannot get console screen buffer info");

View File

@ -54,8 +54,9 @@ pub fn get_module<T>(winapi_impl: T, unix_impl: T) -> Option<T> {
use kernel::windows_kernel::ansi_support::try_enable_ansi_support; use kernel::windows_kernel::ansi_support::try_enable_ansi_support;
// Try to enable ansi on windows if not than use WINAPI. // Try to enable ansi on windows if not than use WINAPI.
does_support = try_enable_ansi_support(); // does_support = try_enable_ansi_support();
does_support = false;
if !does_support { if !does_support {
term = Some(winapi_impl); term = Some(winapi_impl);
} }

View File

@ -48,6 +48,7 @@ impl ITerminal for AnsiTerminal {
fn scroll_up(&self, count: i16) { fn scroll_up(&self, count: i16) {
let mut screen = self.context.screen_manager.lock().unwrap(); let mut screen = self.context.screen_manager.lock().unwrap();
{ {
panic!();
screen.write_string(format!(csi!("{}S"), count)); screen.write_string(format!(csi!("{}S"), count));
} }
} }

View File

@ -41,23 +41,39 @@ impl ITerminal for WinApiTerminal {
} }
fn scroll_up(&self, count: i16) { fn scroll_up(&self, count: i16) {
// yet to be implemented let csbi = kernel::get_console_screen_buffer_info(&self.context.screen_manager);
// Set srctWindow to the current window size and location.
let mut srct_window = csbi.srWindow;
// Check whether the window is too close to the screen buffer top
if srct_window.Top >= count {
srct_window.Top -= count; // move top down
srct_window.Bottom = count; // move bottom down
let success = kernel::set_console_info(false, &mut srct_window, &self.context.screen_manager);
if success {
panic!("Something went wrong when scrolling down");
}
}
} }
fn scroll_down(&self, count: i16) { fn scroll_down(&self, count: i16) {
let csbi = kernel::get_console_screen_buffer_info(&self.context.screen_manager);
let mut srct_window;
let csbi = kernel::get_console_screen_buffer_info(&self.context.screen_manager);
// Set srctWindow to the current window size and location. // Set srctWindow to the current window size and location.
srct_window = csbi.srWindow; let mut srct_window = csbi.srWindow;
panic!("window top: {} , window bottom: {} | {}, {}", srct_window.Top, srct_window.Bottom, csbi.dwSize.Y, csbi.dwSize.X);
// Check whether the window is too close to the screen buffer top // Check whether the window is too close to the screen buffer top
if srct_window.Bottom < csbi.dwSize.Y - count { if srct_window.Bottom < csbi.dwSize.Y - count {
srct_window.Top += count; // move top down srct_window.Top += count; // move top down
srct_window.Bottom += count; // move bottom down srct_window.Bottom += count; // move bottom down
let success = let success = kernel::set_console_info(false, &mut srct_window, &self.context.screen_manager);
kernel::set_console_info(true, &mut srct_window, &self.context.screen_manager);
if success { if success {
panic!("Something went wrong when scrolling down"); panic!("Something went wrong when scrolling down");
} }