Skip to content

Commit 0b6d2c6

Browse files
committed
Switch to windows-sys crate, update MSRV to 1.59, add keywords and categories
This will be a breaking change since the return value is now `std::io::Error`. Also I think for a crate like this, an MSRV change is a breaking one.
1 parent 32bd719 commit 0b6d2c6

File tree

8 files changed

+80
-27
lines changed

8 files changed

+80
-27
lines changed

.github/dependabot.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Reference:
2+
# https://docs.github.com/en/github/administering-a-repository/configuration-options-for-dependency-updates
3+
4+
version: 2
5+
updates:
6+
- package-ecosystem: "cargo"
7+
directory: "/"
8+
schedule:
9+
interval: "daily"
10+
allow:
11+
- dependency-type: all

.github/workflows/ci.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@ name: CI
1111
jobs:
1212
lint:
1313
name: Lint
14-
runs-on: ubuntu-latest
14+
# Typically one would use ubuntu-latest for clippy and rustfmt jobs. Use windows-latest in this
15+
# case because most of the code is Windows-specific.
16+
runs-on: windows-latest
1517
env:
1618
RUSTFLAGS: -D warnings
1719
steps:
1820
- uses: actions/checkout@v3
1921
- uses: dtolnay/rust-toolchain@stable
2022
with:
2123
components: rustfmt, clippy
24+
- uses: Swatinem/rust-cache@v2
2225
- name: Lint (rustfmt)
2326
run: cargo xfmt --check
2427
- name: Lint (clippy)
@@ -30,8 +33,8 @@ jobs:
3033
strategy:
3134
matrix:
3235
os: [ windows-latest, ubuntu-latest ]
33-
# 1.41 is the MSRV
34-
rust-version: [ 1.41, stable ]
36+
# 1.49 is the MSRV
37+
rust-version: [ 1.49, stable ]
3538
fail-fast: false
3639
env:
3740
RUSTFLAGS: -D warnings
@@ -41,6 +44,7 @@ jobs:
4144
with:
4245
toolchain: ${{ matrix.rust-version }}
4346
components: rustfmt, clippy
47+
- uses: Swatinem/rust-cache@v2
4448
- name: Build
4549
run: cargo build
4650
- name: Test

.github/workflows/docs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ jobs:
1313
steps:
1414
- uses: actions/checkout@v3
1515
- uses: dtolnay/rust-toolchain@stable
16+
- uses: Swatinem/rust-cache@v2
1617
- name: Build rustdoc
1718
run: cargo doc --all-features
1819
- name: Organize

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ All notable changes to this project will be documented in this file.
44

55
This project adheres to [Semantic Versioning](https://semver.org).
66

7+
## [0.2.0] - 2022-12-10
8+
9+
### Changed
10+
11+
- `enable_ansi_support` now returns `std::io::Error` rather than the raw Win32 error code.
12+
- Internal implementation now uses the windows-sys crate.
13+
- MSRV updated to Rust 1.49, and new MSRV policy defined: updates will be considered a breaking
14+
change.
15+
716
## [0.1.2] - 2022-01-19
817

918
- Update links to new repository location.
@@ -16,6 +25,7 @@ This project adheres to [Semantic Versioning](https://semver.org).
1625

1726
- Initial release.
1827

28+
[0.2.0]: https://github.com/sunshowers-code/enable-ansi-support/releases/tag/0.2.0
1929
[0.1.2]: https://github.com/sunshowers-code/enable-ansi-support/releases/tag/0.1.2
2030
[0.1.1]: https://github.com/sunshowers-code/enable-ansi-support/releases/tag/0.1.1
2131
[0.1.0]: https://github.com/sunshowers-code/enable-ansi-support/releases/tag/0.1.0

Cargo.toml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@ edition = "2018"
66
license = "MIT"
77
readme = "README.md"
88
repository = "https://github.com/sunshowers-code/enable-ansi-support"
9+
rust-version = "1.49"
10+
keywords = [
11+
"ansi",
12+
"windows",
13+
"console",
14+
"terminal",
15+
"color",
16+
"enable-ansi-support",
17+
]
18+
categories = ["command-line-interface", "os::windows-apis"]
919

1020
[target.'cfg(windows)'.dependencies]
11-
winapi = { version = "0.3.9", features = ["consoleapi", "errhandlingapi", "fileapi", "handleapi", "winnt"] }
21+
windows-sys = { version = "0.42.0", features = [
22+
"Win32_Foundation",
23+
"Win32_Storage_FileSystem",
24+
"Win32_Security",
25+
"Win32_System_Console",
26+
] }

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,22 @@ fn main() {
2222
match enable_ansi_support::enable_ansi_support() {
2323
Ok(()) => {
2424
// ANSI escape codes were successfully enabled, or this is a non-Windows platform.
25+
// Use your terminal color library of choice here.
26+
println!("\x1b[31mHello, world\x1b[0m");
2527
}
2628
Err(_) => {
2729
// The operation was unsuccessful, typically because it's running on an older
2830
// version of Windows. The program may choose to disable ANSI color code output in
2931
// this case.
3032
}
3133
}
32-
// Use your terminal color library of choice here.
3334
}
3435
```
3536

3637
## Minimum supported Rust version
3738

38-
The minimum supported Rust version (MSRV) is **1.41**. Unless there's a compelling reason to update it, it is most
39-
likely going to stay at 1.41 forever.
39+
The minimum supported Rust version (MSRV) is **1.49**. The MSRV will be updated sparingly, and any
40+
update to it will be considered a breaking change.
4041

4142
## License and credits
4243

src/lib.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
//! match enable_ansi_support::enable_ansi_support() {
1919
//! Ok(()) => {
2020
//! // ANSI escape codes were successfully enabled, or this is a non-Windows platform.
21+
//! println!("\x1b[31mHello, world\x1b[0m");
2122
//! }
2223
//! Err(_) => {
2324
//! // The operation was unsuccessful, typically because it's running on an older
@@ -44,27 +45,26 @@
4445

4546
/// Enables ANSI code support on Windows 10.
4647
///
47-
/// Returns the Windows error code if unsuccessful.
48+
/// Returns an [`io::Error`](std::io::Error) with the Windows error code in it if unsuccessful.
4849
///
4950
/// On non-Windows platforms, this is a no-op that always returns `Ok(())`.
5051
///
5152
/// # Examples
5253
///
5354
/// See the [crate documentation](crate).
5455
#[cfg(windows)]
55-
pub fn enable_ansi_support() -> Result<(), u32> {
56+
pub fn enable_ansi_support() -> Result<(), std::io::Error> {
5657
// ref: https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#EXAMPLE_OF_ENABLING_VIRTUAL_TERMINAL_PROCESSING @@ https://archive.is/L7wRJ#76%
5758

58-
use std::{ffi::OsStr, iter::once, os::windows::ffi::OsStrExt, ptr::null_mut};
59-
use winapi::um::{
60-
consoleapi::{GetConsoleMode, SetConsoleMode},
61-
errhandlingapi::GetLastError,
62-
fileapi::{CreateFileW, OPEN_EXISTING},
63-
handleapi::INVALID_HANDLE_VALUE,
64-
winnt::{FILE_SHARE_WRITE, GENERIC_READ, GENERIC_WRITE},
65-
};
59+
use std::{ffi::OsStr, iter::once, os::windows::ffi::OsStrExt};
6660

67-
const ENABLE_VIRTUAL_TERMINAL_PROCESSING: u32 = 0x0004;
61+
use windows_sys::Win32::{
62+
Foundation::INVALID_HANDLE_VALUE,
63+
Storage::FileSystem::{
64+
CreateFileW, FILE_GENERIC_READ, FILE_GENERIC_WRITE, FILE_SHARE_WRITE, OPEN_EXISTING,
65+
},
66+
System::Console::{GetConsoleMode, SetConsoleMode, ENABLE_VIRTUAL_TERMINAL_PROCESSING},
67+
};
6868

6969
unsafe {
7070
// ref: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilew
@@ -73,21 +73,21 @@ pub fn enable_ansi_support() -> Result<(), u32> {
7373
OsStr::new("CONOUT$").encode_wide().chain(once(0)).collect();
7474
let console_handle = CreateFileW(
7575
console_out_name.as_ptr(),
76-
GENERIC_READ | GENERIC_WRITE,
76+
FILE_GENERIC_READ | FILE_GENERIC_WRITE,
7777
FILE_SHARE_WRITE,
78-
null_mut(),
78+
std::ptr::null(),
7979
OPEN_EXISTING,
8080
0,
81-
null_mut(),
81+
0,
8282
);
8383
if console_handle == INVALID_HANDLE_VALUE {
84-
return Err(GetLastError());
84+
return Err(std::io::Error::last_os_error());
8585
}
8686

8787
// ref: https://docs.microsoft.com/en-us/windows/console/getconsolemode
88-
let mut console_mode: u32 = 0;
88+
let mut console_mode = 0;
8989
if 0 == GetConsoleMode(console_handle, &mut console_mode) {
90-
return Err(GetLastError());
90+
return Err(std::io::Error::last_os_error());
9191
}
9292

9393
// VT processing not already enabled?
@@ -97,7 +97,7 @@ pub fn enable_ansi_support() -> Result<(), u32> {
9797
console_handle,
9898
console_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING,
9999
) {
100-
return Err(GetLastError());
100+
return Err(std::io::Error::last_os_error());
101101
}
102102
}
103103
}
@@ -107,7 +107,7 @@ pub fn enable_ansi_support() -> Result<(), u32> {
107107

108108
/// Enables ANSI code support on Windows 10.
109109
///
110-
/// Returns the Windows error code if unsuccessful.
110+
/// Returns an [`io::Error`](std::io::Error) with the Windows error code in it if unsuccessful.
111111
///
112112
/// On non-Windows platforms, this is a no-op that always returns `Ok(())`.
113113
///
@@ -116,6 +116,6 @@ pub fn enable_ansi_support() -> Result<(), u32> {
116116
/// See the [crate documentation](crate).
117117
#[cfg(not(windows))]
118118
#[inline]
119-
pub fn enable_ansi_support() -> Result<(), u32> {
119+
pub fn enable_ansi_support() -> Result<(), std::io::Error> {
120120
Ok(())
121121
}

tests/basic.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) The enable-ansi-support Contributors
2+
// SPDX-License-Identifier: MIT
3+
4+
#[test]
5+
fn test_basic() {
6+
let res = enable_ansi_support::enable_ansi_support();
7+
println!("enable_ansi_support status: {:?}", res);
8+
if res.is_ok() {
9+
println!("\x1b[31mHello, world\x1b[0m");
10+
}
11+
}

0 commit comments

Comments
 (0)