Gate crossterm_winapi with cfg(windows) (#244)

This commit is contained in:
Zrzka 2019-09-23 20:40:14 +02:00 committed by Timon
parent 3b444d077f
commit 4e1fe5a625
7 changed files with 43 additions and 2 deletions

View File

@ -28,5 +28,5 @@ script:
- rustc --version - rustc --version
- if [ "$TRAVIS_RUST_VERSION" = "stable" ]; then cargo fmt --all -- --check; fi - if [ "$TRAVIS_RUST_VERSION" = "stable" ]; then cargo fmt --all -- --check; fi
- cargo build - cargo build
- if [ "$TRAVIS_OS_NAME" = "windows" ]; then cargo test --all -- --nocapture --test-threads 1; else cargo test --all --exclude crossterm_winapi -- --nocapture --test-threads 1; fi - cargo test --all -- --nocapture --test-threads 1
- scripts/test-examples.sh - scripts/test-examples.sh

View File

@ -11,5 +11,8 @@ exclude = ["target", "Cargo.lock"]
readme = "README.md" readme = "README.md"
edition = "2018" edition = "2018"
[dependencies] [target.'cfg(windows)'.dependencies]
winapi = { version = "0.3.7", features = ["winbase","consoleapi","processenv", "handleapi"] } winapi = { version = "0.3.7", features = ["winbase","consoleapi","processenv", "handleapi"] }
[package.metadata.docs.rs]
default-target = "x86_64-pc-windows-msvc"

View File

@ -1,7 +1,10 @@
#[cfg(windows)]
use std::io::Result; use std::io::Result;
#[cfg(windows)]
use crossterm_winapi::{Console, ScreenBuffer}; use crossterm_winapi::{Console, ScreenBuffer};
#[cfg(windows)]
fn set_background_color() -> Result<()> { fn set_background_color() -> Result<()> {
// background value // background value
const BLUE_BACKGROUND: u16 = 0x0010; const BLUE_BACKGROUND: u16 = 0x0010;
@ -23,6 +26,7 @@ fn set_background_color() -> Result<()> {
Ok(()) Ok(())
} }
#[cfg(windows)]
fn set_foreground_color() -> Result<()> { fn set_foreground_color() -> Result<()> {
// background value // background value
const BLUE_FOREGROUND: u16 = 0x0001; const BLUE_FOREGROUND: u16 = 0x0001;
@ -48,7 +52,13 @@ fn set_foreground_color() -> Result<()> {
Ok(()) Ok(())
} }
#[cfg(windows)]
fn main() -> Result<()> { fn main() -> Result<()> {
set_background_color()?; set_background_color()?;
set_foreground_color() set_foreground_color()
} }
#[cfg(not(windows))]
fn main() {
println!("This example is for the Windows platform only.");
}

View File

@ -1,7 +1,10 @@
#[cfg(windows)]
use std::io::Result; use std::io::Result;
#[cfg(windows)]
use crossterm_winapi::ConsoleMode; use crossterm_winapi::ConsoleMode;
#[cfg(windows)]
fn change_console_mode() -> Result<()> { fn change_console_mode() -> Result<()> {
let console_mode = ConsoleMode::new()?; let console_mode = ConsoleMode::new()?;
@ -12,6 +15,12 @@ fn change_console_mode() -> Result<()> {
console_mode.set_mode(10) console_mode.set_mode(10)
} }
#[cfg(windows)]
fn main() -> Result<()> { fn main() -> Result<()> {
change_console_mode() change_console_mode()
} }
#[cfg(not(windows))]
fn main() {
println!("This example is for the Windows platform only.");
}

View File

@ -1,7 +1,10 @@
#[cfg(windows)]
use std::io::Result; use std::io::Result;
#[cfg(windows)]
use crossterm_winapi::{Handle, HandleType}; use crossterm_winapi::{Handle, HandleType};
#[cfg(windows)]
#[allow(unused_variables)] #[allow(unused_variables)]
fn main() -> Result<()> { fn main() -> Result<()> {
// see the description of the types to see what they do. // see the description of the types to see what they do.
@ -20,3 +23,8 @@ fn main() -> Result<()> {
Ok(()) Ok(())
} }
#[cfg(not(windows))]
fn main() {
println!("This example is for the Windows platform only.");
}

View File

@ -1,9 +1,12 @@
#![allow(dead_code)] #![allow(dead_code)]
#[cfg(windows)]
use std::io::Result; use std::io::Result;
#[cfg(windows)]
use crossterm_winapi::ScreenBuffer; use crossterm_winapi::ScreenBuffer;
#[cfg(windows)]
fn print_screen_buffer_information() -> Result<()> { fn print_screen_buffer_information() -> Result<()> {
let screen_buffer = ScreenBuffer::current()?; let screen_buffer = ScreenBuffer::current()?;
@ -18,6 +21,7 @@ fn print_screen_buffer_information() -> Result<()> {
Ok(()) Ok(())
} }
#[cfg(windows)]
fn multiple_screen_buffers() -> Result<()> { fn multiple_screen_buffers() -> Result<()> {
// create new screen buffer // create new screen buffer
let screen_buffer = ScreenBuffer::create(); let screen_buffer = ScreenBuffer::create();
@ -26,6 +30,12 @@ fn multiple_screen_buffers() -> Result<()> {
screen_buffer.show() screen_buffer.show()
} }
#[cfg(windows)]
fn main() -> Result<()> { fn main() -> Result<()> {
print_screen_buffer_information() print_screen_buffer_information()
} }
#[cfg(not(windows))]
fn main() {
println!("This example is for the Windows platform only.");
}

View File

@ -1,3 +1,4 @@
#![cfg(windows)]
#![deny(unused_imports)] #![deny(unused_imports)]
pub use self::{ pub use self::{