Skip to content

Commit 18760c9

Browse files
ChocolateLoverRajdjc
authored andcommitted
Add features to work with no_std, and with alloc in no_std
Always include std feature in make tests I would assume all of the tests require std since this crate originally was std only Add clippy warnings to always use `core` and `alloc` instead of `std` Fix clippy by replacing std with core and alloc when possible Some changes Add --lib when testing without default features Make windows and wasm modules require std feature.
1 parent f628475 commit 18760c9

File tree

8 files changed

+67
-24
lines changed

8 files changed

+67
-24
lines changed

Cargo.toml

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ readme = "README.md"
1313
rust-version = "1.66"
1414

1515
[features]
16-
default = ["unicode-width", "ansi-parsing"]
16+
default = ["unicode-width", "ansi-parsing", "std"]
17+
std = ["dep:libc", "dep:once_cell", "alloc"]
18+
alloc = []
1719
windows-console-colors = ["ansi-parsing"]
1820
ansi-parsing = []
1921

2022
[dependencies]
21-
libc = "0.2.99"
22-
once_cell = "1.8"
23+
libc = { version = "0.2.99", optional = true }
24+
once_cell = { version = "1.8", optional = true }
2325
unicode-width = { version = "0.2", optional = true }
2426

2527
[target.'cfg(windows)'.dependencies]
@@ -43,6 +45,26 @@ proptest = { version = "1.0.0", default-features = false, features = [
4345
] }
4446
regex = "1.4.2"
4547

48+
[[example]]
49+
name = "colors"
50+
required-features = ["std"]
51+
52+
[[example]]
53+
name = "colors256"
54+
required-features = ["std"]
55+
56+
[[example]]
57+
name = "cursor_at"
58+
required-features = ["std"]
59+
60+
[[example]]
61+
name = "keyboard"
62+
required-features = ["std"]
63+
64+
[[example]]
65+
name = "term"
66+
required-features = ["std"]
67+
4668
## These are currently disabled. If you want to play around with the benchmarks
4769
## uncommit this.
4870
#criterion = "0.3.5"

Makefile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ test:
1313
@echo "CARGO TESTS"
1414
@cargo test
1515
@cargo test --all-features
16-
@cargo test --no-default-features
17-
@cargo test --no-default-features --features ansi-parsing
18-
@cargo test --no-default-features --features unicode-width
16+
@cargo test --lib --no-default-features
17+
@cargo test --lib --no-default-features --features alloc
18+
@cargo test --no-default-features --features std
19+
@cargo test --no-default-features --features std,ansi-parsing
20+
@cargo test --no-default-features --features std,unicode-width
1921

2022
check-minver:
2123
@echo "MINVER CHECK"

src/ansi.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use std::{
2-
borrow::Cow,
1+
#[cfg(feature = "alloc")]
2+
use alloc::{borrow::Cow, string::String};
3+
use core::{
34
iter::{FusedIterator, Peekable},
45
str::CharIndices,
56
};
@@ -186,6 +187,7 @@ fn find_ansi_code_exclusive(it: &mut Peekable<CharIndices>) -> Option<(usize, us
186187
}
187188

188189
/// Helper function to strip ansi codes.
190+
#[cfg(feature = "alloc")]
189191
pub fn strip_ansi_codes(s: &str) -> Cow<str> {
190192
let mut char_it = s.char_indices().peekable();
191193
match find_ansi_code_exclusive(&mut char_it) {

src/kb.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use alloc::vec::Vec;
2+
13
/// Key mapping
24
///
35
/// This is an incomplete mapping of keys that are supported for reading

src/lib.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,30 +76,46 @@
7676
//! for stripping and taking ansi escape codes into account for length
7777
//! calculations).
7878
79-
#![warn(unreachable_pub)]
79+
#![warn(
80+
unreachable_pub,
81+
clippy::std_instead_of_core,
82+
clippy::std_instead_of_alloc
83+
)]
84+
#![cfg_attr(not(feature = "std"), no_std)]
85+
#[cfg(feature = "alloc")]
86+
extern crate alloc;
8087

88+
#[cfg(feature = "alloc")]
8189
pub use crate::kb::Key;
90+
#[cfg(feature = "std")]
8291
pub use crate::term::{
8392
user_attended, user_attended_stderr, Term, TermFamily, TermFeatures, TermTarget,
8493
};
94+
#[cfg(feature = "std")]
8595
pub use crate::utils::{
8696
colors_enabled, colors_enabled_stderr, measure_text_width, pad_str, pad_str_with,
8797
set_colors_enabled, set_colors_enabled_stderr, style, truncate_str, Alignment, Attribute,
8898
Color, Emoji, Style, StyledObject,
8999
};
90100

101+
#[cfg(all(feature = "ansi-parsing", feature = "alloc"))]
102+
pub use crate::ansi::strip_ansi_codes;
91103
#[cfg(feature = "ansi-parsing")]
92-
pub use crate::ansi::{strip_ansi_codes, AnsiCodeIterator};
104+
pub use crate::ansi::AnsiCodeIterator;
93105

106+
#[cfg(feature = "std")]
94107
mod common_term;
108+
#[cfg(feature = "alloc")]
95109
mod kb;
110+
#[cfg(feature = "std")]
96111
mod term;
97-
#[cfg(all(unix, not(target_arch = "wasm32")))]
112+
#[cfg(all(unix, not(target_arch = "wasm32"), feature = "std"))]
98113
mod unix_term;
114+
#[cfg(feature = "std")]
99115
mod utils;
100-
#[cfg(target_arch = "wasm32")]
116+
#[cfg(all(feature = "std", target_arch = "wasm32"))]
101117
mod wasm_term;
102-
#[cfg(windows)]
118+
#[cfg(all(feature = "std", windows))]
103119
mod windows_term;
104120

105121
#[cfg(feature = "ansi-parsing")]

src/term.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use std::fmt::{Debug, Display};
1+
use alloc::sync::Arc;
2+
use core::fmt::{Debug, Display};
23
use std::io::{self, Read, Write};
3-
use std::sync::{Arc, Mutex, RwLock};
4-
54
#[cfg(any(unix, all(target_os = "wasi", target_env = "p1")))]
65
use std::os::fd::{AsRawFd, RawFd};
76
#[cfg(windows)]
87
use std::os::windows::io::{AsRawHandle, RawHandle};
8+
use std::sync::{Mutex, RwLock};
99

1010
use crate::{kb::Key, utils::Style};
1111

src/unix_term.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1+
use core::{fmt::Display, mem, str};
12
use std::env;
2-
use std::fmt::Display;
33
use std::fs;
44
use std::io::{self, BufRead, BufReader};
5-
use std::mem;
65
use std::os::fd::{AsRawFd, RawFd};
7-
use std::str;
86

97
#[cfg(not(target_os = "macos"))]
108
use once_cell::sync::Lazy;

src/utils.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
use std::borrow::Cow;
1+
use alloc::borrow::Cow;
2+
use core::{
3+
fmt::{self, Debug, Formatter},
4+
sync::atomic::{AtomicBool, Ordering},
5+
};
26
use std::env;
3-
use std::fmt;
4-
use std::fmt::{Debug, Formatter};
5-
use std::sync::atomic::{AtomicBool, Ordering};
67

78
use once_cell::sync::Lazy;
89

@@ -820,7 +821,7 @@ pub fn truncate_str<'a>(s: &'a str, width: usize, tail: &str) -> Cow<'a, str> {
820821

821822
#[cfg(feature = "ansi-parsing")]
822823
{
823-
use std::cmp::Ordering;
824+
use core::cmp::Ordering;
824825
let mut iter = AnsiCodeIterator::new(s);
825826
let mut length = 0;
826827
let mut rv = None;

0 commit comments

Comments
 (0)