From 4e1fe5a625b3ba4eb99e811e1cf6ece8350e6fe2 Mon Sep 17 00:00:00 2001 From: Zrzka Date: Mon, 23 Sep 2019 20:40:14 +0200 Subject: [PATCH] Gate crossterm_winapi with cfg(windows) (#244) --- .travis.yml | 2 +- crossterm_winapi/Cargo.toml | 5 ++++- crossterm_winapi/examples/cw_coloring_example.rs | 10 ++++++++++ crossterm_winapi/examples/cw_console.rs | 9 +++++++++ crossterm_winapi/examples/cw_handle.rs | 8 ++++++++ crossterm_winapi/examples/cw_screen_buffer.rs | 10 ++++++++++ crossterm_winapi/src/lib.rs | 1 + 7 files changed, 43 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 86a8ce7..4a119fa 100644 --- a/.travis.yml +++ b/.travis.yml @@ -28,5 +28,5 @@ script: - rustc --version - if [ "$TRAVIS_RUST_VERSION" = "stable" ]; then cargo fmt --all -- --check; fi - 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 diff --git a/crossterm_winapi/Cargo.toml b/crossterm_winapi/Cargo.toml index 7a3a44d..7aafb22 100644 --- a/crossterm_winapi/Cargo.toml +++ b/crossterm_winapi/Cargo.toml @@ -11,5 +11,8 @@ exclude = ["target", "Cargo.lock"] readme = "README.md" edition = "2018" -[dependencies] +[target.'cfg(windows)'.dependencies] winapi = { version = "0.3.7", features = ["winbase","consoleapi","processenv", "handleapi"] } + +[package.metadata.docs.rs] +default-target = "x86_64-pc-windows-msvc" diff --git a/crossterm_winapi/examples/cw_coloring_example.rs b/crossterm_winapi/examples/cw_coloring_example.rs index 6124a8e..ef55773 100644 --- a/crossterm_winapi/examples/cw_coloring_example.rs +++ b/crossterm_winapi/examples/cw_coloring_example.rs @@ -1,7 +1,10 @@ +#[cfg(windows)] use std::io::Result; +#[cfg(windows)] use crossterm_winapi::{Console, ScreenBuffer}; +#[cfg(windows)] fn set_background_color() -> Result<()> { // background value const BLUE_BACKGROUND: u16 = 0x0010; @@ -23,6 +26,7 @@ fn set_background_color() -> Result<()> { Ok(()) } +#[cfg(windows)] fn set_foreground_color() -> Result<()> { // background value const BLUE_FOREGROUND: u16 = 0x0001; @@ -48,7 +52,13 @@ fn set_foreground_color() -> Result<()> { Ok(()) } +#[cfg(windows)] fn main() -> Result<()> { set_background_color()?; set_foreground_color() } + +#[cfg(not(windows))] +fn main() { + println!("This example is for the Windows platform only."); +} diff --git a/crossterm_winapi/examples/cw_console.rs b/crossterm_winapi/examples/cw_console.rs index 59d8502..ba3e595 100644 --- a/crossterm_winapi/examples/cw_console.rs +++ b/crossterm_winapi/examples/cw_console.rs @@ -1,7 +1,10 @@ +#[cfg(windows)] use std::io::Result; +#[cfg(windows)] use crossterm_winapi::ConsoleMode; +#[cfg(windows)] fn change_console_mode() -> Result<()> { let console_mode = ConsoleMode::new()?; @@ -12,6 +15,12 @@ fn change_console_mode() -> Result<()> { console_mode.set_mode(10) } +#[cfg(windows)] fn main() -> Result<()> { change_console_mode() } + +#[cfg(not(windows))] +fn main() { + println!("This example is for the Windows platform only."); +} diff --git a/crossterm_winapi/examples/cw_handle.rs b/crossterm_winapi/examples/cw_handle.rs index 5ce335d..2a26148 100644 --- a/crossterm_winapi/examples/cw_handle.rs +++ b/crossterm_winapi/examples/cw_handle.rs @@ -1,7 +1,10 @@ +#[cfg(windows)] use std::io::Result; +#[cfg(windows)] use crossterm_winapi::{Handle, HandleType}; +#[cfg(windows)] #[allow(unused_variables)] fn main() -> Result<()> { // see the description of the types to see what they do. @@ -20,3 +23,8 @@ fn main() -> Result<()> { Ok(()) } + +#[cfg(not(windows))] +fn main() { + println!("This example is for the Windows platform only."); +} diff --git a/crossterm_winapi/examples/cw_screen_buffer.rs b/crossterm_winapi/examples/cw_screen_buffer.rs index 2e69dd7..9d8a380 100644 --- a/crossterm_winapi/examples/cw_screen_buffer.rs +++ b/crossterm_winapi/examples/cw_screen_buffer.rs @@ -1,9 +1,12 @@ #![allow(dead_code)] +#[cfg(windows)] use std::io::Result; +#[cfg(windows)] use crossterm_winapi::ScreenBuffer; +#[cfg(windows)] fn print_screen_buffer_information() -> Result<()> { let screen_buffer = ScreenBuffer::current()?; @@ -18,6 +21,7 @@ fn print_screen_buffer_information() -> Result<()> { Ok(()) } +#[cfg(windows)] fn multiple_screen_buffers() -> Result<()> { // create new screen buffer let screen_buffer = ScreenBuffer::create(); @@ -26,6 +30,12 @@ fn multiple_screen_buffers() -> Result<()> { screen_buffer.show() } +#[cfg(windows)] fn main() -> Result<()> { print_screen_buffer_information() } + +#[cfg(not(windows))] +fn main() { + println!("This example is for the Windows platform only."); +} diff --git a/crossterm_winapi/src/lib.rs b/crossterm_winapi/src/lib.rs index af65941..180cd8e 100644 --- a/crossterm_winapi/src/lib.rs +++ b/crossterm_winapi/src/lib.rs @@ -1,3 +1,4 @@ +#![cfg(windows)] #![deny(unused_imports)] pub use self::{